Man*_*Mal 460 nsurlconnection ios nsurlsession ios9 xcode7
我将Xcode更新为7.0或iOS 9.0时遇到问题.不知怎的,它开始给我标题错误
"无法加载资源,因为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];
}
Run Code Online (Sandbox Code Playgroud)
该服务运行良好的Xcode早期版本和iOS以前的版本但是当我更新到iOS 9.0上的Xcode 7.0时,它开始给我一个问题,如我调用上述Web服务方法时跟随.我得到的记录错误是:
连接失败:错误Domain = NSURLErrorDomain Code = -1022"无法加载资源,因为App Transport Security策略要求使用安全连接." UserInfo = {NSUnderlyingError = 0x7fada0f31880 {Error Domain = kCFErrorDomainCFNetwork Code = -1022"(null)"},NSErrorFailingURLStringKey = MyServiceURL,NSErrorFailingURLKey = MyServiceURL,NSLocalizedDescription =无法加载资源,因为App Transport Security策略要求使用安全连接.}
我试过以下问题和答案,但没有得到任何结果,有任何提前的想法,我如何删除该服务调用错误?
Man*_*Mal 893
我已经通过在info.plist中添加一些键来解决它.我遵循的步骤是:
打开我的Project目标info.plist
文件
增加了一个名为关键NSAppTransportSecurity
的Dictionary
.
NSAllowsArbitraryLoads
as 的子键Boolean
,并将其值设置YES
为如下图所示.清洁项目,现在一切正常,就像以前一样.
参考链接:https://stackoverflow.com/a/32609970
编辑:
或者在info.plist
文件的源代码中我们可以添加:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>yourdomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
Arj*_*jan 261
请注意,使用NSAllowsArbitraryLoads = true
允许与任何服务器的所有连接都不安全.如果要确保只能通过不安全的连接访问特定域,请尝试以下操作:
或者,作为源代码:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>domain.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
编辑后清理和构建项目.
Tej*_*ina 40
iOS 9.0或更高版本以及OS X v10.11及更高版本中提供了传输安全性.
因此,默认情况下,仅在应用中允许https调用.要关闭App Transport Security,请在info.plist文件中添加以下行...
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请访问:https:
//developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33
Mr.*_*ean 32
对于iOS 10.x和Swift 3.x [也支持以下版本]只需在'info.plist'中添加以下行
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
Ena*_*que 25
在Swift 4中你可以使用
- >转到Info.plist
- >单击信息属性列表的加号
- >将App Transport Security设置添加为字典
- >单击加号图标应用程序传输安全设置
- >添加允许任意负载设置为YES
贝娄图像看起来像
Avi*_*651 21
我已解决为plist文件.
添加NSAppTransportSecurity:Dictionary.
将名为"NSAllowsArbitraryLoads"的子项添加为布尔值:YES
Sam*_*Sam 17
这是 Apple 强制对您的 apis 进行更严格安全的方式(被迫通过 http 使用 https)。我将解释如何删除此安全设置。
这里的大多数答案都指出将此密钥添加到您的 info.plist
仅此一项并没有为我解决这个问题。我不得不在里面添加相同的密钥
Project -> Targets -> Info -> Custom iOS Target Properties
然而,这将允许任何人发生不安全的连接。如果您只想允许特定域使用不安全连接,您可以将以下内容添加到您的 info.plist。
Kes*_*era 16
无法加载资源,因为App Transport Security策略需要使用在Swift 4.03中工作的安全连接.
打开你的pList.info作为源代码并粘贴:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
Ash*_*k R 11
来自Apple文档
如果您正在开发新应用,则应该专门使用HTTPS.如果您有现有应用,则应尽可能多地使用HTTPS,并创建一个计划,以便尽快迁移其余应用.此外,您通过更高级别的API进行的通信需要使用具有前向保密性的TLS 1.2版进行加密.如果尝试建立不符合此要求的连接,则会引发错误.如果您的应用需要向不安全的域发出请求,则必须在应用的Info.plist文件中指定此域.
绕过应用程序传输安全性:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
允许所有不安全的域
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
阅读更多:在iOS 9和OSX 10.11中配置App Transport Security例外
小智 9
在 XCode 12.5 中。IOS 14.我做了以下条目
这是 info.plist 源代码的样子
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
小智 5
iOS 9(可能)强制开发人员专门使用App Transport Security.我无意中听到了这个地方,所以我不知道这是否属实.但我怀疑它并得出了这个结论:
在iOS 9上运行的应用程序(可能)将不再连接到没有SSL的Meteor服务器.
这意味着运行meteor run ios或meteor run ios-device将(可能?)不再起作用.
在应用程序的info.plist中,NSAppTransportSecurity [Dictionary]
需要NSAllowsArbitraryLoads [Boolean]
设置一个密钥YES
或Meteor需要尽快使用https
它localhost server
.
如果您使用的是Xcode 8.0至8.3.3,而Swift 2.2至3.0
就我而言,需要将URL http://更改为https:// (如果不起作用,请尝试)
Add an App Transport Security Setting: Dictionary.
Add a NSAppTransportSecurity: Dictionary.
Add a NSExceptionDomains: Dictionary.
Add a yourdomain.com: Dictionary. (Ex: stackoverflow.com)
Add Subkey named " NSIncludesSubdomains" as Boolean: YES
Add Subkey named " NSExceptionAllowsInsecureHTTPLoads" as Boolean: YES
Run Code Online (Sandbox Code Playgroud)
对于那些在本地主机上开发的人,请遵循以下步骤:
Information Property List
并添加App Transport Security Settings
并为其分配Dictionary
类型App Transport Security Settings
条目旁边的“+”按钮并添加NSExceptionAllowsInsecureHTTPLoads
类型Boolean
并将其值设置为YES
。NSExceptionAllowsInsecureHTTPLoads
条目并单击“右移行”选项,使其成为上述条目的子项。NSExceptionAllowsInsecureHTTPLoads
条目旁边的“+”按钮并添加Allow Arbitrary Loads
类型Boolean
并将其值设置为YES
注意:它最终应该类似于下图所示
归档时间: |
|
查看次数: |
235977 次 |
最近记录: |