问题描述
我想从我的应用上传文件到我的服务器。下面的代码在应用程序处于活动状态时工作良好。如果我按Home键或打开另一个应用程序上传停止。
我激活后台提取,但仍然无法正常工作。
Afnetworking有后台支持,但我无法弄清楚如何实现这个功能,我的代码。
pre $ N $ C $ str = [[ NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@Archive.zip];
NSDictionary * parameters = @ {@foo:@bar};
NSURL * filePath = [NSURL fileURLWithPath:str];
AFHTTPRequestSerializer * serializer = [AFHTTPRequestSerializer serializer];
NSData * imageData = [NSData dataWithContentsOfURL:filePath];
$ b $ NSMutableURLRequest * request =
[serializer multipartFormRequestWithMethod:@POSTURLString:@http:// url
参数:参数
constructBodyWithBlock :^(id< AFMultipartFormData> formData){
[formData appendPartWithFileData:imageData
name:@image
fileName:@Archive.zip
mimeType:@application /压缩];
}];
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation * operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation * operation,id responseObject){
NSLog(@Success%@,responseObject);
}失败:^(AFHTTPRequestOperation *操作,NSError *错误){
NSLog(@Failure%@,error.description);
}];
$ b $ [操作setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite){
NSLog(@Wrote%lld /%lld,totalBytesWritten,totalBytesExpectedToWrite);
}];
[操作开始];
更改这一行
[operation start];
至此
[操作setShouldExecuteAsBackgroundTaskWithExpirationHandler:^ {
//处理iOS关闭你(可能记下你的
//停止的地方,以便以后可以恢复)
}];
[manager.operationQueue addOperation:operation];
I want to upload files to my server from my app. The code below is working great when app is active. If I press home button or open another app uploading stops.
I activated background fetch but still not working.
Afnetworking has background support but I cant figure out how I implement this feature to my code.
NSString *str=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Archive.zip"];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:str];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSData *imageData=[NSData dataWithContentsOfURL:filePath];
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://url"
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:@"image"
fileName:@"Archive.zip"
mimeType:@"application/zip"];
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@", error.description);
}];
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
Change this line
[operation start];
to this
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
// Handle iOS shutting you down (possibly make a note of where you
// stopped so you can resume later)
}];
[manager.operationQueue addOperation:operation];
You can look at these linksAFHTTPRequestOperation doesn't work after stand by modeand Alternative for enqueueHTTPRequestOperation in AFNetworking 2.0
这篇关于AFNetworking后台文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!