HTTPSリクエストする場合:
[1]証明書が信頼され、何もしません
[2]証明書が信頼されていない、自己署名されています
(1)ATS特性を無効にし、コンフィギュレーションファイルを変更します
(2)デジタル証明書と信頼をインストール
NSURLSessionのサンプルコードは次のとおりです。
#importを" ViewController.h " のViewController @interface() <NSURLSessionDataDelegate> @end @implementationのViewController - (ボイド)touchesBegan:(NSSet <UITouch *> *)はwithEventに触れる:(たUIEvent *)イベント{ NSURL * URL = [NSURL URLWithString :@ " https://kyfw.12306.cn/otn/ " ]; NSURLSession *セッション= [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] デリゲート:自己delegateQueue:[NSOperationQueue mainQueue]]; [[セッションdataTaskWithURL:URLのcompletionHandler:^(のNSData * _Nullableデータ、NSURLResponse * _Nullable応答、NSError * _Nullableエラー){ のNSLog(@ " %@ " 、[[NSStringのALLOC] initWithData:データ・エンコーディング:NSUTF8StringEncoding])。 }] 履歴書]; } - (ボイド)URLSession:(NSURLSession *)セッションdidReceiveChallenge:(NSURLAuthenticationChallenge *)チャレンジcompletionHandler :( ボイド(^)(NSURLSessionAuthChallengeDisposition、NSURLCredential * _Nullable))completionHandler { のNSLog(@ " %の@ " 、challenge.protectionSpace)。 / * NSURLSessionAuthChallengeUseCredential = 0は、nilにも指定された資格情報、使用 NSURLSessionAuthChallengePerformDefaultHandling = 1、挑戦を扱うデフォルト-このデリゲートによって実装されていなかったかのように。資格パラメータは無視されます。 NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2、全体の要求がキャンセルされます。資格パラメータは無視されます。 NSURLSessionAuthChallengeRejectProtectionSpace = 3は、この課題は拒否され、次の認証保護空間を試行すべきです。資格パラメータは無視されます。 * / NSURLCredential * credentail = [[NSURLCredential ALLOC] initWithTrust:challenge.protectionSpace.serverTrust]。 completionHandler(NSURLSessionAuthChallengeUseCredential、credentail)。 }
AFN設定