//// ViewController.m// test_UIdocument_01//// Created by admin on 3/27/16.// Copyright © 2016 jeffasd. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong)UIButton *presentButton;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _presentButton = [UIButton buttonWithType:UIButtonTypeCustom]; _presentButton.frame = CGRectMake(100, 100, 200, 200); _presentButton.backgroundColor = [UIColor redColor]; [self.view addSubview:_presentButton]; }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSString *fileString = [[NSBundle mainBundle] pathForResource:@"ios" ofType:@"pdf"]; //// NSURL *fileUrl = [NSURL URLWithString:fileString];// NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"ios" withExtension:@"pdf"];//// NSLog(@"the fileUrl is %@", fileUrl);// // NSURL *testFileUrl = [NSURL fileURLWithPath:fileString];// // NSLog(@"the file url is %@", testFileUrl);// // NSString *string1 = fileUrl.path;// // NSArray *array = fileUrl.pathComponents;// // NSString *string2 = fileUrl.parameterString; // [NSFileManager ]; UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileUrl];//這樣寫處出現
<p class="p1"><span class="s1"><strong>2016-03-27 14:20:08.253 test_UIdocument_01[743:44711] -[NSMachPort _openDocumentWithApplication:]: unrecognized selector sent to instance 0x170169fc0</strong></span></p><p class="p1"><span class="s1"><strong>2016-03-27 14:20:08.255 test_UIdocument_01[743:44711] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSMachPort _openDocumentWithApplication:]: unrecognized selector sent to instance 0x170169fc0'</strong></span></p><p class="p1"><span class="s1"><strong>*** First throw call stack:</strong></span></p><p class="p1"><span class="s1"><strong>(0x18673822c 0x1981a80e4 0x18673f2f4 0x18673c0a8 0x18663e96c 0x18b701a80 0x18b89a7f4 0x18b4baab0 0x18b4bcf2c 0x18b2d6db8 0x18b1ec55c 0x18b1ec0c4 0x18b1ebfcc 0x18aaf562c 0x1000bcf94 0x1000c1c28 0x1866ef544 0x1866ed5ec 0x186618f74 0x18ff3b6fc 0x18b21ad94 0x100084960 0x198852a08)</strong></span></p><p class="p1"><span class="s1"><strong>libc++abi.dylib: terminating with uncaught exception of type NSException</strong></span></p><p class="p2"><span class="s1"><strong>(lldb) </strong></span></p>這個錯誤 原因是:<div>查閱了一下Apple Developer上的文檔,原來,在ARC環境下展示<code>UIDocumentInteractionController</code>時,當我的函數方法調用完畢,退棧之後,<code>UIDocumentInteractionController</code>的執行個體就被釋放掉了,展示出來的這個View由<code>Quick Look</code>架構來操作,並不會對<code>UIDocumentInteractionController</code>產生引用。當點擊View上面的Button時,內部操作仍然會繼續訪問這個<code>UIDocumentInteractionController</code>執行個體,就會報出上述錯誤。</div> // [documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES]; UIView *view = [UIView new]; view.frame = CGRectMake(100, 200, 300, 300); view.backgroundColor = [UIColor cyanColor]; [self.view addSubview:view]; [documentController presentOpenInMenuFromRect:CGRectMake(100, 200, 300, 300) inView:self.view animated:YES];
//iPhone 下面不能指定 rect 只會在底部 下面彈出,iPad 上面可以指定 彈出的 Rect
}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end