天天看點

ios https

一、ASI請求,http換成https

    self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:getAccessTokenUrl]];

    [self.request setValidatesSecureCertificate:NO];//設定為NO

    //[self.request setSslSecurityLevel:(CFStringRef*)kCFStreamSocketSecurityLevelTLSv1];//設定安全等級,預設不用設定

二、WebView,http換成https

- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

    //加入以下代碼

    if (!_authenticated && [requestUrl hasPrefix:@"https://"]) {

        _authenticated =NO;

        _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        _request = request;

        [_urlConnection start];

        return YES;

    }

}

//修改以下三個方法

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{

    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 {

    // remake a webview call now that authentication has passed ok.

    _authenticated = YES;

    [self.webView loadRequest:_request]; //  self.webView替換成自己的webview

    // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)

    [_urlConnection cancel];

    [self refreshWithMJComplete];

    [self stopLoading];

}

// 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];

}