2015-12-10 36 views
0

我使用this project讓我開始使用OSX下的WebKit框架。爲什麼WebView無法使用字符串加載外部URL?

它在本地加載資源,我已經能夠得到它的工作。然而,當我改變awakeFromNib加載像一個URL字符串像http://www.example.com/index.html它不起作用,視圖是空白的,即使這是一個有效的URL。

原文:

- (void)awakeFromNib { 
    WebPreferences *prefs = [webView preferences]; 
    [prefs _setLocalStorageDatabasePath:@"~/Library/WebViewExample/LocalStorage"]; 

    NSString *resourcesPath = [[NSBundle mainBundle] resourcePath]; 
    NSString *htmlPath = [resourcesPath stringByAppendingString:@"/htdocs/640x480_naked.html"]; 
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]]; 

} 

修改爲外部URL:

- (void)awakeFromNib { 
    WebPreferences *prefs = [webView preferences]; 
    [prefs _setLocalStorageDatabasePath:@"/tmp"]; 

    NSString *urlText = @"http://www.example.com/index.html"; 
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]]; 
} 

出了什麼問題?

+0

可能與運輸安全有關? http://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9 – danh

+0

@danh - 謝謝!事實證明是這樣。 – Matt

回答

1

感謝@danh的評論和鏈接。這爲我解決了它。

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSExceptionDomains</key> 
    <dict> 
     <key>example.com</key> 
     <dict> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
      <key>NSExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
     </dict> 
    </dict> 
</dict> 
相關問題