objective-c 如何使用 AFNetworking 使用 NSURLSession

Posted

技术标签:

【中文标题】objective-c 如何使用 AFNetworking 使用 NSURLSession【英文标题】:objective-c how to us NSURLSession with AFNetworking 【发布时间】:2015-04-14 18:58:58 【问题描述】:

我在这里有这段代码可以让我登录到一个 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];

如何添加用户名和密码?

【问题讨论】:

【参考方案1】:

尝试使用 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;

【讨论】:

我更新了我的问题,我只需要知道如何添加用户名和密码

以上是关于objective-c 如何使用 AFNetworking 使用 NSURLSession的主要内容,如果未能解决你的问题,请参考以下文章

如何在c++中,调用objective-c

Objective-C 观察者 - 如何在 Swift 中使用

Objective-c:如何使用自动布局向 UIView 添加边框 [关闭]

如何在 Swift 中使用 Objective-C #define

如何在objective-c中使用didSelectRowAtIndexPath

如何使用 Swift 从 Objective-C 启动实例类型