重用NSURLUploadTask

时间:2015-05-04 11:10:57

标签: ios nsurlsession nsurlsessionuploadtask

我有以下代码上传文件:

self.uploadManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:lSessionConfiguration];
self.uploadManager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:uploadURL]];
[req setHTTPMethod:@"POST"];
NSProgress *progress;
[[self.uploadManager uploadTaskWithRequest:req fromFile:fileURL progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    NSAssert(!error, @"%s: uploadTaskWithRequest error: %@", __FUNCTION__, error);
    NSLog(@"Task Completed: %@ %@", response, [responseObject class]);



    UILocalNotification *notif = [[UILocalNotification alloc] init];
    //notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    notif.alertBody = @"Upload Complete";
    notif.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:notif];
}] resume];

[progress addObserver:self
           forKeyPath:@"fractionCompleted"
              options:NSKeyValueObservingOptionNew
              context:NULL];

第二次加载时,出现以下错误:

  

标识符为<的后台URLSession ID>已经存在!

如何重复使用URLSession

0 个答案:

没有答案