UIDocumentInteractionController瀏覽文件

之前寫的一個項目打開文件直接用的UIWebView,word、excel、tex等等打開很方便。這次寫項目用到了UIDocumentInteractionController,這是IOS提供的使用其他程序打開你的App不支持的文件格式。

    UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:url]];

    [documentInteractionController setDelegate:[FileOpenTool sharedInstance]];

    BOOL canShow = [documentInteractionController presentPreviewAnimated:YES];

    

    if (!canShow) {

        // 無法打開的文件類型

        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        UIViewController *rootVC = appDelegate.window.rootViewController;

        [v presentOptionsMenuFromRect:CGRectZero inView:rootVC.view animated:YES];

    }

這種方法是可行的但是一開始用的時候我並沒有在主線程中使用,因爲是UI的變動,所以一定要在主線程中

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                 // Do time-consuming task in background thread

                 // Return back to main thread to update UI

                 dispatch_sync(dispatch_get_main_queue(), ^{

                     // 瀏覽寫在這裏

                 });

             });



報錯信息如下:

bool _WebTryThreadLock(bool), 0x1556d49d0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1   0x187dd0f8c WebThreadLock
2   0x18929ea68 <redacted>
3   0x18927508c <redacted>
4   0x189274f64 <redacted>
5   0x189408f04 <redacted>
6   0x189049810 <redacted>
7   0x18905803c <redacted>
8   0x1892eb6c0 <redacted>
9   0x18905f4b4 <redacted>
10  0x18924a88c <redacted>
11  0x18936a244 <redacted>
12  0x18924a404 <redacted>
13  0x1892046cc <redacted>
14  0x18910fd28 <redacted>
15  0x18910fa04 <redacted>
16  0x18910f96c <redacted>
17  0x18904c0e4 <redacted>
18  0x1869f2a28 <redacted>
19  0x1869ed634 <redacted>
20  0x1869ed4f4 <redacted>
21  0x1869ecb24 <redacted>
22  0x1869ec86c <redacted>
23  0x186a1bc90 <redacted>
24  0x183b721e0 <redacted>
25  0x183b71d58 <redacted>
26  0x183b7153c pthread_mutex_lock
27  0x183b71020 start_wqthread


發佈了33 篇原創文章 · 獲贊 30 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章