2014-01-09 1 views
2

웹에서 uiwebview에서 기본 http 인증을 수행하는 방법을 살펴 보았습니다. 나는 그것을 다 해보았고 내가하는 일과 상관없이 일관성없는 결과를 얻고있다. 때로는 인증이 작동하고 그렇지 않은 경우도 있습니다. 그것은 벽 위로 나를 몰고있다. 그리고 나는 약간의 도움을 좋아할 것이다!UIWebView 및 HTTP 기본 인증 관련 문제

다음은 관련 코드입니다. 나는 항상 챌린지에 도전하지만 webview가 항상로드되지는 않습니다 (기본적으로 webViewDidFinishLoad를 기다리는 시간이 있습니다 - 오류 없음). 어떤 아이디어?

/* 
Load a webview 
*/ 
- (void)loadWebView 
{ 
    NSLog(@"loading webview"); 
    if(_webView == NULL){ 
     // UIWebView initialization 
     _webView = [[UIWebView alloc] init]; 
     _webView.hidden = TRUE; 
    } 

    // set up webview request 
    NSURL *url = [NSURL URLWithString:BASE_URL]; 
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestReloadIgnoringCacheData 
                 timeoutInterval:30]; 

    // load the request 
    [_webView setDelegate:self]; 
    [_webView loadRequest:request]; 

    // add ourselves as delegate to connection events so we can authenticate 
    (void)[NSURLConnection connectionWithRequest:request delegate:self]; 
} 


/** 
authenticates against HTTP basic authorization 
*/ 
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    //receive a authenticate and challenge with the user credential 
    NSLog(@"got auth challenge. Authenticating..."); 
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic] && 
     [challenge previousFailureCount] == 0) 
    { 
     NSURLCredential *credentail = [NSURLCredential 
             credentialWithUser:AUTH_USERNAME 
             password:AUTH_PASS 
             persistence:NSURLCredentialPersistenceForSession]; 


     [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Message" message:@"Invalid credentails" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

/** 
store auth credentials 
*/ 
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection; 
{ 
    NSLog(@"using credential storage"); 
    return YES; 
} 

답변

1

대신 BASE_URL의 시도하십시오 요청 헤더 대신 URL에 인증을 전달할 수 있습니다

NSString* urlString = [NSString stringWithFormat:@"%@//%@:%@@%@",@"http:",userName,password,myUrl] 
+0

영업 이익은 특별히 아니 작업 주위에 기본 인증을 요구한다. – RajV