Copyright © Mike's Blog
Design by Dzignine
2013年11月26日 星期二

UIDocumentInteractionController - 分享至其他應用程式

這是從iOS3.2就支援的一個Class,沒想到之前都沒有用過。
通常我們要分享或是開啓到別的App方式可能都會先想到用- (BOOL)openURL:(NSURL *)url;方式,
但是如果想要開啓一些第三方的App總不能去google下來各家URL Scheme吧?

於是在google大神中找到答案,沒想的這個Controller意外的好用,會自動找iPhone上面支援的App開起,
甚至還可以預覽,而且Code還非常的簡單:

#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController <UIDocumentInteractionControllerDelegate>
//如果要preview的話就要加上delegate
@end
@implementation MyViewController

- (void)shareToOtherAppsWithPreview:(BOOL)isPreivew
{

NSURL *URL = [[NSBundle mainBundle] URLForResource:@"hello" withExtension:@"jpg"];
UIDocumentInteractionController *documentVC = [UIDocumentInteractionController interactionControllerWithURL:URL];

if( isPreivew ) {
//如果要preview的話才需要設定
[documentVC setDelegate:self];

[documentVC presentPreviewAnimated:YES];
} else {

//打開分享Menu
[documentVC presentOpenInMenuFromRect:[self.textView frame] inView:self.view animated:YES];
}
}
@end

簡簡單單幾行code就可以達到分享的功能,而且是支援手機所有第三方的App。
注意URL要傳入的是你實體的file path,而不是string喔!~

參考連結:
http://mobile.tutsplus.com/tutorials/iphone/previewing-and-opening-documents-with-uidocumentinteractioncontroller/

0 意見:

張貼留言