ついにできた!
あなたができることはこれです:
通常どおりにリクエストを開始しますUIWebView
。次に、-in- NOwebView:shouldStartLoadWithRequest
と応答し、代わりに同じ要求でNSURLConnectionを開始します。
を使用NSURLConnection
すると、自己署名サーバーと通信できます。これは、では使用できない追加のデリゲートメソッドを介して認証を制御できるためUIWebView
です。したがって、を使用connection:didReceiveAuthenticationChallenge
して、自己署名サーバーに対して認証できます。
次に、でconnection:didReceiveData
、リクエストをキャンセルし、 -をNSURLConnection
使用して同じリクエストを再開しUIWebView
ます。これは、サーバー認証をすでに通過しているため、これで機能します:)
以下に、関連するコードスニペットを示します。
注:表示されるインスタンス変数は次のタイプです。
UIWebView *_web
NSURLConnection *_urlConnection
NSURLRequest *_request
(私の場合のようにインスタンス変数を使用します。_request
これは多くのログイン詳細を含むPOSTですが、必要に応じて、メソッドへの引数として渡されたリクエストを使用するように変更できます。)
#pragma mark - Webview delegate
// Note: This method is particularly important. As the server is using a self signed certificate,
// we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the
// request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods
// which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete
// the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated);
if (!_authenticated) {
_authenticated = NO;
_urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[_urlConnection start];
return NO;
}
return YES;
}
#pragma mark - NURLConnection delegate
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection");
if ([challenge previousFailureCount] == 0)
{
_authenticated = YES;
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection");
// remake a webview call now that authentication has passed ok.
_authenticated = YES;
[_web loadRequest:_request];
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
}
// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
これが私が抱えていたのと同じ問題を抱えている他の人たちに役立つことを願っています!