이건 혹시, 테스트 서버에 접근 할 때, SSL 인증을 통과 시키고자 하시는 분들에게 도움을 드리고자 공유 합니다.
iOS 출시를 위한 서버로 접근은 이 코드를 적용 시켜서는 안됩니다.
먼저 , .plist에 예외 처리 설정을 추가 합니다.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
위와 같이 설정을 추가 하셨으면 아래와 같이 소스를 변경 시켜 보세요.
Swift 3.0에서는 아래 Step 7부분만 바꿔주시면 됩니다.
Finally got the answer as :
Step 1 >> import SafariServices
Step 2 >> Use NSURLConnectionDelegate with your ViewController i.e.
class ViewController:UIViewController, NSURLConnectionDelegate
Step 3 >> Override methods :
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
Step 4 >> GOTO viewDidLoad where to load your URL in the Webview, & make changes as :
let url = NSURL (string: URL)//where URL = https://203.xxx.xxx.xxx
let requestObj = NSURLRequest(URL: url!)
var request: NSURLRequest = NSURLRequest(URL: url!)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
YOUR_WEBVIEW.loadRequest(requestObj);
Step 5 >> GOTO webView with shouldStartLoadWithRequest & return true. If you return false, you are never-ever getting the results.
func webView(IciciWebView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool
{
//Do whatever you want to do.
return true
}
Step 6 >> Update the function :
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
{
print("In canAuthenticateAgainstProtectionSpace");
return true;
}
Step 7 >> Update the function :
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
{
print("In willSendRequestForAuthenticationChallenge..");
challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
{
print("In willSendRequestForAuthenticationChallenge..");
challenge.sender!.use(URLCredential(trust: challenge.protectionSpace.serverTrust!), for: challenge)
challenge.sender!.continueWithoutCredential(for: challenge)
}
출처 : https://stackoverflow.com/a/36442505
참고하세요.
'나의 플랫폼 > iOS' 카테고리의 다른 글
[iOS] UITableView의 footerView 변경 - height, background color (0) | 2017.06.28 |
---|---|
[iOS] Kingfisher 이미지 다운로드시, Authentication with NSURLCredential (0) | 2017.06.27 |
[iOS] 키보드 강제로 내리기 (0) | 2017.06.16 |
[iOS][Alamofire] NSURLSession/NSURLConnection HTTP load failed (2) | 2017.06.15 |
[iOS] Assets image stretching (0) | 2017.06.13 |