ios 使用其它應用程序打開文件 踩過的一個坑

在自己的App裏面想把文件導出去,使用其它的App打開,查了文檔之後,開始寫代碼,起初使用如下:

- (void)exportFileToOtherApp:(NSString*)filePath

{

    NSURL *url = [NSURL fileURLWithPath:filePath];

    UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];

    [documentInteractionController setDelegate:self];

    CGRect rect = CGRectMake(self.view.bounds.size.width, 40.0, 0.0, 0.0);

    [documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];

}

運行起來後,可以彈出來選擇程序的窗口,也可以選擇App,但是選了App後,就沒反應了,文件沒導出去,實驗來實驗去,最後問題終於解決了,原來是:UIDocumentInteractionController *documentInteractionController這個局部變量惹的禍。

把UIDocumentInteractionController *documentInteractionController移到頭文件裏面,一切正常。

總結下來,應該是把變量定義在了函數裏面,結果函數走完,窗口彈出後,變量的生命期結束了,所以再在彈出的窗口裏面選擇App導出文件,這時變量的內容估計已爲空了,所以沒有內容可導出。

把變量定義爲全局的,就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章