iPhoneでダイジェスト認証
NSURL *URL = [NSURL URLWithString:@"ダイジェスト認証がかかったアドレス"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
以上で開始し、
-(void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"ユーザー名" password:@"パス" persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
で一旦受ける。
(※http://stackoverflow.com/questions/589377/in-the-iphone-sdk-how-would-you-programatically-pass-a-username-password-to-a
を参考にさせていただきました。)
上記でユーザー名、パスが正しく送り返されれば、その他のdelegate、
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
などに情報が来る。
