1

我收到警告:

Incompatible pointer types assigning to 'AFHTTPSessionManager *' from 'NSURLSessionDataTask * _Nullable'

'POST:parameters:success:failure:' is depreceated 

尝试使用 AFNetworking 3.0 发出请求时。我想知道使用 AFNewtworking 3.0 为使用 iOS 7.0 或更高版本的应用程序发出发布请求的正确方法是什么。这是我的代码:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager setResponseSerializer:[SignupResponseSerializer serializer]];

    __weak typeof(self)weakSelf = self;

    NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kRegisterURL];

    NSData *plainData = UIImagePNGRepresentation(self.profilePicture);
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];

    if (self.firstNameField.text && self.lastNameField.text && self.passwordField.text && self.emailField.text) {
        NSDictionary *params = @{ @"first_name": self.firstNameField.text, @"last_name": self.lastNameField.text, @"password": self.passwordField.text, @"email": self.emailField.text, @"avatar_data": base64String};

        self.sessionManager = [manager POST:urlString parameters:params success:^(NSURLSessionTask *task, id responseObject) {
            NSDictionary *JSON = [responseObject objectAtIndex:0];
            NSString *token = JSON[@"auth_token"];
            NSString *avatarURL = JSON[@"avatar_url"];


            weakSelf.credentialStore.avatarURL = avatarURL;
            weakSelf.credentialStore.authToken = token;
            weakSelf.credentialStore.firstName = self.firstNameField.text;
            weakSelf.credentialStore.lastName = self.lastNameField.text;
            weakSelf.credentialStore.email = self.emailField.text;
            weakSelf.credentialStore.password = self.passwordField.text;

            [SVProgressHUD dismiss];
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(NSURLSessionTask *task, NSError *error) {
//            if (task.isCancelled) {
//                return;
//            }
            [SVProgressHUD showErrorWithStatus:@"Login Failed"];
            NSLog(@"%@", error);
        }];
    }
4

1 回答 1

1

在 AFNetworking 3.0 中,POST 方法返回一个NSURLSessionDataTask包含您请求的对象。此外,现在它有一个progress参数,它是一个调用来跟踪请求进度的块。
所以我建议你从这个改变方法调用:

self.sessionManager = [manager POST:urlString parameters:params success:^(NSURLSessionTask *task, id responseObject) {
...

对此:

//If you want a reference to the manager you can initialize the sessionManager var like this but it's not necessary
self.sessionManager = manager;
[manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        //progress block implementation
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //success block implementation
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //failure block implementation
    }];
于 2016-04-05T14:13:29.707 回答