Ios 无法加载资源,因为应用程序传输安全策略要求使用安全连接
当我将Xcode更新到7.0或iOS 9.0时,我面临着这个问题。 不知怎的,它开始给我标题错误 “由于应用程序传输安全性问题,无法加载资源 策略要求使用安全连接“ Web服务方法:Ios 无法加载资源,因为应用程序传输安全策略要求使用安全连接,ios,nsurlconnection,nsurlsession,ios9,xcode7,Ios,Nsurlconnection,Nsurlsession,Ios9,Xcode7,当我将Xcode更新到7.0或iOS 9.0时,我面临着这个问题。 不知怎的,它开始给我标题错误 “由于应用程序传输安全性问题,无法加载资源 策略要求使用安全连接“ Web服务方法: -(void)ServiceCall:(NSString*)ServiceName :(NSString *)DataString { NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessio
-(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服务方法时,它开始出现如下问题。我收到的记录错误是:
连接失败:错误域=nsurErrorDomain代码=-1022“错误”
无法加载资源,因为应用程序传输安全策略无效
需要使用安全连接。”
UserInfo={NSUnderlyingError=0x7fada0f31880{Error
域=KCFerrorDomainCFN网络代码=-1022“(空)”},
NSErrorFailingURLStringKey=MyServiceURL,
NSErrorFailingURLKey=MyServiceURL,
NSLocalizedDescription=无法加载资源,因为
应用程序传输安全策略要求使用安全的
连接。}
我尝试了以下问题和答案,但没有得到任何结果,是否有任何提前的想法,我可以如何删除该服务呼叫错误
我通过在info.plist中添加一些键解决了这个问题。 我遵循的步骤是:
info.plist
文件
NSAppTransportSecurity
的密钥添加为字典
NSAllowsArbitraryLoads
的子键作为Boolean
,并将其值设置为YES
,如下图所示
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>
NSAppTransportSecurity
NSAllowsArbitraryLoads
NSExceptionDomains
yourdomain.com
n包括多个域
N第三方例外要求转发保密
您只需要在URL中使用HTTPS而不是HTTP,它就可以工作了iOS 9(可能)强制开发者只使用应用程序传输安全性。我在某个地方偶然听到了这个消息,所以我自己也不知道这是不是真的。但我对此表示怀疑,并得出以下结论:
在iOS 9上运行的应用程序(可能)将不再连接到没有SSL的Meteor服务器。
这意味着运行meteor运行的ios或meteor运行的ios设备将(可能?)不再工作
在应用程序的info.plist中,
NSAppTransportSecurity[Dictionary]
需要有一个密钥nsallowsraboraryloads[Boolean]
才能设置为YES
,或者Meteor需要很快为其本地主机服务器使用https
。传输安全性在iOS 9.0或更高版本中提供,在OSXV10.11及更高版本中
因此,默认情况下,仅允许在应用程序中使用https呼叫。要关闭应用程序传输安全,请在info.plist文件中添加以下行
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
有关更多信息:
我已解算为plist文件
添加NSAppTransportSecurity:字典
将名为“NSAllowsArbitraryLoads”的子键添加为布尔值:YES
请注意,在项目的info.plist
中使用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>
NSAppTransportSecurity
NSExceptionDomains
domain.com
N异常低安全Http负载
n包括多个域
编辑后清理并生成项目 在Xcode 7.1之后的版本中(swift 2.0)
对于iOS 10.x和Swift 3.x[也支持以下版本]只需在“info.plist”中添加以下行
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
我通过结合前面提到的许多选项设法解决了这个问题。我将包括一份清单,列出我要做的所有事情,以使这项工作顺利进行
简言之:
将我的手表扩展名(不是我的手表应用程序)的NSAllowsArbitraryLoads
设置为true
确保我使用的是https
,而不是http
第一步:
首先也是最明显的是,我必须在手表扩展名的info.plist
中添加一个NSAppTransportSecurity
键作为字典,并将名为NSAllowsArbitraryLoads
的子键作为布尔值设置为true。仅在watch扩展中设置,而不是在watch应用程序的plist中设置。但是请注意,这允许所有连接,并且可能不安全
或
NSAppTransportSecurity
NSAllowsArbitraryLoads
第二步:
然后我必须确保我试图加载的url是https
,而不仅仅是http
。对于我仍然使用http的任何URL:
Swift:
让newURLString=oldURLString.stringByReplacingOccurrencesOfString(“http”,带字符串:“https”)
Obj-C:
NSString*newURLString=[oldURLString stringByReplacingOccurrencesOfString:@“http”和字符串:@“https”]代码>对于使用一年签名证书而不是“NSAllowsArbitraryLoads”选项的自托管解析服务器,我已经解决了这个问题
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<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>
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
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
<!--By Passing-->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>your.domain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>1.0</string>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
<!--End Passing-->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>