美文网首页
iOS 后台运行,直接复制就好。

iOS 后台运行,直接复制就好。

作者: 何必太轻浮 | 来源:发表于2016-06-23 17:15 被阅读387次

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

然后在delegate里加入以下代码,原理是进入后台时程序会在600秒那样结束任务,我做的就是在结束任务前新开一个任务,再结束旧任务,这样就一直的在后台运行,原链接: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:^{

}];

//结束旧的后台任务

[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

相关文章

  • iOS 后台运行,直接复制就好。

    在xx-info.plist 里的 "Required background modes" 里加入"App pro...

  • Paper Collection - Background Ta

    1.IOS后台运行机制详解(一)2.IOS后台运行机制详解(二)3.IOS后台运行 之 后台播放音乐4.转载:IO...

  • iOS 短信验证码倒计时按钮

    级别: ★★☆☆☆标签:「iOS 验证码后台倒计时」「NSTimer后台运行」「iOS 定时器后台运行」作者: ...

  • iOS开发笔记:后台

    iOS7程序后台运行

  • iOS 申请后台运行的时间

    iOS 申请后台运行的时间

  • 上传文件样式美化

    直接复制就好了

  • iOS 后台刷新

    [TOC] iOS 后台刷新 首先大概介绍下iOS的APP运行状态简介和后台运行的一些基础知识 1. App运行状...

  • iOS后台运行

    我们知道,当app进入后台以后大约3分钟或者10分钟之后app就会被系统挂起。 最近有个项目需求:App作为web...

  • iOS:后台运行

    当我们有需求,需要应用在后台长久的运行一段时间,此时,就需要用到方法代码仅作记录和分享,详情参考唐巧的相关书籍。 ...

  • iOS后台运行

    iOS为了让设备尽量省电,减少不必要的开销,保持系统流畅,因而对后台机制采用墓碑式的“假后台”。除了系统官方极少数...

网友评论

      本文标题:iOS 后台运行,直接复制就好。

      本文链接:https://www.haomeiwen.com/subject/klfodttx.html