gpt4 book ai didi

ios - 无法加载资源,因为 App Transport Security 策略要求使用安全连接

转载 作者:IT王子 更新时间:2023-10-29 07:24:25 26 4
gpt4 key购买 nike

当我将 Xcode 更新到 7.0 或 iOS 9.0 时,我遇到了问题。不知何故,它开始给我标题错误

"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection"

网络服务方法:

-(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 服务方法时,它开始出现如下问题。我得到的记录错误是:

Connection failed: Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7fada0f31880 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=MyServiceURL, NSErrorFailingURLKey=MyServiceURL, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}

我已经尝试了以下问题和答案,但没有得到任何结果,是否有任何先进的想法可以消除该服务调用错误?

  1. The resource could not be loaded is ios9
  2. App Transport Security Xcode 7 beta 6
  3. https://stackoverflow.com/a/32609970

最佳答案

我已经通过在 info.plist 中添加一些键来解决它。我遵循的步骤是:

  1. 打开我的项目目标的 info.plist 文件

  2. 添加了一个名为 NSAppTransportSecurity 的 Key 作为 Dictionary

  3. 添加一个名为 NSAllowsArbitraryLoads 的子项作为 Boolean 并将其值设置为 YES,如下图所示。

enter image description here

清理项目,现在一切都像以前一样正常运行。

引用链接: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>

关于ios - 无法加载资源,因为 App Transport Security 策略要求使用安全连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32631184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com