NStimer我想在后台调用methos

时间:2015-10-12 11:32:52

标签: objective-c nstimer

我想每3分钟在后台拨打电话,所以我正在使用Twilio。我可以为前景中的每3个调用,但是当我在后台的iPhone设备上构建应用程序时,它无法正常工作。经过一段时间fb会话正在退出..

UIApplication *app1 = [UIApplication sharedApplication];

//create new uiBackgroundTask
__block UIBackgroundTaskIdentifier bgTask1 = [app1 beginBackgroundTaskWithExpirationHandler:^{
    [app1 endBackgroundTask:bgTask1];
    bgTask1 = UIBackgroundTaskInvalid;
}];

//and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   //run function methodRunAfterBackground
    timerForPhone = [NSTimer scheduledTimerWithTimeInterval:[string integerValue] target:self selector:@selector(methodForMakingCall) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timerForPhone forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
});

1 个答案:

答案 0 :(得分:1)

将应用程序置于后台后,您无法控制它。操作系统可以根据资源需求杀死它。与Android不同,您需要唤醒您的应用程序以执行某些逻辑。有一些解决方法,比如使用位置管理器的重要更改机制,但仍然没有指定的API。无论何时在iOS上执行某些后台逻辑,都需要牢记这一点。