天天看點

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導出檔案,這時變量的内容估計已為空了,是以沒有内容可導出。

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