URL(https://xxxx.xxxx.xx/MobileService/Service.svc/login:)への接続リクエスト方法は、
編集:
https接続で認証できないようです。http は正常に動作します。なぜなのかご存知ですか?
コード:
NSString *postData = @"{}";
NSData *requestData = [postData dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:allAppsURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connectionDict setObject:connection forKey:@"all"];
認証方法:
// If the application needs to authenthicate
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"TamTam" accessGroup:nil];
NSString *password = [keychainItem objectForKey:kSecValueData];
NSString *username = [keychainItem objectForKey:kSecAttrAccount];
username = [@"\\" stringByAppendingString:username];
NSURLCredential *newCredential = [[[NSURLCredential alloc] initWithUser:username password:password
persistence:NSURLCredentialPersistenceForSession] autorelease];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
}
else {
// pull from server is always two connections, all and sub. To prevent multiple messages from pull do not check sub
if(!(connection == [connectionDict objectForKey:@"sub"])){
[[self delegate] serverConnection:NO process:@"login"];
}
NSLog(@"previous authentication failure");
}
}