2015-10-08 68 views
0

我有一個本地的HTML(靜態內容和無鏈接內部)文件,需要在Web視圖中加載。我已經使用UIWebView加載它,但升級到Swift 2.0和Xcode 7後,現有的實現在presentViewController這一行崩潰,我們嘗試呈現內部具有web視圖但以前工作正常的視圖控制器。這似乎是因爲WebKit介紹的問題,但它不應該影響現有的執行權。所以任何人都可以幫助我,或者建議我走最好的路? 該項目的目標是iOS 8及以上版本(僅iPad)。 這是正在無法在Swift 2.0和Xcode 7中的WebView中加載本地HTML文件

let path = NSBundle.mainBundle().URLForResource("index", withExtension: "html") 
let prepareRequest = NSURLRequest(URL: path!) 
self.webView.loadRequest(prepareRequest) 

感謝

Crash Report  
1 0x8707b09 StartWebThread()         
2 0x524d6b0 __pthread_once_handler  
3 0x523eebe _os_once  
4 0x524d65f pthread_once  
5 0x870789e WebThreadEnable  
6 0x75ce14a +[WebView(WebPrivate) enableWebThread]  
7 0x75cad90 WebKitInitialize   
8 0x2822a30 ___UIApplicationLoadWebKit_block_invoke  
9 0x4f0abef _dispatch_client_callout  
10 0x4ef4a92 dispatch_once_f  
11 0x4ef4922 dispatch_once  
12 0x2822940 _UIApplicationLoadWebKit   
13 0x4083461 _class_initialize   
14 0x408bfe5 lookUpImpOrForward   
15 0x408be8d _class_lookupMethodAndLoadCache3   
16 0x409612f objc_msgSend   
17 0x2cef77a UINibDecoderDecodeObjectForValue   
18 0x2cef9b0 UINibDecoderDecodeObjectForValue    
19 0x2cef4c3 -[UINibDecoder decodeObjectForKey:]   
20 0x289480c -[UIView initWithCoder:]   
21 0x28be337 -[UIScrollView initWithCoder:]   
22 0x2cef7ae UINibDecoderDecodeObjectForValue   
23 0x2cef4c3 -[UINibDecoder decodeObjectForKey:]   
24 0x2b6cbca -[UIRuntimeConnection initWithCoder:]  
25 0x2cef7ae UINibDecoderDecodeObjectForValue   
26 0x2cef9b0 UINibDecoderDecodeObjectForValue   
27 0x2cef4c3 -[UINibDecoder decodeObjectForKey:] 
28 0x2b6be80 -[UINib instantiateWithOwner:options:]   
29 0x29860d4 -[UIViewController _loadViewFromNibNamed:bundle:]   
30 0x298686b -[UIViewController loadView]   
31 0x2986a9f -[UIViewController loadViewIfRequired] 

回答

0

我測試你的代碼上一個新項目的代碼,它不會崩潰我。當您升級到Xcode7時,您是否禁用了App Transport Security?

如果您還沒有禁用,請將此內容添加到您的列表中。

<key>NSAppTransportSecurity</key> 
    <dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 
+0

是的,我已經添加了plist中的代碼,仍然我得到崩潰。在主要問題中添加了崩潰報告 – Sandy

+0

您不需要'NSAllowsArbitraryLoads'來加載文件。它只與網絡請求有關。 –

0

從文檔:

使用loadHTMLString:基本URL:方法開始加載本地HTML文件或的loadRequest:方法開始加載網頁內容。

也許他們已經改變了一些出於安全原因:

爲了幫助您避免受到安全攻擊,一定要使用此方法加載本地HTML文件;不要使用loadRequest:

我使用loadHTMLString:baseURL:並且像魅力一樣工作。它需要一個字符串,所以你應該得到html文件的內容並將其存儲在一個String中。

試試這個辦法,讓我知道

0

您可以使用下面的新iOS9-只有API:

func loadFileURL(_ URL: NSURL, allowingReadAccessToURL readAccessURL: NSURL) -> WKNavigation?

第二NSURL應該如果指向一個目錄index.html需要從你的包中加載資源。

0

我自己找到了答案。當我們使用dispatch_sync打開彈出窗口時,我們可以避免崩潰,並且適用於我。

dispatch_async(dispatch_get_main_queue()){() - >虛空中 self.presentViewController(popoverViewController,動畫:真,完成:無) }

相關問題