【问题标题】:objective-c how to us NSURLSession with AFNetworkingobjective-c 如何使用 AFNetworking 使用 NSURLSession
【发布时间】: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];

如何添加用户名和密码?

【问题讨论】:

    标签: ios objective-c afnetworking nsurlsession


    【解决方案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;
    

    【讨论】:

    • 我更新了我的问题,我只需要知道如何添加用户名和密码
    猜你喜欢
    • 2014-04-12
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多