HTTP 인증을 통해 서버를 인증하는 경우 (위의 의견에 따르면) 나는 다르게 처리 할 것입니다. 사용자가 자신의 사용자 이름/암호를 입력하면 NSURLConnection
을 사용하여 모든 페이지 (가벼운 것)에 요청하십시오. 사용자 이름/암호가 올바르지 않으면 NSURLConnectionDelegate
메서드에서 오류를 처리하고 사용자에게 새 사용자 이름/암호를 묻는 메시지를 표시 할 수 있습니다.
위임 방법 대신 iOS 5 버전 NSURLConnection
으로도이 작업을 수행 할 수 있지만 문서는 좋지 않습니다.
첫 번째 요청이 성공하면 웹보기를로드합니다.
편집 : HTTP의 인증 확인을
을 성공하면 확인 할 수있는 NSURLConnection를 사용하는 추가 예를 들어, 여기에 간단한 예입니다. NSURLConnection
에 원하는 요청을로드하고 응답 데이터를로드하면 webView
에 응답 데이터를로드하여 새 요청을 시작함으로써이 기능을 향상시킬 수 있습니다. NSURLConnection
을 사용하는 방법에 대해 자세히 알아 보려면 URL 로딩 시스템 문서를 읽어보십시오.
- (void)performHTTPAuthRequest
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@test.starsightings.com", username, password]]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
// if the request succeeds, credentials must be good so load the webView
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// request completed successfully so username/password must be good
[self.webView loadRequest:theRequest];
}
// ignore the failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
// if we get an auth challenge, the credentials are bad so cancel the request and alert the user
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
// alert user that username/pwd is invalid
}
는 서버가 집행이 암호 요구 사항입니다 : 다음과 같이
webView
위임에는 사용자 자격 증명으로
webView
요청과 동일한 도메인에 대한 요청과NSURLConnection
을 시작? 그렇다면 앱 사용자에게 적절한 오류를 처리하고 표시 할 적절한 오류가 응답에 포함되어야합니다. – XJones아니요, 암호는 다음과 같이 서버에 전송됩니다. http : // username : [email protected] 그리고 Mac 웹 브라우저에 잘못된 사용자 이름/암호를 입력하면 다시 입력해야합니다. 이것은 iphone에서 작동하지 않으므로 '타이머'와 같은 것을 가지고 싶습니다. – user1110365