Application Transport Security in Safari Extension

My app extension needs to open the URL from many websites. I do the following:

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); 

I can get the url, but at this moment I got this error:

App Transport Security has blocked the loading of the plaintext HTTP resource (http: //) because it is unsafe. Temporary exceptions can be configured through your application's Info.plist file.

I tried to completely disable ATS,

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

but it does not work, and I cannot list the websites inside NSExceptionDomain. I tried on the simulator and on the device. Can anybody help?

EDIT

I think the code causing the problem is:

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

I use this line of code after the url log to get html like plain text.

+8
ios objective-c ios9 nsstring nsurl
source share
2 answers

Do you NSAppTransportSecurity dictionary to the extension of the Info.plist application or only to the parent Info.plist application? Because if the extension is executing queries, the exception should be in the Info.plist extension file .

If this does not help, try using NSURLConnection directly and see if it matters. I doubt it will happen, but it might be worth a try.

+5
source share

I had the same problem, but installing NSAllowsArbitraryLoads on YES fixed it for me. I suggest trying:

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

* note that I use usedEncoding instead of encoding.

this will allow you to get an error message and see what the best encoding is. Perhaps you are using the wrong encoding or that the file you are transmitting cannot be decoded; those. .mp4, .m4a and .mp3 files will not work, but .m3u8 will.

+3
source share

All Articles