2011-06-10 93 views
7

我開始進行iPhone編程,並遇到很大的問題,無法解決。在UIWebView中加載HTTPS url

所以,我有一個UIWebview,我可以加載HTTP網址沒有問題:

NSString urlAdress; 
urlAdress = @"http://servername"; 
NSURL *url = [NSURL URLWithString:urlAdress]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
[webView loadRequest:requestObj]; 

它的工作,我的頁面加載在我的UIWebView,但是當我更換:

urlAdress = @"http://servername"; 

urlAdress = @"https://servername"; 

我有空白屏幕。

我讀了它的正常,但有沒有簡單的方法來在我的webview中加載https url?
我讀了ASIHTTPRequest,但我沒有達到實施它。

我只是想加載HTTPS URL

+0

會發生什麼事,如果你加載'的https:// servername'在Safari? – deanWombourne 2011-06-10 14:05:09

+0

在safari中我可以加載「http:// servername」和「https:// servername」 – Borneto 2011-06-10 14:07:55

+1

這是否帶有自簽名證書? – Jim 2011-06-10 16:53:03

回答

6

試試這個:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    return YES; 
} 


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
} 
+1

我只是將它添加到包含UIWebView的UIViewController?我不太確定該怎麼做。 – powerj1984 2012-08-31 17:35:11

+1

這兩個方法是[NSURLConnectionDelegate](https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html)協議的一部分,因此您可以將它們放入任何文件充當您NSURLConnection的代表。 – 2012-10-12 14:10:58

+0

我也在使用自己分配的證書,它與上面的實現一起工作。 – m4n1c 2013-04-03 06:01:32