2012-07-04 4 views
1

나는 iPhone으로 포팅하는 안드로이드 앱을 가지고 있으며 중요한 기능은 사용자가 다운로드하는 간단한 텍스트 파일을 열어야한다는 것입니다. 안드로이드에서 일반적인 방법은 사용자가 이메일 첨부 파일로 파일을 가져 오는 것입니다.하지만 사용자가 파일을 다운로드하여 내 앱에서 열 수 있도록 할 수는 있습니다. iPhone에서이 작업을 수행 할 수 있습니까?iPhone에 파일을 저장할 수 있습니까?

+1

[여기] (http://stackoverflow.com/a/4346034/335858)는 전자 메일 첨부 파일을 열 때 좋은 대답입니다. – dasblinkenlight

+1

메일을 사용하지 않는 경우 가장 일반적인 다운로드 방법에는 NSURLConnection 클래스가 포함됩니다. –

+1

다음은 문서 디렉토리에 저장된 파일을 저장하고 읽는 예제입니다. http://stackoverflow.com/a/5619769/1264925 – sigre

답변

0

텍스트 파일을 어떻게 처리하는지 잘 모르겠지만 사용자가 응용 프로그램의 Mail 응용 프로그램에서 첨부 파일을 열 것을 선택하면 첨부 된 전자 메일에서 텍스트 파일을 검색 할 수 있습니다.

먼저 텍스트 파일을 열 수 있다고 앱을 등록해야합니다. 이렇게하려면 앱의 의 Info.plist 파일로 이동하고 다음 섹션을 추가 :이 앱은 텍스트 파일을 열 수 있습니다 아이폰 OS를 말할 것이다

<key>CFBundleDocumentTypes</key> 
    <array> 
     <dict> 
     <key>CFBundleTypeName</key> 
     <string>Text Document</string> 
     <key>CFBundleTypeRole</key> 
     <string>Viewer</string> 
     <key>LSHandlerRank</key> 
     <string>Alternate</string> 
     <key>LSItemContentTypes</key> 
     <array> 
      <string>public.text</string> 
     </array> 
    </dict> 
</array> 

. 이제 "Safari 또는 Mail"에서 "Open In ..."이라는 버튼이 있고 사용자가 텍스트 파일을 열고 싶다면 앱이 목록에 표시됩니다.

당신은 또한 당신의 AppDelegate에있는 텍스트 파일의 개방을 처리해야합니다 :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileURLFromApp) name:@"fileURLFromApp" object:nil]; 

:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{  
if (url != nil && [url isFileURL]) { 
    //Removes the un-needed part of the file path so that only the File Name is left 
    NSString *newString = [[url absoluteString] substringWithRange:NSMakeRange(96, [[url absoluteString] length]-96)]; 
    //Send the FileName to the USer Defaults so your app can get the file name later 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:newString forKey:@"fileURLFromApp"]; 
    //Save the Defaults 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    //Post a Notification so your app knows what method to fire 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"fileURLFromApp" object:nil]; 
} else { 
} 
return YES; 
} 

당신은 당신의 ViewController.m에 그 통지에 등록해야합니다 그런 다음 필요한 방법을 만들고 파일을 검색 할 수 있습니다.

- (void) fileURLFromApp 
{ 
//Get stored File Path 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *filePath = [defaults objectForKey:@"fileURLFromApp"]; 
NSString *finalFilePath = [documentsDirectory stringByAppendingPathComponent:filePath]; 
//Parse the data 
//Remove "%20" from filePath 
NSString *strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
//Get the data from the file 
NSString* content = [NSString stringWithContentsOfFile:strippedContent encoding:NSUTF8StringEncoding error:NULL]; 

위의 방법은 NSString의 텍스트 파일 내용을 content

으로 줄 것입니다. 행운을 빌어 요!

관련 문제