对于我是否以正确的方式处理以下内容,我真的很感激。
一位朋友问我是否可以帮助他为他的初创公司制作一个应用程序。他们有一个运行某种风格的 asp.net 应用程序的服务器。
他想要一个允许用户登录和管理其帐户的 iOS 应用程序。
我正在检查 NSURLSession 和 NSURLSessionDataTask。
这是我必须做到的:
NSURL *url = [NSURL URLWithString:@"https://myfriendssite.com/Account/Login"];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"reponse: %@",response);
if(error == nil)
{
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
NSLog(@"code: %d", httpResp.statusCode);
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[webView loadHTMLString:text baseURL:url];
NSLog(@"Data: %@",text);
}
else
{
NSLog(@"Error: %@", error);
}
}];
[dataTask resume];
}
当我使用 [task resume] 完成任务时,我会收到该任务的“didReceiveChallenge”。那时我回应:
NSURLCredentialPersistence persistence = NSURLCredentialPersistenceNone;
NSURLCredential *credential = [NSURLCredential credentialWithUser:[usernameField text] password:[passworField text] persistence:persistence];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
但是我需要对响应返回的 cookie 做些什么吗?
"Set-Cookie" = "__RequestVerificationToken=cIxrC6dJnHyD6pIe-zgkx_Hr0CbsRZ7_dbQfsSIWsFAtVXjV_-o4d_2GycwhY0E-JjyA8R3iNsTreVOIcTNWQvxBenlEMLe8RJ5vz6JcyRSjLNn7H1Uklu0MD-afm08RVe7L8m4DzvtJv5rTAAie-w2; path=/; HttpOnly";
我在这里很不适应,所以我很感激任何指点!
干杯!
亚当