繁体   English   中英

Objective-C如何通过AFNetworking使用NSURLSession

[英]objective-c how to us NSURLSession with AFNetworking

我在这里使用此代码登录到URL,我的问题是当我尝试删除不起作用的凭据时,但我在网上找到了解决方案:

http://www.ciiycode.com/0yHHmeXWgUqQ/using-nsurlcredentialpersistenceforsession-while-request-and-then-clearing-the-credentials-on-logout-still-persist-the-cerdentials

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];

如何为此添加用户名和密码?

尝试使用AFNSURLSession而不是AFHTTPRequestOperation。 在AFNSURLSession中,您可以为委托setSessionDidReceiveAuthenticationChallengeBlock提供一个块,您可以在其中提供必要的凭据。

从AFNSURLSession.h:

/**
 Sets a block to be executed when a connection level authentication challenge has occurred, as handled by the `NSURLSessionDelegate` method `URLSession:didReceiveChallenge:completionHandler:`.
 @param block A block object to be executed when a connection level authentication challenge has occurred. The block returns the disposition of the authentication challenge, and takes three arguments: the session, the authentication challenge, and a pointer to the credential that should be used to resolve the challenge.
 */
- (void)setSessionDidReceiveAuthenticationChallengeBlock:(NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential))block;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM