2
我正在開發一個上傳多個文件的應用程序。 對於上傳,我使用AFHTTPRequestOperation
。它成功地工作,但如果我鎖定並解鎖屏幕後,它停止上傳文件。AFHTTPRequestOperation在待機模式後不起作用
我要上傳文件代碼是在這裏
NSUserDefaults *defaultUser = [NSUserDefaults standardUserDefaults];
NSString *userId = [defaultUser stringForKey:@"UserId"];
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",UploadURL,userId]]];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:nil];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:nil parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: data name:@"f" fileName:[NSString stringWithFormat:@"%d_image.jpeg",rand()] mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
if(error.code == -1001){
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"The request timed out." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[myAlert show];
}
}];
[operation start];
誰能給我建議,對於處理這種情況。
謝謝。
這與setShouldExecuteAsBackgroundTaskWithExpirationHandler有關,但我不能100%確定代碼必須去哪裏 – Nick
http://stackoverflow.com/a/9164755/1702413 – TonyMkenu
@TonyMkenu這是NSURL連接,而不是AFNetworking? – Nick