当前位置: 首页 > news >正文

申请域网站贵州省住房和城乡建设局网站首页

申请域网站,贵州省住房和城乡建设局网站首页,中小企业网站建设新闻,数字营销包括哪些方面三种方式使得iOS程序即使在关闭或崩溃的情况下也能够在后台持续进行一些任务#xff0c;比如更新程序界面快照#xff0c;下载文件等。这三个方法分别是Background Fetch#xff0c;Remote Notification和NSURLSession的backgroundSessionConfiguration Background Fetch 开…三种方式使得iOS程序即使在关闭或崩溃的情况下也能够在后台持续进行一些任务比如更新程序界面快照下载文件等。这三个方法分别是Background FetchRemote Notification和NSURLSession的backgroundSessionConfiguration Background Fetch 开启 首先在info plist文件中开启UIBackgroundModes的Background fetch。或者手动编辑这个值 keyUIBackgroundModes/key arraystringfetch/string /array iOS默认不进行background fetch需要设置一个时间的间隔 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//UIApplicationBackgroundFetchIntervalMinimum表示尽可能频繁去获取如果需要指定至少多少时间更新一次就需要给定一个时间值[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];return YES; } 最后在App Delegate里实现下面的方法这个方法只能在30秒内完成。 - (void) application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {NSURLSessionConfiguration *sessionConfiguration [NSURLSessionConfiguration defaultSessionConfiguration];NSURLSession *session [NSURLSession sessionWithConfiguration:sessionConfiguration]; NSURL *url [[NSURL alloc] initWithString:http://yourserver.com/data.json]; NSURLSessionDataTask *task [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error) { completionHandler(UIBackgroundFetchResultFailed); return; } // 解析响应/数据以决定新内容是否可用 BOOL hasNewData ... if (hasNewData) { completionHandler(UIBackgroundFetchResultNewData); } else { completionHandler(UIBackgroundFetchResultNoData); } }]; // 开始任务 [task resume]; } 测试 通过查看UIApplication的applicationState - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {NSLog(Launched in background %d, UIApplicationStateBackground application.applicationState); return YES; } Remote Notification 在普通的远程通知里带上content-available标志就可以在通知用户同时在后台进行更新。通知结构如下 {aps : {content-available : 1 }, content-id : 42 } 接收一条带有content-available的通知会调用下面的方法 - (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {NSLog(Remote Notification userInfo is %, userInfo); NSNumber *contentID userInfo[content-id]; // 根据 content ID 进行操作 completionHandler(UIBackgroundFetchResultNewData); } 利用NSURLSession进行background transfer task 使用[NSURLSessionConfiguration backgroundSessionConfiguration]创建一个后台任务当应用退出后崩溃或进程被关掉都还是会运行。 范例先处理一条远程通知并将NSURLSessionDownloadTask添加到后台传输服务队列。 - (NSURLSession *)backgroundURLSession {static NSURLSession *session nil;static dispatch_once_t onceToken; dispatch_once(onceToken, ^{ NSString *identifier io.objc.backgroundTransferExample; NSURLSessionConfiguration* sessionConfig [NSURLSessionConfiguration backgroundSessionConfiguration:identifier]; session [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]]; }); return session; } - (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(Received remote notification with userInfo %, userInfo); NSNumber *contentID userInfo[content-id]; NSString *downloadURLString [NSString stringWithFormat:http://yourserver.com/downloads/%d.mp3, [contentID intValue]]; NSURL* downloadURL [NSURL URLWithString:downloadURLString]; NSURLRequest *request [NSURLRequest requestWithURL:downloadURL]; NSURLSessionDownloadTask *task [[self backgroundURLSession] downloadTaskWithRequest:request]; task.taskDescription [NSString stringWithFormat:Podcast Episode %d, [contentID intValue]]; //执行resume保证开始了任务 [task resume]; completionHandler(UIBackgroundFetchResultNewData); } 下载完成后调用NSURLSessionDownloadDelegate的委托方法这些委托方法全部是必须实现的。了解所有类型session task的生命周期可以参考官方文档https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html#//apple_ref/doc/uid/10000165i-CH2-SW42 #Pragma Mark - NSURLSessionDownloadDelegate- (void) URLSession:(NSURLSession *)sessiondownloadTask:(NSURLSessionDownloadTask *)downloadTaskdidFinishDownloadingToURL:(NSURL *)location {NSLog(downloadTask:% didFinishDownloadingToURL:%, downloadTask.taskDescription, location); // 必须用 NSFileManager 将文件复制到应用的存储中因为临时文件在方法返回后会被删除 // ... // 通知 UI 刷新 } - (void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { } - (void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { } 后台的任务完成后如果应用没有在前台运行需要实现UIApplication的两个delegate让系统唤醒应用 - (void) application:(UIApplication *)applicationhandleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {// 你必须重新建立一个后台 seesiong 的参照// 否则 NSURLSessionDownloadDelegate 和 NSURLSessionDelegate 方法会因为// 没有 对 session 的 delegate 设定而不会被调用。参见上面的 backgroundURLSession NSURLSession *backgroundSession [self backgroundURLSession]; NSLog(Rejoining session with identifier % %, identifier, backgroundSession); // 保存 completion handler 以在处理 session 事件后更新 UI [self addCompletionHandler:completionHandler forSession:identifier]; } - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session { NSLog(Background URL session % finished events. , session); if (session.configuration.identifier) { // 调用在 -application:handleEventsForBackgroundURLSession: 中保存的 handler [self callCompletionHandlerForSession:session.configuration.identifier]; } } - (void)addCompletionHandler:(CompletionHandlerType)handler forSession:(NSString *)identifier { if ([self.completionHandlerDictionary objectForKey:identifier]) { NSLog(Error: Got multiple handlers for a single session identifier. This should not happen. ); } [self.completionHandlerDictionary setObject:handler forKey:identifier]; } - (void)callCompletionHandlerForSession: (NSString *)identifier { CompletionHandlerType handler [self.completionHandlerDictionary objectForKey: identifier]; if (handler) { [self.completionHandlerDictionary removeObjectForKey: identifier]; NSLog(Calling completion handler for session %, identifier); handler(); } }原文地址 转载于:https://www.cnblogs.com/pandas/p/4334871.html
http://www.huolong8.cn/news/78844/

