iOS后台模式

来源:互联网 发布:linux一键安装php 编辑:程序博客网 时间:2024/06/04 20:45

工程截图如下:

 一起看下官方文档:

大致意义如下,有些功能本人也没见过相应的应用:

1、后台播放音频内容,例 酷我音乐。

2、基于位置的信息给用户,并要求使用标准定位服务 , 例 快的、乐动力

3、该应用程序提供的VoIP服务,   例 QQ语音聊天

4、该应用需要从网络上的定期的新内容   例 天气预报

5、应用程序使用远程通知     例  所有推送的APP

6、该应用程序进程使用NewsstandKit框架,新闻杂志

7、该应用程序定期提供数据的附件进行通信  例 不详

8、应用程序使用CoreBluetooth框架蓝牙配件在后台进行沟通  例不详

9、应用程序使用CoreBluetooth框架,外围模式蓝牙配件通信   例不详

接下来在工程的info-plist里面添加相应的选项,如下图:



4、从网络上的定期的新内容示例

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    [application setMinimumBackgroundFetchInterval:     UIApplicationBackgroundFetchIntervalMinimum];//注册 更新时间间隔    return YES;}- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:3000/update.do"];    //实现数据请求    NSURLSession *updateSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];    [updateSession dataTaskWithHTTPGetRequest:url                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {                                NSDictionary *messageInfo = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];                                NSLog(@"messageInfo:%@",messageInfo);                                completionHandler(UIBackgroundFetchResultNewData);                            }];}

首先在 application:didFinishLaunchingWithOptions: 中设置 minimun background fetch interval 类型为 UIApplicationBackgroundFetchIntervalMinimum(默认为 UIApplicationBackgroundFetchIntervalNever),然后实现代理方法 application:performFetchWithCompletionHandler: 中实现数据请求。




0 0
原创粉丝点击