我将Xcode更新为7.0或iOS 9.0时遇到问题 . 不知怎的,它开始给我 Headers 错误
“无法加载资源,因为App Transport Security策略要求使用安全连接”
Web服务方法:
-(void)ServiceCall:(NSString*)ServiceName :(NSString *)DataString
{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
[sessionConfiguration setAllowsCellularAccess:YES];
[sessionConfiguration setHTTPAdditionalHeaders:@{ @"Accept" : @"application/json" }];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",ServiceURL]];
NSLog(@"URl %@%@",url,DataString);
// Configure the Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:[NSString stringWithFormat:@"%@=%@", strSessName, strSessVal] forHTTPHeaderField:@"Cookie"];
request.HTTPBody = [DataString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = @"Post";
// post the request and handle response
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// Handle the Response
if(error)
{
NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
// Update the View
dispatch_async(dispatch_get_main_queue(), ^{
// Hide the Loader
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] delegate].window animated:YES];
});
return;
}
NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL];
for (NSHTTPCookie * cookie in cookies)
{
NSLog(@"%@=%@", cookie.name, cookie.value);
strSessName=cookie.name;
strSessVal=cookie.value;
}
NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
}
该服务运行良好的Xcode早期版本和iOS以前的版本但是当我更新到iOS 9.0上的Xcode 7.0时,它开始给我一个问题,如我调用上述Web服务方法时跟随 . 我得到的记录错误是:
连接失败:错误Domain = NSURLErrorDomain Code = -1022“无法加载资源,因为App Transport Security策略要求使用安全连接 . ” UserInfo = {NSUnderlyingError = 0x7fada0f31880 ,NSErrorFailingURLStringKey = MyServiceURL,NSErrorFailingURLKey = MyServiceURL,NSLocalizedDescription =无法加载资源,因为App Transport Security策略要求使用安全连接 . }
我试过以下问题和答案,但没有得到任何结果,有任何提前的想法,我如何删除该服务调用错误?
18 回答
无法加载资源,因为App Transport Security策略需要使用在Swift 4.03中工作的安全连接 .
打开你的pList.info作为源代码并粘贴:
iOS 9(可能)迫使开发人员独占使用 App Transport Security . 我随意偷听了这个,所以我不知道这是否属实 . 但我怀疑它并得出了这样的结论:
The app running on iOS 9 will (maybe) no longer connect to a Meteor server without SSL.
这意味着运行meteor run ios或meteor run ios-device将(可能?)不再有效 .
在app的info.plist中,
NSAppTransportSecurity [Dictionary]
需要将一个键NSAllowsArbitraryLoads [Boolean]
设置为YES
,或者Meteor需要尽快使用https
作为localhost server
.我设法通过结合许多提到的选项来解决这个问题 . 我将列出一份清单,列出我必须做的所有事情才能让它发挥作用 .
简而言之:
为我的 Watch 扩展程序(不是我的 Watch 应用程序)将
NSAllowsArbitraryLoads
设置为true .确保我使用的是
https
而不是http
.Step one:
首先,最明显的是我必须在我的监视扩展名
info.plist
中添加NSAppTransportSecurity
键作为字典,并使用名为NSAllowsArbitraryLoads
的子键作为布尔值设置为true . 仅在 Watch 扩展中设置此项,而不是 Watch 应用程序的plist . 虽然请注意这允许所有连接并且可能不安全 .要么
Step two:
然后我必须确保我尝试加载的网址是
https
而不仅仅是http
. 对于我使用的任何仍然是http的网址:Swift :
let newURLString = oldURLString.stringByReplacingOccurrencesOfString("http", withString: "https")
Obj-C:
NSString *newURLString = [oldURLString stringByReplacingOccurrencesOfString:@“http” withString:@“https”];
在Xcode 7.1之后(swift 2.0)
我通过在info.plist中添加一些键来解决它 . 我遵循的步骤是:
打开我的项目目标的
info.plist
文件添加了一个名为
NSAppTransportSecurity
的密钥作为Dictionary
.添加了一个名为
NSAllowsArbitraryLoads
的子键为Boolean
,并将其值设置为YES
,如下图所示 .清理项目,现在一切正常,就像以前一样 .
参考链接:https://stackoverflow.com/a/32609970
EDIT: 或者在
info.plist
文件的源代码中我们可以添加:Make sure you change the right info.plist file .
这是 second time 我在这个问题上浪费时间,因为我没有在MyProjectNameUITests下更改info.plist .
From Apple documentation
如果您正在开发新的应用程序,则应该专门使用HTTPS . 如果您有现有应用,则应尽可能多地使用HTTPS,并创建一个计划,以便尽快迁移其余应用 . 此外,您通过更高级别的API进行的通信需要使用具有前向保密性的TLS 1.2版进行加密 . 如果您尝试 Build 不具有Info.plist文件的连接 .
绕过应用程序传输安全性:
允许所有不安全的域
阅读更多:Configuring App Transport Security Exceptions in iOS 9 and OSX 10.11
您只需要在URL中使用HTTPS而不是HTTP,它就可以使用
如果您使用的是Xcode 8.0和swift 3.0或2.2
对于iOS 10.x和Swift 3.x [也支持以下版本]只需在'info.plist'中添加以下行
如果您不是XML的忠实粉丝,那么只需在plist文件中添加以下标记即可 .
If you are using Xcode 8.0 to 8.3.3 and swift 2.2 to 3.0
打开 pList.info 作为 Source Code ,在
</dict>
之前的底部添加以下代码,最后用您的基本网址更改
your.domain.com
. 谢谢 .我已解决为plist文件 .
添加NSAppTransportSecurity:Dictionary .
将名为“NSAllowsArbitraryLoads”的子项添加为布尔值:YES
我已经使用一年签名证书而不是选项“NSAllowsArbitraryLoads”解决了自托管解析服务器的问题
解析服务器,因为任何node.js服务器都会显示您必须指定的 public https url . 例如:
parse-server --appId --masterKey --publicServerURL https://your.public.url/some_nodejs
随意看看我的configuration files
请注意,使用
NSAllowsArbitraryLoads = true
允许与任何服务器的所有连接都不安全 . 如果您想确保只能通过不安全的连接访问 specific domain ,请尝试以下操作:或者,作为源代码:
编辑后清理和构建项目 .
Transport security 在iOS 9.0或更高版本以及OS X v10.11及更高版本中提供 .
因此,默认情况下,只允许在应用程序中使用 https 次调用 . 要关闭App Transport Security,请在 info.plist 文件中添加以下行...
欲了解更多信息:
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33
在Swift 4中你可以使用
贝娄图像看起来像