1. How to open items in other iOS Apps
iOS提供了使用其他app預覽文件的支持,這就是Document Interaction Controller。此外,iOS也支持文件關聯,允許其他程序調用你的app打開某種文件。
-、創建實例
DocumentInteraction Controller使用靜態方法interactionControllerWithURL創建實例,這個方法使用一個NSURL作爲參數。
代碼:
NSURL *url=[NSURL fileURLWithPath:path];
controller = [UIDocumentInteractionController interactionControllerWithURL:url];
二、顯示預覽窗口
Document Interaction Controller對象使用presentPreviewAnimated方法彈出一個全屏的文檔預覽窗口。
代碼:
BOOL b=[controller presentPreviewAnimated:YES];
三、顯示菜單
如果你不想直接彈出預覽窗口,你可以顯示一個選項菜單給用戶,由用戶選擇相應的操作。顯示菜單可以使用下列方法:
–presentOptionsMenuFromRect:inView:animated:
–presentOptionsMenuFromBarButtonItem:animated:
–presentOpenInMenuFromRect:inView:animated:
–presentOpenInMenuFromBarButtonItem:animated:
這些方法都是類似的,只是顯示位置有區別而已。以下代碼演示其中一個方法的使用。
代碼:
CGRect navRect = self.navigationController.navigationBar.frame;
navRect.size = CGSizeMake(1500.0f, 40.0f);
[controller presentOptionsMenuFromRect:navRect inView:self.view animated:YES];
四、使用委託
如果你顯示一個Document Interaction Controller ,則必需要爲delegate屬性用指定一個委託。讓委託告訴DocumentInteraction Controller如何顯示。
代碼:
controller.delegate =self;
委託對象需要實現一系列委託方法,最常見的包括:
–documentInteractionControllerViewControllerForPreview:
–documentInteractionControllerViewForPreview:
–documentInteractionControllerRectForPreview:
這3個方法在用戶點擊“快速查看”菜單時依次調用。
代碼:
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
return self.view.frame;
}
// 點擊預覽窗口的“Done”(完成)按鈕時調用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
[_controller autorelease];
}
+=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+
其實很簡單
1. .h文件
<UIDocumentInteractionControllerDelegate>
UIDocumentInteractionController *documentController;
2. .m文件- (void)openInotherApp{
NSString *documentPath = [[NSBundle mainBundle] pathForResource:@"xcode" ofType:@"pdf"];
NSURL *documentURL = [NSURL fileURLWithPath:documentPath isDirectory:NO];
documentController = [[UIDocumentInteractionController alloc] init];
documentController.delegate = self;
documentController.URL = documentURL;
//documentController.UTI = @"com.microsoft.word.doc";//@"public.png"; //預覽用
[documentController presentOpenInMenuFromRect:CGRectMake(760, 20, 100, 100) inView:self.view animated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
NSLog(@"Starting to send this puppy to %@", application);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
NSLog(@"We're done sending the document.");
}
參考:http://pinkstone.co.uk/how-to-open-items-in-other-ios-apps/
- https://github.com/versluis/Open-In-Other-Apps
- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html#//apple_ref/occ/cl/UIDocumentInteractionController
- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionControllerDelegate_protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40009305