2016-06-24 6 views
0

신뢰할 수없는 SSL 인증서로 신속하게 HTTPS 사이트에 로그인하는 데 어려움을 겪고 있습니다. NSURLConnectionDelegate 메서드를 사용하여 내 UIWebView 안에 페이지를 볼 수 있습니다. 웹 브라우저를 사용하여이 웹 사이트에 자동 로그인 할 수 있습니다 (예 : URL - https://mywebsite.com:8003/home.html?session_id=sessionVariable).신뢰할 수없는 SSL 인증서가있는 HTTPS 사이트의 자동 로그인 문제가 신속하게

내 대리인 기능은
let websiteURL = "https://mywebsite.com:8003/home.html?session_id=\(Session_Id)" 

     print(websiteURL) 
     let url = NSURL (string: websiteURL) 
     let urlRequest = NSURLRequest(URL: url!) 
     let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)! 
     self.webviewInstance.loadRequest(urlRequest) 

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{ 
    print("canAuthenticateAgainstProtectionSpace method Returning True") 
    return true 
} 


func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){ 

    print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)") 

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { 
     print("send credential Server Trust") 
     let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!) 
     challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge) 

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{ 
     print("send credential HTTP Basic") 
     let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession) 
     challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge) 

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{ 
     print("send credential NTLM") 

    } else{ 
     challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge) 
    } 
} 

내가 키 NSAppTransportSecurity 내 PLIST 파일의 변화를 aded 한 NSAllowsArbitraryLoads 등 :하지만있는 UIWebView에서

다음

가있는 viewDidLoad 안에 내 코드입니다

이 서버와 JSON 호출을 상호 작용할 수 있습니다. 그러나 webView에서 자동 로그인이 작동하지 않습니다. 상담하십시오.

답변

0

당신이 직접 만든 요청을 제외하고는 위임자 메소드가 호출되지 않으며 웹 뷰와 일반 앱 요청간에 쿠키가 공유되지 않는다고 생각합니다.

가장 좋은 방법은 웹보기 및 다른 곳에서 http/https 요청을 가로 채고 사용자 정의 헤더를 추가하므로 (프로토콜 클래스가 다시 볼 때 재전송 요청을 인식 할 수 있도록) 사용자 정의 NSURLProtocol을 작성한 다음 NSURLConnection 객체를 사용하여 요청을 다시 보냅니다.

자세한 내용은 Using a custom URL scheme with UIWebView