在應用中,我們有時想預覽文件,但是又不想為此而專門寫一個viewController。這時QLPreviewController的作用就來了。
QLPreviewController最簡單的用法如下:
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [path stringByAppendingPathComponent:@"test.jpg"];
NSURL *fileRUL = [NSURL fileURLWithPath:fileName];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileRUL];
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentPreviewAnimated:YES];
結果如下:
具體如何設定預覽的內容和行為,是在QLPreviewControllerDelegate和QLPreviewControllerDataSource中完成的,所以這里只需要在controller中實現QLPreviewControllerDelegate和QLPreviewControllerDataSource相關方法即可
// Returns the number of items that the preview controller should preview - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController { NSInteger numToPreview = 0; NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; if (selectedIndexPath.section == 0) numToPreview = NUM_DOCS; else numToPreview = self.documentURLs.count; return numToPreview; } // returns the item that the preview controller should preview - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx { NSURL *fileURL = nil; NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; if (selectedIndexPath.section == 0) { fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[idx] ofType:nil]]; } else { fileURL = [self.documentURLs objectAtIndex:idx]; } return fileURL; }
另外一些解決方案:
在SDK中打開其他接入應用的解決方案
http://blog.csdn.net/arthurchenjs/article/details/6920631
應用程序間通訊 - URL Scheme
http://blog.csdn.net/flower4wine/article/details/6454957
在Mac和iOS中注冊自定義的URL Scheme
http://cocoa.venj.me/blog/custom-url-scheme-on-mac-and-ios/
iOS應用中打開一些其他應用的URL-Scheme
iPhone開發技巧 URL Scheme啟動進程調試教程
http://mobile.51cto.com/iphone-278973.htm
應用之間調用
http://blog.csdn.net/pjk1129/article/details/6641211