나는 iPhone으로 포팅하는 안드로이드 앱을 가지고 있으며 중요한 기능은 사용자가 다운로드하는 간단한 텍스트 파일을 열어야한다는 것입니다. 안드로이드에서 일반적인 방법은 사용자가 이메일 첨부 파일로 파일을 가져 오는 것입니다.하지만 사용자가 파일을 다운로드하여 내 앱에서 열 수 있도록 할 수는 있습니다. iPhone에서이 작업을 수행 할 수 있습니까?iPhone에 파일을 저장할 수 있습니까?
1
A
답변
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
으로 줄 것입니다. 행운을 빌어 요!
관련 문제
- 1. SQLite는 iPhone에 얼마나 저장할 수 있습니까?
- 2. iPhone 장치 토큰 : iPhone에 저장할 수 있습니까?
- 3. Core Graphics로 이미지를 iPhone에 저장할 수 있습니까?
- 4. iPhone에 저장할 텍스트 받기
- 5. 브라우저에서 바로 MongoDB에 파일을 저장할 수 있습니까?
- 6. Flash Builder에서 ActionScript로 파일을 저장할 수 있습니까?
- 7. 파일을 현재 디렉토리에 저장할 수 있습니까?
- 8. 어떻게 파일을 삭제할 때 저장할 수 있습니까?
- 9. 어떻게 파일을 날짜로 저장할 수 있습니까?
- 10. 패키지 파일을 다른 디렉토리에 저장할 수 있습니까?
- 11. bash를 사용하여 RAM에 파일을 저장할 수 있습니까?
- 12. iPhone의 파일 시스템에 파일을 저장할 수 있습니까?
- 13. 모듈에 자체 구성 파일을 저장할 수 있습니까?
- 14. AppleScript로 간단하게 파일을 저장할 수 있습니까?
- 15. Git은 GZip 파일을 차별적으로 저장할 수 있습니까?
- 16. PDF 파일을 iPhone에 저장 하시겠습니까
- 17. 음악 파일을 iphone에 다운로드
- 18. Xcode에서 파일을 저장할 수 없습니다
- 19. 앱을 통해 iPhone에 데이터를 저장할 때의 제한
- 20. iOS - iPhone에 등록 된 속성 목록 파일을 수정할 수 있습니까?
- 21. 어떻게 내 xml 파일을 iPhone에 URL에 넣을 수 있습니까?
- 22. iPhone에 iWork 파일을 표시 할 API가 있습니까?
- 23. typename을 저장할 수 있습니까?
- 24. 바이너리를 SVN에 저장할 수 있습니까?
- 25. iphone에 zip 파일을 다운로드하는 방법?
- 26. 파일을 iPhone에 저장하는 방법은 무엇입니까?
- 27. 파일을 응용 프로그램 iPhon/iphone에 저장하려면?
- 28. Eclipse : 파일을 저장할 수 없습니다. 'negative time'오류
- 29. 어떻게하면 sqlite ..에 오디오 파일을 저장할 수 있습니까?
- 30. Java 클래스 <?> 파일을 저장할 수 있습니까?
[여기] (http://stackoverflow.com/a/4346034/335858)는 전자 메일 첨부 파일을 열 때 좋은 대답입니다. – dasblinkenlight
메일을 사용하지 않는 경우 가장 일반적인 다운로드 방법에는 NSURLConnection 클래스가 포함됩니다. –
다음은 문서 디렉토리에 저장된 파일을 저장하고 읽는 예제입니다. http://stackoverflow.com/a/5619769/1264925 – sigre