Tot*_*tka 7 objective-c nsurl nsstring ios ios9
我的应用扩展程序需要从许多网站打开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);
Run Code Online (Sandbox Code Playgroud)
我可以获取URL,但此时我收到此错误:
App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全.可以通过应用程序的Info.plist文件配置临时例外.
我试图完全关闭ATS,
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key> <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我无法在NSExceptionDomain中列出网站.我试过模拟器和设备.有人可以帮忙吗?
编辑
我认为导致问题的代码是这样的:
NSString* htmlString = [NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding]
Run Code Online (Sandbox Code Playgroud)
我在url log之后使用这行代码将html作为纯文本.
您是将NSAppTransportSecurity字典添加到应用程序扩展的Info.plist文件中,还是仅添加到父应用程序的Info.plist文件中?因为如果扩展程序发出请求,则异常需要位于扩展程序的Info.plist文件中。
如果这没有帮助,请尝试NSURLConnection直接使用,看看是否有任何区别。我怀疑会,但可能值得一试。