2012-03-07 43 views
-1

和我之前的其他人一樣,我有一些我想在我的應用中顯示的網頁內容。我的網站是在Microsoft Visual Web Developer中開發的基於ASP.NET的網站,它使用Ajax Toolkit和其他漂亮的插件。我可以在iPhone的移動Safari瀏覽器中打開該網站,甚至可以將一些適合移動設備的網頁放在一起。UIWebKit無效率

我(或更準確地說,我的老闆)想要一個iPhone應用程序,它存儲用於訪問我的網站的憑證,並在打開應用程序時自動發送它們。這裏的問題:

我的網站不加載在UIWebView。它不會遇到任何委託方法(完成加載,未能加載等)。它只是啓動連接請求並坐在那裏。該網站在我用於測試的同一設備上的Safari上工作,所以我認爲這個問題與UIWebKit沒有在Safari中構建的某些工具有關。

什麼(特別是)導致我的網站無法加載在UIWebKit,當它在Safari中正確加載?

我會後我已經設置了捕捉任何生命跡象,從我的電話的方法來

[myWebView loadRequest:myRequest]; 

無下列方法被調用時,我跑我的應用程序,並達到上述的loadRequest調用。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    NSLog(@"WillSendForAuthenticationChallenge"); 
} 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
    NSLog(@"Webview failed to load."); 
    if (error.code == NSURLErrorCancelled) return; // this is Error -999, called when the user forcibly ends the connection by starting a new connection. 
    UIAlertView *failure = [[UIAlertView alloc] initWithTitle:@"Failed" message:error.localizedDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
    [failure show]; 
    [spinner stopAnimating]; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSLog(@"Loading request: %@",request.URL); 
    return YES; 
} 

- (void)webViewDidFinishLoad:(UIWebView *)webview 
{ 
    NSLog(@"Webview finished loading."); 
    [spinner stopAnimating]; 
} 

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    NSLog(@"Can Authenticate: %@",[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]); 
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    NSLog(@"Received an authentication challenge."); 
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 
+0

這是什麼問題? – Caleb 2012-03-07 21:10:52

+0

對於任何人來幫助你這個,也許你可以告訴我們你當前如何通過證書到網站的情況下,它的工作(顯示一些代碼),以及你想如何使用UIWebKit(描述性)。 – Jim 2012-03-07 21:21:18

+0

你設置了webview代理嗎? – fbernardo 2012-03-08 18:40:30

回答

0

嗯,這是我如何解決它。 UIWebView從不調用任何NSURLConnection方法,並且它沒有任何內置的安全方法,所以當Web視圖即將加載頁面時,我首先將NSURLConnection發送到相同的地址,然後可以處理通過類似didReceiveAuthenticationChallenge的方法進行認證。變量「_sessionChecked」是一個簡單的BOOL,用於跟蹤是否已經爲該頁面發送了認證NSURLConnection。一旦有了,我可以簡單地將頁面加載到Web視圖中。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSLog(@"Loading request: %@",request.URL); 
    if (_sessionChecked) { 
     NSLog(@"Session already checked."); 
     return YES; 
    } 

    NSLog(@"Will check session."); 

    NSMutableURLRequest *newReq = request.mutableCopy; 

    [newReq setValue:USRAGNT forHTTPHeaderField:@"User-Agent"]; 

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:newReq delegate:self]; 
    if (conn == nil) { 
     NSLog(@"Cannot create connection."); 
    } 
    return NO; 
} 

當NSURLConnection從服務器獲取響應時調用didReceiveResponse方法。這個響應包含兩個重要的事情:連接是否成功通過NSURLRequest提供的憑證進行身份驗證,以及如果是,則指向哪個url。成功驗證後,我將新的url發送到要加載的Web視圖。

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"connection:didReceiveResponse"); 

    if ([response isKindOfClass:[NSHTTPURLResponse class]]) { 
     _sessionChecked = YES; 

     NSString *newUrl = @""; 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
     int status = [httpResponse statusCode]; 

     if (status == 401 || status == 403) { 
      NSLog(@"Not authenticated. http status code: %d", status); 
      newUrl = @""; 
      [myWebView stopLoading]; 
      [spinner stopAnimating]; 
      UIAlertView *failed = [[UIAlertView alloc] initWithTitle:@"Authentication Failed" message:@"You do not have any credentials stored, or your stored credentials are not valid." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
      [failed show]; 
      _sessionChecked = NO; 
     } 
     else { 
      NSLog(@"Authenticated. http status code: %d", status); 
      newUrl = [NSString stringWithFormat:@"%@", response.URL]; 
     } 

     // cancel the connection. we got what we want from the response, 
     // no need to download the response data. 
     [connection cancel]; 

     // start loading the new request in webView 
     NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:newUrl]]; 
     [myWebView loadRequest:req]; 
    } 
} 

非常感謝Ardal Ahmet的this how-to