[ios] App Transport Security 정책에 보안 연결을 사용해야하므로 리소스를로드 할 수 없습니다

Xcode를 7.0 또는 iOS 9.0으로 업데이트 할 때 문제가 발생합니다. 어떻게 든 제목 오류가 발생하기 시작했습니다.

“앱 전송 보안 정책에 보안 연결을 사용해야하므로 리소스를로드 할 수 없습니다.”

웹 서비스 방법 :

-(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으로 업데이트하면 위의 웹 서비스 메소드를 호출 할 때 다음과 같은 문제가 발생하기 시작했습니다. 내가 얻는 로그 오류는 다음과 같습니다.

연결 실패 : 오류 Domain = NSURLErrorDomain 코드 = -1022 “App Transport Security 정책에 보안 연결을 사용해야하므로 리소스를로드 할 수 없습니다.” UserInfo = {NSUnderlyingError = 0x7fada0f31880 {오류 도메인 = kCFErrorDomainCFNetwork 코드 = -1022 “(null)”}, NSErrorFailingURLStringKey = MyServiceURL , NSErrorFailingURLKey = MyServiceURL , NSLocalizedDescription = 앱 전송 보안 정책에 보안을 사용해야하므로 리소스를로드 할 수 없습니다 연결.}

다음 질문과 답변을 시도했지만 결과가 나타나지 않습니다. 해당 서비스 호출 오류를 제거하는 방법에 대한 사전 아이디어가 있습니까?

  1. 리소스를로드 할 수 없습니다 : ios9
  2. 앱 전송 보안 Xcode 7 베타 6
  3. https://stackoverflow.com/a/32609970


답변

info.plist에 키를 추가하여 해결했습니다. 내가 따르는 단계는 다음과 같습니다.

  1. 내 프로젝트 대상의 info.plist파일을 열었습니다

  2. 라는 키 추가 NSAppTransportSecurityA와를 Dictionary.

  3. NSAllowsArbitraryLoadsas 라는 하위 키를 추가하고 다음 이미지와 같이 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>


답변

사용주의 NSAllowsArbitraryLoads = true프로젝트에서는 info.plist모든 서버에 대한 모든 연결이 불안정 할 수 있습니다. 안전하지 않은 연결을 통해 특정 도메인 에만 액세스 할 수있게하려면 다음을 시도하십시오.

여기에 이미지 설명을 입력하십시오

또는 소스 코드로 :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>domain.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

편집 후 프로젝트를 정리하고 빌드하십시오.


답변

전송 보안 은 iOS 9.0 이상 및 OS X v10.11 이상에서 제공됩니다.

따라서 기본적으로 https 호출 만 앱에서만 허용됩니다. App Transport Security를 ​​끄려면 info.plist 파일에 다음 줄을 추가 하십시오 …

<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>

추가 정보 :
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33


답변

iOS 10.x 및 Swift 3.x (버전도 지원됨)의 경우 ‘info.plist’에 다음 줄을 추가하십시오.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>


답변

스위프트 4에서는

-> Go Info.plist

-> 정보 속성 목록의 더하기 클릭

-> 앱 전송 보안 설정 을 사전으로 추가

-> 클릭 플러스 아이콘 앱 전송 보안 설정

-> 임의로드 허용 설정 YES

벨로우즈 이미지

여기에 이미지 설명을 입력하십시오


답변

plist 파일로 해결했습니다.

NSAppTransportSecurity : Dictionary를 추가하십시오.

“NSAllowsArbitraryLoads”라는 하위 키를 부울로 추가 : YES

여기에 이미지 설명을 입력하십시오


답변

App Transport Security 정책에 Swift 4.03에서 작동하는 보안 연결을 사용해야하므로 리소스를로드 할 수 없습니다.

pList.info를 소스 코드로 열고 붙여 넣기 :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>