【发布时间】:2015-04-14 18:58:58
【问题描述】:
我在这里有这段代码可以让我登录到一个 URL,我的问题是当我尝试删除它不起作用的凭据时,但我在网上找到了一个解决方案:
If you're using NSURLSession, invalidate the session and create a new one, e.g.:
[self.session invalidateAndCancel];
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
This will clear session-scoped credentials.
所以我的问题是,我可以将 NSURLSession 与 AFNetworking 一起使用吗:
NSURL *url = [NSURL URLWithString:kIP];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
NSURLCredential *credential = [NSURLCredential
credentialWithUser:user
password:password
persistence:NSURLCredentialPersistenceForSession];
[operation setCredential:credential];
[[NSOperationQueue mainQueue] addOperation:operation];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completionHandler) {
completionHandler(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (completionHandler) {
completionHandler(nil, error);
}
}];
[operation start];
更新
我开始做这个了:
NSURL *url = [NSURL URLWithString:kIP];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (completionHandler) {
completionHandler(responseObject, nil);
}
}] resume];
如何添加用户名和密码?
【问题讨论】:
标签: ios objective-c afnetworking nsurlsession