Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App Transport Security on Safari Extension

My App extension need to open URL from many websites. I do as 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 point I got this error:

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.

I tried to completely turn off ATS,

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

but it doesn't work, and I cannot list websites inside NSExceptionDomain. I tried on simulator and on device. Somebody could help?

EDIT

i think that the code that cause the issue it's this:

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

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

like image 430
Totka Avatar asked Sep 07 '15 16:09

Totka


2 Answers

Are you adding the NSAppTransportSecurity dictionary to the app extension's Info.plist file, or just in the parent app's Info.plist file? Because if the extension is making the requests, the exception needs to be in the extension's Info.plist file.

If that doesn't help, try using NSURLConnection directly and see if it makes any difference. I doubt it will, but it might be worth a try.

like image 189
dgatwood Avatar answered Oct 20 '22 06:10

dgatwood


I was experiencing the same problem, but setting NSAllowsArbitraryLoads to YES fixed it for me. I suggest trying:

NSError *error;

NSStringEncoding encoding;

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

*please note that I am using usedEncoding instead of encoding.

this will allow you to get an error and see what the best encoding is. It might be that you are using the wrong encoding or that the file you are passing can't be decoded; i.e. .mp4, .m4a, and .mp3 files won't work but .m3u8 will.

like image 33
Karim Mourra Avatar answered Oct 20 '22 08:10

Karim Mourra