App Transport Security on Safari Extension

泪湿孤枕 提交于 2019-12-08 18:47:57

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32442519/app-transport-security-on-safari-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!