相关文章:

  • 网站建设方案报价单wordpress正文底部版权信息
  • 网站 数据库龙岩市城乡建设局网站进不去
  • 公司做网站如何跟客户介绍什么网站能让小孩做算术题
  • 怎么让百度多收录网站沈阳做网站的公司有哪些
  • 制作大型网站电商网站多少钱
  • 对网站分析哈尔滨建站模板大全
  • 网站使用引导wordpress贴心插件
  • 思科中国网站开发案例php团购网站的难点
  • 济南集团网站建设报价scrm服务商
  • it培训网站重庆平台网站建设设计
  • 网站结构怎么做遂宁移动网站建设
  • 网站建设售后质量保证怎么给公司做推广
  • 往建设厅网站上传东西做公司网站排名
  • 池州网站开发怀柔 做网站的
  • 承德 网站维护廊坊app网站制作
  • 西安商城网站开发qq是哪个开发运营公司的
  • 做网站要什么步骤外包服务是什么
  • 门户网站 方案亚马逊跨境电商个人开店
  • 网站dw建设php网站开发事例
  • 做巧克力的网站大气蓝色wap网站模板
  • 东莞搜索引擎网站推广广告公司名称推荐
  • 教师在哪些网站可以做兼职ios移动网站开发工具
  • 网站开发用什么编程语言哈尔滨房产信息网官方网站
  • 河南快速网站备案wordpress本地打开慢
  • 网站建站平台源码化妆品行业的网站开发
  • 常用的网站有哪些ucenter整合wordpress
  • 汕头企业网站模板建站wordpress模板调用文件夹下
  • 安徽住房和建设网站wordpress 国产评论插件
  • 做火锅加盟哪个网站好网站更换域名备案
  • 外贸网站教程wordpress有点尴尬诶该页无法显示