2017-08-17 101 views
0

我在搜索&嘗試了任何我發現在這個上的stackoverflow,但不能繼續這個問題。iOS Obj-C:如何使用其默認應用程序打開本地文件?

我正在創建一個GPS應用程序,我想與另一個名爲SkyDemon的人(這是專門從事航空導航)進行交互。

在最後一個,您可以導出路線,並通過電子郵件接收它。然後它包含一個名爲MyRoute.flightplan的附件文件(它基本上是一個包含多個信息的xml文件),可以直接從「郵件」應用程序打開,例如,當觸摸應用程序時,它會正確打開它的路線。 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得到相同的控制器爲「郵件「要打開:

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)

有沒有人有想法?

感謝您的幫助:)

+0

沒有人有想法解決這個問題嗎? –

回答

0

我終於找到了解決方案。 這是由於UIDocumentInteractionController必須保留在可以在要顯示視圖之前發佈的事件之外的事實。 這意味着,如果你在一個按鈕事件中聲明一個UIDocumentInteractionController,那麼你會得到這個錯誤,除非你已經把它的指針聲明爲全局的(或者在這個範圍之外)。 在我的例子,在視圖控制器的.h文件添加

UIDocumentInteractionController *documentController; 

,並使用直接documentController指針到按鈕觸摸代碼解決問題。

此外,在IOS 10中,將writeToFile編碼模式必須是NSUTF8StringEncoding代替NSStringEncodingDetectionAllowLossyKey

相關問題