2014-07-15 23 views
0

我正在製作一個iOS應用程序,允許2個用戶互相交互。當第一個用戶使用UIWebView導航到他的應用程序上的URL時,我會將此URL發送給第二個用戶。第二個用戶收到此鏈接並使用UIWebView加載它。我如何檢測UIWebview在iOS中加載需要身份驗證的url

一切都很好,直到一個人進入私人區域。例如,第一位用戶訪問www.google.com並登錄他的Gmail帳戶時。現在我捕獲這個鏈接併發送給第二個用戶,他嘗試加載它,但是這個失敗了,因爲它是第一個用戶的私人區域。

第二位用戶的UIWebView如何知道接收到的url是在私人區域還是必須通過身份驗證才能訪問?

這裏一些例證代碼:

在第一用戶,當導航像www.google.com的網址。我稱這個功能爲

- (void)loadRequestUrl:(NSString *)url { 
     NSLog(@"load request"); 

     //Load request 
     self.websiteUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:websiteUrl]]; 
     [wbWebview loadRequest:request]; 

     //Send url to partner 
     //I'm using GCDAsyncSocket to send this url string to our server 
     //And server will send it to my current partner 

     //Just example code for sending 
     [asyncSocket send:url]; 
} 

//在第二位用戶。我從GCDAsyncSocket的代理收到了url字符串。在此委託我調用這個函數

- (void)receiveUrl:(NSString *)url { 

     //Just Load request 

     self.websiteUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

     NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:websiteUrl]]; 

     [wbWebview loadRequest:request]; 

} 

這裏說的第一個用戶,他登錄自己的谷歌賬戶 https://accounts.google.com.vn/accounts/SetSID?ssdc=1&sidt=ALWU2cscH%252BVqOOjXXIRKgtz4Q8qmIcJ5lE0dy7xh7MISa%252Bw75BNSeOrF3cO91IED8Cy6PfREuDjuXLzdOMEPaaP0p6XZpCzJFQi4w2xAZa9VKubLQ5xk5%252FF%252FOj8KR0f6e5PSav%252Fww0mKEAuPoI0Dtnve600Pj6PERFtvlH3kbt2Y0hk4KEBpn6nk7zAXUdt2wc%252FaHK0%252FufzyfIMI2hkLpCFu1W1XaOIS3zwuGttA5tXjyLb3AeBLmPgfBbsd7hwZWp7IRVJGglde8gAJ%252F%252BmIhQD4eMQa1s7LD8tnuoagx%252BmRzQ4EGqtcAlE%252FGE3e8b1itkh2HXZQZYB612X1NpcPgta1XbgO7IHd0g7HsDEnsqodhHtr9F7vGl4fO%252BCcHFYaHjH3dT2mCjnOwBn%252Bbh0%252FykYpxqbx2W8K%252BHcZp3B4KI166qCPCZvgnvq7QACPsPuWGVrll4Nw2yLK%252FE2bdmFVILfgIpVbY9SheA%253D%253D&continue=http%253A%252F%252Fwww.google.com.vn%252F%253Fgfe_rd%253Dcr%2526ei%253Dgf7EU-TLL-zV8ge_rYCIAw

第二用戶接收之後URL的例子,但不能打開。我永遠不能捕捉到錯誤的任何代表

重要性:我並不需要強制第二個用戶打開它,我只是想捕獲事件告訴第二個用戶是第一個用戶進入私人區

+0

我們展示你的代碼。 – Neeku

回答

3

要在uiwebview中接收和處理認證挑戰,這裏是代碼。

- (void)viewDidLoad 
{ 

// Load the web view with your url 
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL  URLWithString:@"yoururl"]]]; 
    isauth = NO; 
} 

現在爲代表。在將新請求加載到webview之前調用這個函數。

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

    if (!isauth) { 
    isauth = NO; 
    [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 
    return NO; 
    } 
    return YES; 
} 


//in nsurlconnections delegate. This one deals with the authentication challenge. this time set isauth to YES 

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

    if ([challenge previousFailureCount] == 0) { 
    isauth = YES; 
    [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge]; 
    } 
    else 
    [[challenge sender] cancelAuthenticationChallenge:challenge]; 
} 

// if the authentication is successfully handled than you will get data in this method in which you can reload web view with same request again. 

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

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yoururl"]]; 

    [webView loadRequest:urlRequest]; 
} 

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection; 
{ 
    return NO; 
} 

希望它有幫助。

謝謝。

+0

感謝@Sandip,你真的想出辦法解決我的問題,我會盡力知道它是否有效。但我認爲這引發了另外兩個問題。首先,每個url都會被加載2次,一次用於NSURLConnection,另一次用於UIWebview,這意味着幾乎不需要認證就可以打開它,我想。其次,我如何知道@「用戶名」和@「密碼」來填寫函數[NSURLCredential credentialWithUser:password:persistence:forAuthenticationChallenge]上的參數 –

+0

我們無法直接使用UIwebview連接到我們的服務器,因此我們需要提出單獨的請求檢查身份驗證,如何通過在接收響應方法中取消請求來減少流量。 – Sandip

3

要獲取用戶名和密碼,有多種方式可以做到這一點,您可以創建自己的機制相同。一種簡單的方法是。

-

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

NSLog(@"Need Authentication"); 

UIAlertView *webLogin = [[UIAlertView alloc] initWithTitle:@"Auth" 
                message:@"Enter User and Password" 
                delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"OK" 
                , nil]; 

self. challenge = challenge; 
webLogin.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
[webLogin show]; 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

user = [[alertView textFieldAtIndex:0]text]; 
pass = [[alertView textFieldAtIndex:1]text]; 

if (buttonIndex == [alertView cancelButtonIndex]) { 

    [self dismissModalViewControllerAnimated:YES]; 
} 
else if (buttonIndex != [alertView cancelButtonIndex]) { 

    [self handleChallenge:self.challenge withUser:user password:pass]; 
} 


} 

- (void)handleChallenge:(NSURLAuthenticationChallenge *)aChallenge  withUser:(NSString *)userName password:(NSString *)password { 

NSURLCredential *credential = [[NSURLCredential alloc] 
           initWithUser:userName password:password 
           persistence:NSURLCredentialPersistenceForSession]; 
[[aChallenge sender] useCredential:credential forAuthenticationChallenge:aChallenge]; 

} 
+0

感謝@Sandip,值得嘗試您的解決方案,我會嘗試 –