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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值