开发环境 Xcode 10.1
运行环境 iOS 12.1
说一下问题场景,这个是分享成功后调用服务器方法保存分享数据到服务器,基本上成功不了几次,只有偶尔才能调用接口成功,开始以为是AFN的问题,结果上网搜索已经有人给苹果反馈问题,很显然这个锅AFN不背,虽然是APPLE问题,但也要想办法解决。
https://github.com/AFNetworking/AFNetworking/issues/4279
尝试解决:
方案一:后台切换到前台时调用网络链接加延时
方案二:为APP续命
#import "AppDelegate.h"@interface AppDelegate ()@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier taskId;
@property (nonatomic, strong) NSTimer *timer;@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.return YES;
}- (void)applicationDidEnterBackground:(UIApplication * )application
{NSLog(@"---进入后台----");self.taskId = [application beginBackgroundTaskWithExpirationHandler:^(void) {//当申请的后台时间用完的时候调用这个block//此时我们需要结束后台任务,[self endTask];}];// 模拟一个长时间的任务 Taskself.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(longTimeTask:) userInfo:nil repeats:YES];
}- (void)applicationDidBecomeActive:(UIApplication * )application
{NSLog(@"---活跃状态----");[self endTask];
}#pragma mark - 停止timer
-(void)endTask
{if (_timer != nil||_timer.isValid ){[_timer invalidate];_timer = nil;//结束后台任务[[UIApplication sharedApplication] endBackgroundTask:_taskId];_taskId = UIBackgroundTaskInvalid;NSLog(@"停止计时");}
}- (void)longTimeTask:(NSTimer *)timer
{NSTimeInterval time = [[UIApplication sharedApplication] backgroundTimeRemaining];NSLog(@"系统留给的我们的时间 = %.02f Seconds", time);
}- (void)applicationWillResignActive:(UIApplication *)application {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}//- (void)applicationDidEnterBackground:(UIApplication *)application {
// // 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.
//}- (void)applicationWillEnterForeground:(UIApplication *)application {// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}//- (void)applicationDidBecomeActive:(UIApplication *)application {
// // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//}- (void)applicationWillTerminate:(UIApplication *)application {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}@end