{"id":1942,"date":"2016-10-06T11:54:57","date_gmt":"2016-10-06T03:54:57","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=1942"},"modified":"2016-10-06T12:02:04","modified_gmt":"2016-10-06T04:02:04","slug":"%e8%bd%89%e5%9c%a8%e4%bd%bf%e7%94%a8-nsurlsession-%e6%99%82%e5%85%81%e8%a8%b1%e4%bd%bf%e7%94%a8%e4%b8%8d%e5%ae%89%e5%85%a8%e7%9a%84-ssl-%e9%80%a3%e7%b7%9a","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=1942","title":{"rendered":"[\u8f49]\u5728\u4f7f\u7528 NSURLSession \u6642\u5141\u8a31\u4f7f\u7528\u4e0d\u5b89\u5168\u7684 SSL \u9023\u7dda"},"content":{"rendered":"<p>\u4f46\u5f88\u591a\u6642\u5019\u4f60\u5c31\u6703\u53bb\u505a\u90a3\u4e9b\u4f60\u4e0d\u8a72\u505a\u7684\u4e8b\u3002\u6bd4\u65b9\u8aaa\uff0c\u4f60\u8981\u5beb\u4e00\u500b iOS \u4e0a\u7684 Client\uff0c\u800c API Server \u4e5f\u9084\u5728\u958b\u767c\u4e2d\uff0c\u63a5\u4e0b\u4f86\u6240\u6709\u7684 API \u90fd\u6703\u4f7f\u7528 HTTPS\uff0c\u4f46\u662f\u9084\u6c92\u6709\u82b1\u9322\u53bb\u8cb7\u6191\u8b49\uff0c\u6240\u4ee5\u5c31\u5148\u96a8\u4fbf\u7522\u751f\u4e86\u4e00\u500b\u6771\u897f\u6a94\u8457\u5148\uff0c\u800c\u5982\u679c\u4f60\u4f7f\u7528 NSURLSession \u8a66\u5716\u5efa\u7acb\u4e0d\u5b89\u5168\u7684 HTTPS \u9023\u7dda\u7684\u8a71\uff0cNSURLSession \u5c31\u62d2\u7d55\u9023\u7dda\u4e26\u4e14\u56de\u50b3 Error \u7269\u4ef6\u3002\u4f46\u662f\u9019\u7a2e\u72c0\u6cc1\u4e0b\uff0c\u4f60\u9084\u662f\u8981\u5148\u60f3\u8fa6\u6cd5\u628a\u9023\u7dda\u5efa\u7acb\u8d77\u4f86\uff0c\u624d\u6709\u8fa6\u6cd5\u5728 Server \u9084\u5728\u958b\u767c\u4e2d\u7684\u72c0\u6cc1\u4e0b\u4e5f\u5beb\u9ede Client Side \u7684\u7a0b\u5f0f\u3002<\/p>\n<p><!--more-->\u9996\u5148\u6211\u5011\u5fc5\u9808\u8981\u5efa\u7acb\u81ea\u5df1\u7684 NSURLSession \u7269\u4ef6\uff0c\u9019\u6a23\u624d\u80fd\u5920\u5728\u5efa\u7acb\u7269\u4ef6\u7684\u6642\u5019\uff0c\u628a delegate \u7269\u4ef6\u50b3\u9032\u53bb\u3002NSURLSession \u7684 delegate \u662f\u4e00\u500b\u552f\u8b80\u7684\u5c6c\u6027\uff0c\u53ea\u80fd\u5728\u5efa\u7acb instance \u7684\u6642\u5019\u50b3\u905e\u9032\u53bb\uff1b\u6240\u4ee5\uff0c\u6211\u5011\u4e5f\u4e0d\u80fd\u6539\u8b8a [NSURLSession sharedSession] \u7684 delegate \u5c6c\u6027\u3002<\/p>\n<figure class=\"figure-code code\"><figcaption><\/figcaption><div class=\"highlight\">\n<p>[c]NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];<br \/>\nURLSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:operationQueue];[\/c]<\/p>\n<\/div>\n<\/figure>\n<p>\u63a5\u8457\u5be6\u4f5c\u4e00\u500b delegate method<\/p>\n<figure class=\"figure-code code\"><figcaption><\/figcaption><div class=\"highlight\">\n<p>[c]- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler<br \/>\n{<br \/>\n#warning This allows insecure SSL. We need to remove this from release builds.<br \/>\n NSURLProtectionSpace * protectionSpace = challenge.protectionSpace;<br \/>\n if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {<br \/>\n SecTrustRef serverTrust = protectionSpace.serverTrust;<br \/>\n completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust: serverTrust]);<br \/>\n }<br \/>\n else {<br \/>\n completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);<br \/>\n }<br \/>\n}[\/c]<\/p>\n<\/div>\n<\/figure>\n<p>\u5343\u842c\u8a18\u5f97\u5728 Release \u8edf\u9ad4\u7684\u6642\u5019\u628a\u9019\u6bb5\u62ff\u6389\u3002<\/p>\n<p>\u8cc7\u6599\u4f86\u6e90\uff1ahttp:\/\/api.logdown.com\/posts\/168699\/using-nsurlsession-allows-the-use-of-less-secure-ssl-online<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f46\u5f88\u591a\u6642\u5019\u4f60\u5c31\u6703\u53bb\u505a\u90a3\u4e9b\u4f60\u4e0d\u8a72\u505a\u7684\u4e8b\u3002\u6bd4\u65b9\u8aaa\uff0c\u4f60\u8981\u5beb\u4e00\u500b iOS \u4e0a\u7684 Client\uff0c\u800c API Server<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=1942\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1942","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1942"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1942\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}