2017年2月7日火曜日

iOS Objective-C SSL証明書エラーを回避する方法

NSURLRequestを使っているとき、SSLで自己証明書(オレオレ証明書)をつかっているサーバのURLにアクセスするとエラーになります。
これを回避する方法です。
開発時などに役に立つTIPSだと思います。

エラーの例
2017-02-07 10:49:25.250 App[46178:1264401] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
2017-02-07 10:49:25.253 App[46178:1264332] FADHttp failed -1200: https://mydomain/ An SSL error has occurred and a secure connection to the server cannot be made.

Info.plistにAppTransportSecurityを設定して、NSURLRequestをオーバーライドするコードを追加します。

参考資料
[iOS 9] iOS 9 で追加された App Transport Security の概要 | Developers.IO http://dev.classmethod.jp/smartphone/iphone/ios-9-intro-ats/
Working with Apple’s App Transport Security
http://www.neglectedpotential.com/2015/06/working-with-apples-application-transport-security/
Aplle / Cocoa Keys / Information Property List Key Reference
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40016240