2015-10-12 18 views
0

我想在後臺每3分鐘撥打一次電話,所以我正在使用Twilio。我能夠在前臺每打3個電話,但是當我在後臺iPhone設備上構建應用程序時,它不起作用。經過一段時間fb會話正在註銷..NStimer我想調用背景中的方法

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

將應用程序放入後臺後,對其沒有太多控制。操作系統可以根據資源需求殺死它。與Android不同,您需要喚醒您的應用程序才能執行某些邏輯。有一些解決方法,如使用位置管理器的重要更改機制,但仍然沒有指定的API。無論何時在iOS上執行一些後臺邏輯,都需要牢記這一點。

相關問題