iOS Obj-C:如何使用其默认应用程序打开本地文件?
我在搜索&尝试了任何我发现在这个上的stackoverflow,但不能继续这个问题。iOS Obj-C:如何使用其默认应用程序打开本地文件?
我正在创建一个GPS应用程序,我想与另一个名为SkyDemon的人(这是专门从事航空导航)进行交互。
在最后一个,您可以导出路线,并通过电子邮件接收它。然后它包含一个名为MyRoute.flightplan的附件文件(它基本上是一个包含多个信息的xml文件),可以直接从“邮件”应用程序打开,例如,当触摸应用程序时,它会正确打开它的路线。
我试图重现的方式“邮件”确实是通过创建具有良好的扩展本地XML文件:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [NSString stringWithFormat:@"%@/directions.flightplan",documentsDirectory];
NSString *content = @"<?xml version=\"1.0\" encoding=\"utf-8\"?><DivelementsFlightPlanner><PrimaryRoute Start=\"N484459.10 E0020640.25\" Level=\"3000\" PlannedFuel=\"120.000000\"><RhumbLineRoute To=\"N483837.45 E0014947.70\" Level=\"MSL\" LevelChange=\"B\" /><ReferencedAirfields /></PrimaryRoute></DivelementsFlightPlanner>";
[content writeToFile:filename atomically:NO encoding:NSStringEncodingDetectionAllowLossyKey error:nil];
然后我打电话UIDocumentInteractionController得到相同的控制器为“邮件“要打开:
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filename]];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
应用答案UIDocumentInteractionControllerDelegate本:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
NSLog(@">>>>>> Start sending to %@", application);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
NSLog(@">>>>>> Done sending.");
}
当我运行我的应用程序得到很好的控制,我可以选择的应用程序“SkyDemon”,然后我有以下断言失败:
2017年8月17日17:16 :49.709766 + 0200 MyApp [22530:4191257] ***声明失败 - [_ UIDocumentInteractionControllerOpenWithAppActivity performActivity],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.9.1/UIDocumentInteractionController.m:420
2017-08-17 17:16:49.712475 + 0200 MyApp [22530:4191257] ***终止应用程序,由于未捕获的异常'NSInternalInconsistencyExcept离子',原因是:'UIDocumentInteractionController过早消失!'
***第一掷调用堆栈: (0x18accefe0 0x189730538 0x18acceeb4 0x18b767720 0x19150b170 0x1917b81d8 0x19114df80 0x191151770 0x190f256f4 0x190f254e4 0x190f24f98 0x190f24b1c 0x190e39338 0x190e39154 0x18dfea0d4 0x10020da10 0x100212b78 0x18ac7d0c8 0x18ac7ace4 0x18abaada4 0x18c615074 0x190e65f74 0x100102878 0x189bb959c) 的libC++ abi.dylib:与未捕获的终止异常类型NSException (lldb)
有没有人有想法?
感谢您的帮助:)
我终于找到了解决方案。 这是由于UIDocumentInteractionController必须保留在可以在要显示视图之前发布的事件之外的事实。 这意味着,如果你在一个按钮事件中声明一个UIDocumentInteractionController,那么你会得到这个错误,除非你已经把它的指针声明为全局的(或者在这个范围之外)。 在我的例子,在视图控制器的.h文件添加
UIDocumentInteractionController *documentController;
,并使用直接documentController指针到按钮触摸代码解决问题。
此外,在IOS 10中,将writeToFile编码模式必须是NSUTF8StringEncoding代替NSStringEncodingDetectionAllowLossyKey。
没有人有想法解决这个问题吗? –