iOS 后臺運行,直接復制就好。

在xx-info.plist 里的 "Required background modes" 里加入"App provides Voice over IP services"

然后在delegate里加入以下代碼,原理是進入后臺時程序會在600秒那樣結(jié)束任務,我做的就是在結(jié)束任務前新開一個任務,再結(jié)束舊任務,這樣就一直的在后臺運行,原鏈接:http://www.devdiv.com/forum.php?mod=viewthread&tid=200491

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()

{

UIBackgroundTaskIdentifier backgroundTaskIdentifier;

UIBackgroundTaskIdentifier oldBackgroundTaskIdentifier;

NSInteger count;

}

@property (nonatomic,strong) NSTimer *timer;

@end

@implementation AppDelegate

//-(NSTimer *)timer{

//? ? if (_timer==nil) {

//? ? ? ? _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

//

//? ? ? ? [_timer fire];

//? ? }

//? ? return _timer;

//}

- (BOOL) isMultitaskingSupported{

BOOL result = NO;

if ([[UIDevice currentDevice]

respondsToSelector:@selector(isMultitaskingSupported)]){ result = [[UIDevice currentDevice] isMultitaskingSupported];

}

return result;

}

- (void) timerMethod:(NSTimer *)paramSender{

count++;

if (count % 500 == 0) {

UIApplication *application = [UIApplication sharedApplication];

//開啟一個新的后臺

backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{

}];

//結(jié)束舊的后臺任務

[application endBackgroundTask:backgroundTaskIdentifier];

oldBackgroundTaskIdentifier = backgroundTaskIdentifier;

}

NSLog(@"%ld",count);

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

if ([self isMultitaskingSupported] == NO){

return; }

//開啟一個后臺任務

backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{

}];

oldBackgroundTaskIdentifier = backgroundTaskIdentifier;

if ([self.timer isValid]) {

[self.timer invalidate];

}

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];

}

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

// Override point for customization after application launch.

//? ? ViewController *vc = [[ViewController alloc] init];

//? ? UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

//? ? self.window.rootViewController = vc;

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

//? ? [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

//? ? [self timer];

//? ? [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:10000];

return YES;

}

- (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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

-(void)backgroundHandler{

[self timer];

}

-(void)timerFired{

NSLog(@"%s %@",__func__,_timer);

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

if (backgroundTaskIdentifier != UIBackgroundTaskInvalid){

[application endBackgroundTask:backgroundTaskIdentifier];

if ([self.timer isValid]) {

[self.timer invalidate];

}

}

}

- (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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容