为iOS安排后台任务的最佳方法?

时间:2013-11-21 04:25:17

标签: ios

我现在有一个应用程序,允许用户从网站提取信息。遗憾的是,我没有任何访问该网站的API,这只是我屏幕刮擦的东西。理想情况下,用户每天会从网站上抓取一次信息。 目前我设定时间并使用performFetchWithCompletionHandler。当performFetchWithCompletionHandler被调用时,它会检查设置的时间,如果时间是当前时间,它将执行抓取。不幸的是,performFetchWithCompletionHandler不能以这种方式工作(我无法保证它会在用户指定的时间和分钟内触发)。有没有办法让我的应用程序在后台运行并在指定的时间间隔内抓取一个网站?

1 个答案:

答案 0 :(得分:1)

这对你有帮助......

首先申报,              NSTimer * timerAppBG; 在你的AppDelegate.h文件....

- >请将以下所有方法放在AppDelegate.m文件中......

- (void)applicationWillResignActive:(UIApplication *)application
{
     timerAppBG = [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(applicationWillResign) userInfo:nil repeats:YES];
}
- (void) applicationWillResign
{
      NSLog(@"About to lose focus");
     //Write your code here...
     [self myVcInitMethod];
}

- (void) myVcInitMethod 
{
     [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(applicationWillResign)
     name:UIApplicationWillResignActiveNotification
     object:NULL];
}

感谢。

相关问题