我错过了什么吗?因为我希望应用程序接收响应并做一些事情,即使它在后台。 这是我在我的应用程序中使用的内容:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = /*generate the parameters*/;
NSURL *filePath = [NSURL fileURLWithPath:@"imageName.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
//debug here
//Told the app to stop the Loading View, save response to DB
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
//debug here
//Told the app to stop the Loading View and try again sometimes
}];
答案 0 :(得分:6)
这是我的坏事。我忘了告诉应用程序在进入后台时继续运行。我将这些代码添加到我的项目中:
UIBackgroundTaskIdentifier backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
}];
当应用程序进入后台时:
[[UIApplicatioz sharedApplication] endBackgroundTask:backgroundTask];
答案 1 :(得分:1)
在Appdelegate.m文件的顶部定义UIBackgroundTaskIdentifier
UIBackgroundTaskIdentifier bgTask = 0;
然后在你的
中
- (void)applicationDidEnterBackground:(UIApplication *)application
功能添加以下代码
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
应该看作:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}