[iPhone Dev] Good Reader나 카카오톡 등와 같은 앱과 이미지를 주고 받기 (UIDocumentInteractionController)
May 23
iphone data sharing, File System, ios, iPad, iphone, launching, UIDocumentInteractionControlle 1 Comment
아래 그림과 같이 앱에서 이미지 등의 데이트를 다른 앱으로 보내면서 그 앱을 띄우거나, 다른 앱에서 이미지를 받아 앱이 뜨는 것에 관한 포스팅입니다.
(Good Reader나 카카오톡과 같은 앱으로 이미지를 보내서 띄우거나 받아서 앱이 뜨는 것을 말하겠죠?)
애플의 아래 문서를 참고하면 될 것입니다. 이 것을 참고해서 만들어봤습니다.
UIDocumentInteractionController Class Reference
Opening Files Whose Type Is Unknown
[보내는 쪽]
그림과 같이 Sender에서 UIDocumentInteractionController 을 구현한 앱 목록을 보여주고 앱을 띄우는 방법입니다.
UIDocumentInteractionControllerDelegate을 구현하고 openReceiver 부분을 구현해주면 됩니다.
presentOpenInMenuFromRect:launchButton 는 여러가지가 있습니다.
delegate
@interface SenderAppDelegate : NSObject <UIApplicationDelegate, UIDocumentInteractionControllerDelegate> {
-(void)openReceiver {
NSURL* tempURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"]];
NSLog(@"url: %@", [tempURL absoluteURL]);
self.tempC = [UIDocumentInteractionController interactionControllerWithURL:tempURL];
self.tempC.delegate = self;
[self.tempC presentPreviewAnimated:YES];
[self.tempC presentOpenInMenuFromRect:launchButton.frame inView:self.window animated:YES];
return;
...
[받는 쪽]
JPEG (jpg) 이미지 열기 지원을 위해 받는 쪽은 아래와 같이 합니다.
1. info.plist에 다음 키를 추가
CFBundleDocumentTypes
이름이 Documents Type으로 변경됩니다.
2. CFBundleTypeIconFiles 은 지정하지 않으면 앱의 아이콘이 사용됩니다. 그냥 이렇게 하면 되겠죠.
3. Document Type Name 에 “JPEG” 정도를 입력합니다.
4. LSItemContentTypes 을 추가
item0에 public.jpeg 을 추가합니다.
제가 위와 같이 추가한 것은 아래 그림과 같고

아래와 같이 여러 파일 포맷을 지원할 수 있습니다.
PNG와 JPEG 정도 지원하면 되겠죠?

5. 앱이 위와 같이 이미지를 전달 받아서 launching되면 application:handleOpenURL: 이나 application:openURL:sourceApplication:annotation: 이 불리는데,
application:handleOpenURL:은 4.1 이하에서 지원되지만 곧 deprecated 될 예정이라고 합니다.
application:openURL:sourceApplication:annotation: 은 4.2 이상에서 지원되는데요,
어차피 call-back이라 둘 다 넣어 두면 되게습니다.
전달 받는 URL이 sender가 보내준 URL이고 이 것으로 NSData를 만든 후 UIImage를 만들면 됩니다.
아래와 같이~
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(@"url: %@", [url absoluteURL]);
NSData* imageData = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:imageData];
imageView.image = image;
return YES;
}
Xcode 3.X 의 경우는 추가가 잘 안되는 경우가 있는 것 같은데요.
이 경우는 아래 코드를 직접 plist를 열어서 추가해도 됩니다.
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string></string>
</array>
<key>CFBundleTypeName</key>
<string>JPG</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
</array>
</dict>
</array>
