加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何解决iOS后台应用程序提取不起作用?

发布时间:2020-12-15 02:02:16 所属栏目:百科 来源:网络整理
导读:我正在尝试让iOS后台应用程式提取在我的应用程式中工作。在Xcode测试时,它可以在设备上运行时不起作用! 我的测试设备正在运行iOS 9.3.5(我的部署目标是7.1) 我在Xcode中的目标“功能”下的“后台模式”下启用了“后台提
我正在尝试让iOS后台应用程式提取在我的应用程式中工作。在Xcode测试时,它可以在设备上运行时不起作用!

>我的测试设备正在运行iOS 9.3.5(我的部署目标是7.1)
>我在Xcode中的目标“功能”下的“后台模式”下启用了“后台提取”功能

enter image description here

在应用程序:didFinishLaunchingWithOptions我已经尝试过各种间隔与setMinimumBackgroundFetchInterval,包括UIApplicationBackgroundFetchIntervalMinimum

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // tell the system we want background fetch
    //[application setMinimumBackgroundFetchInterval:3600]; // 60 minutes
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    //[application setMinimumBackgroundFetchInterval:1800]; // 30 minutes

    return YES;
}

我已经实现了应用程序:performFetchWithCompletionHandler

void (^fetchCompletionHandler)(UIBackgroundFetchResult);
NSDate *fetchStart;

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    fetchCompletionHandler = completionHandler;

    fetchStart = [NSDate date];

    [[NSUserDefaults standardUserDefaults] setObject:fetchStart forKey:kLastCheckedContentDate];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [FeedParser parseFeedAtUrl:url withDelegate:self];
}

 -(void)onParserFinished
{
    DDLogVerbose(@"AppDelegate/onParserFinished");

    UIBackgroundFetchResult result = UIBackgroundFetchResultNoData;

    NSDate *fetchEnd = [NSDate date];
    NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
    DDLogVerbose(@"Background Fetch Duration: %f seconds",timeElapsed);
    if ([self.mostRecentContentDate compare:item.date] < 0) {
        DDLogVerbose(@"got new content: %@",item.date);
        self.mostRecentContentDate = item.date;
        [self scheduleNotificationWithItem:item];
        result = UIBackgroundFetchResultNewData;
    }
    else {
        DDLogVerbose(@"no new content.");
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
        localNotification.alertBody = [NSString stringWithFormat:@"Checked for new posts in %f seconds",timeElapsed];
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }

    fetchCompletionHandler(result);
}

>我有(成功!)测试与模拟器和设备使用Xcode的Debug / SimulateBackgroundFetch
>我已经通过另一个SO答案(http://stackoverflow.com/a/29923802/519030)所示的新计划成功测试了
>我的测试显示执行在performFetch方法的代码大约0.3秒(所以它不需要很长时间)
>我已经验证设备在设置中启用了后台刷新功能。
>当然,我已经看过其他的SO问题,希望有人经历过同样的事情。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读