2015-09-07 60 views
8

我的應用程序擴展需要打開許多網站的URL。我做如下:Safari擴展應用程序傳輸安全

for (NSExtensionItem *item in self.extensionContext.inputItems) { 

    for (NSItemProvider *itemProvider in item.attachments) { 

     if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) { 

      [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) { 
       NSLog(@"URL: %@",url); 

我能得到的URL,但在這一點上,我得到這個錯誤:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

我想徹底關閉ATS,

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

但它不起作用,並且我無法在NSExceptionDomain中列出網站。我嘗試在模擬器和設備上。有人可以幫忙嗎?

編輯

我認爲,這會導致問題的代碼是這樣的:

NSString* htmlString = [NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding] 

我使用這行代碼URL日誌後才能獲取HTML爲純文本。

回答

5

你加入NSAppTransportSecurity字典應用程序擴展的的Info.plist文件,或者只是在父應用程序的的Info.plist文件?因爲如果分機正在發出請求,則該例外需要位於分機的Info.plist文件中。

如果這沒有幫助,請直接嘗試使用NSURLConnection,看看它是否有任何區別。我懷疑它會,但它可能值得一試。

3

我遇到了同樣的問題,但將NSAllowsArbitraryLoads設置爲YES修復了我的問題。我建議嘗試:

NSError *error; 

NSStringEncoding encoding; 

NSString *content = [NSString stringWithContentsOfURL:contentURL usedEncoding:&encoding error:&error]; 

*請注意,我正在使用usedEncoding而不是編碼。

這將允許你得到一個錯誤,看看最好的編碼是什麼。這可能是因爲您使用了錯誤的編碼或者您傳遞的文件無法解碼;即.mp4,.m4a和.mp3文件將不起作用,但.m3u8將會起作用。

+0

感謝您的回答,但它對我無效。在plist中的值設置爲YES,並且我嘗試了您的方法來獲取html,但它返回給我 – Totka

+0

@Totka我假設您已經在ios8中嘗試了此URL並且它可以正常工作。我發現有時在http上添加一個「s」使它可以在ios9上運行。 –