Safari 扩展上的应用程序传输安全

App Transport Security on Safari Extension

我的应用程序扩展需要从许多网站打开 URL。我这样做:

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 作为纯文本。

我遇到了同样的问题,但是将 NSAllowsArbitraryLoads 设置为“是”为我解决了这个问题。我建议尝试:

NSError *error;

NSStringEncoding encoding;

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

*请注意,我使用的是 usedEncoding 而不是编码。

这会让你得到一个错误,看看最好的编码是什么。可能是您使用了错误的编码,或者您传递的文件无法解码;即 .mp4、.m4a 和 .mp3 文件将不起作用,但 .m3u8 可以。

您是将 NSAppTransportSecurity 字典添加到应用程序扩展的 Info.plist 文件中,还是只添加到父应用程序的 Info.plist 文件?因为如果扩展正在发出请求,则异常需要在扩展的 Info.plist 文件中。

如果这没有帮助,请尝试直接使用 NSURLConnection 看看是否有任何不同。我怀疑它会,但它可能值得一试。