2012-05-21 5 views
0

현재 잘 작동하는 내 iPhone 앱의 URL에서 이미지를로드하고 있습니다. 하지만 이제 보호 된 URL에 액세스하려고합니다. 자격 증명 (사용자 이름/암호)을 사용하여 URL에 액세스하는 방법을 알려주는 코드 조각으로 안내해주십시오.iOS에서 인증이 필요한 URL에 액세스하는 방법

내 애플 URL에서 이미지를로드되는 간단한 코드는이 두 콜백

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{ 
    // IF to handle NTLM authentication. 
    if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodNTLM]) 
     return YES; 

    // Mostly sent by IIS. 
    if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) 
     return YES; 

    return NO; 
} 


- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge 
{ 
    if ([[[challenge protectionSpace] authenticationMethod] isEqual:NSURLAuthenticationMethodNTLM]) 
    { 
     [[challenge sender] useCredential:[NSURLCredential 
               credentialWithUser:Username 
               password:password 
               persistence:NSURLCredentialPersistenceNone] 
        forAuthenticationChallenge:challenge]; 
    } 
    else if ([[[challenge protectionSpace] authenticationMethod] isEqual:NSURLAuthenticationMethodServerTrust]) 
    { 
      [[challenge sender] useCredential:[NSURLCredential 
               credentialWithUser:Username 
               password:password 
               persistence:NSURLCredentialPersistenceNone] 
        forAuthenticationChallenge:challenge]; 
    } 
    else 
    { 
     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
    } 
} 

답변

0

당신이 만드는 ASIHTTPRequest 같은 일부 타사 라이브러리를 사용할 수 있습니다 들어

NSURL *url = [NSURL URLWithString: @"http://www.somedirectory.com"]; 
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
    [Pic setImage:image]; 
0

봐 아래와 같습니다 아주 쉽습니다.

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setUsername:@"username"]; 
[request setPassword:@"password"]; 

또는 당신은 NSURLConnection 클래스를 사용할 수 있지만, 인증 구현은 조금 까다 롭습니다.

+0

나는 이것을 시도했지만 didReceiveAuthenticationChallenge 콜을 다시 호출하지 않는다. – Sublime

관련 문제