iOS Obj-C:如何使用默认应用程序打开本地文件?

时间:2017-08-17 15:26:11

标签: ios objective-c uidocumentinteraction

我搜索过&尝试了我在stackoverflow上发现的任何东西,但不能继续解决这个问题。

我正在创建一个GPS应用程序,我希望与另一个名为SkyDemon的(专门用于航空导航)进行交互。

在最后一个,您可以导出路线,并通过电子邮件接收。然后它包含一个名为MyRoute.flightplan的附件文件(它基本上是一个包含多个信息的xml文件),可以直接从“mail”应用程序打开,例如,当触摸应用程序时,它会正确打开它上面的路径。 The email received, with the attached file, that can be directly opened by touching it The route opened on SkyDemon App

我尝试通过创建具有良好扩展名的本地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获取与打开“mail”相同的控制器:

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-08-17 17:16:49.709766 + 0200 MyApp [22530:4191257] ***断言失败 - [_ UIDocumentInteractionControllerOpenWithAppActivity performActivity],/ BuildRoot / Library / Cache / com.apple.xbs / Sources / UIKit /的UIKit-3600.9.1 / UIDocumentInteractionController.m:420

     

2017-08-17 17:16:49.712475 + 0200 MyApp [22530:4191257] ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'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)

有人有想法吗?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。 这是因为UIDocumentInteractionController必须保留在可以在要显示的视图之前释放的事件之外。 这意味着,如果在按钮事件中声明了UIDocumentInteractionController,那么除非您已将其指针声明为全局(或在此范围之外),否则您将收到此错误。 在我的示例中,添加

UIDocumentInteractionController *documentController;

在视图控制器的.h文件中,并直接使用documentController指针进入按钮触摸代码解决问题。

此外,在iOS 10中,writeToFile编码模式必须是 NSUTF8StringEncoding ,而不是 NSStringEncodingDetectionAllowLossyKey

相关问题