iOS8后台运行方案总结

上传人:汽*** 文档编号:467206850 上传时间:2022-08-31 格式:DOC 页数:21 大小:100KB
返回 下载 相关 举报
iOS8后台运行方案总结_第1页
第1页 / 共21页
iOS8后台运行方案总结_第2页
第2页 / 共21页
iOS8后台运行方案总结_第3页
第3页 / 共21页
iOS8后台运行方案总结_第4页
第4页 / 共21页
iOS8后台运行方案总结_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《iOS8后台运行方案总结》由会员分享,可在线阅读,更多相关《iOS8后台运行方案总结(21页珍藏版)》请在金锄头文库上搜索。

1、iOS 8.0后台任务长期运行方案后台运行方案实现方式为:Backgro un dRu nne叶 NSURLSession1. 配置軸MlCinaz ts infs 乱呢讷辭Build 內曲99Build RusVoice cvsr IF9 kfl*t3tana 旳 ic3Cs Dcttnai acceissory conniricabor3 IMta BtK)ao41i L acCBsSdrieaj*ds 35 a E .Btcotti LE access:Parrott isMca:x&5tepi: J J4d dRcqu -2 E?nirrtd Joow* 唱 io)cuf pn,ie*

2、 Keychain ShmngApp GroupsOf?- HomoKitOats Pto:Mi i2. 在 AppDelegate 中property (assign , nonatomic ) BOOL isBackground; / 判读是否是后台运行-(void )applicationDidEnterBackground:( UIApplication*)applicationBackgroundRunner shared run;self . isBackground = yes;/在回到界面后关闭后台运行-(void )applicationWillEnterForeground

3、:( UIApplication*)application BackgroundRunner shared stop ;self .isBackground = NO;3. 手机密码锁开启后的文件保护问题设置/ 设置常量#define UrlsessionDownload #define BundleIdentifier NSBundle mainBundlebundleIdentifier/ 打开 uisesion 目录的文件保护设置为无 /创建可使用的 urlseseion 路径 -(void )createUrlSessionNSString *theFMCachePath= nil;N

4、SArray *pathes = NSSearchPathForDirectoriesInDomains (NSCachesDirect ory, NSUserDomainMask , YES);theFMCachePath = pathes objectAtIndex :0; theFMCachePath = theFMCachePath stringByAppendingString :UrlsessionDownload ;theFMCachePath = theFMCachePath stringByAppendingString :NSString stringWithFormat

5、:%/ ,BundleIdentifier ;NSFileManager *fileManager = NSFileManager defaultManager ;if(!fileManagerisExecutableFileAtPath :theFMCachePath)/设置文件为无权限保护NSDictionary *attributes = NSDictionary dictionaryWithObject :NSFileProtectionNoneforKey :NSFileProtectionKey ;fileManagercreateDirectoryAtPath :theFMCac

6、hePathwithIntermediateDirectories :YESattributes :attributeserror :nil ;4上传 sessioin 的代码(, UploadSessionBackgound.h, UploadSessionBackgound.m )/Upl oadSessionBackgound.h#import protocol UploadSessionBackgoundDelegate-(void )requestUploadFinishDictionary:( NSDictionary *)dictionary;endinterface Uploa

7、dSessionBackgound : NSObject property (nonatomic , strong ) NSURLSession *urlSession;property (nonatomic , strong ) NSURLSessionUploadTask *sesssionDataTask;property (nonatomic , weak ) id sessioinDelegate;-(void )startBackground:( NSMutableURLRequest *)request;-(void )stopUploadBackground;end/Upl o

8、adSessionBackgound.m#import UploadSessionBackgound.himplementation UploadSessionBackgound synthesize urlSession,sesssionDataTask,sessioinDelegate;-(NSURLSession *)urlSessionstatic NSURLSession *session;static dispatch_once_t onceToken;dispatch_once (&onceToken, Asession = NSURLSession sessionWithCon

9、figuration :backgroundConfigObject delegate :self delegateQueue :nil;);return session;-(void )startBackground:( NSMutableURLRequest *)request self .sesssionDataTask = self .urlSession uploadTaskWithStreamedRequest :request;self .sesssionDataTask resume ;-(void )stopUploadBackgroundself .sesssionData

10、Task cancel ;- (void )URLSession:( NSURLSession *)session dataTask:( NSURLSessionDataTask *)dataTask didReceiveData:( NSData *)data;if(data= nil)self .sessioinDelegaterequestUploadFinishDictionary :nil;return ;NSDictionary *dict = NSJSONSerializationJSONObjectWithData :data options :0 error :nil ;NS

11、Log (dict:% ,dict);self .sessioinDelegate requestUploadFinishDictionary :dict;- (void )URLSession:( NSURLSession *)session didBecomeInvalidWithError:( NSError *)error self .sessioinDelegate requestUploadFinishDictionary :nil; end上传 session 写好后,下面就可以使用这个 session 了,每个应用上传接口都不一样,但是 最终会调用上传, 我这里的方式是上传的信

12、息全 部在 NSMutableURLRequest 中,NSMutableURLRequest 就大概说下NSMutableURLRequest*request= NSMutableURLRequest requestWithURL :s_url cachePolicy :NSURLRequestReloadIgnoringLocalCacheD ata timeoutInterval :CONNECT_TIMEOUT ;request setHTTPBody :data;request setHTTPMethod :PUT ;s_url :上传的接口 url,如 CONNECT_TIMEOU

13、T :超时时间,如: 60data:将要上传的nsdata数据上传完成后回调方法:-(void)requestUploadFinishDictionary:( NSDictionary *)dictionary/处理逻辑5.很多上传逻辑中并不是仅仅包含上传文件的 业务,也有很多普通的网络请求, 我们来说说如 何把普通的网络请求也和 urlsession 方式处理, (SeesionConnection.h, SeesionConnection.m)/ SeesionConnection.h #import protocol SeesionConnectionDelegate -(void )r

14、equestSeesionConnectionDictionary:( NSDictionary *)dictionary;endinterface SeesionConnection : NSObjectproperty (nonatomic , strong ) NSURLSession *urlSession;property (nonatomic , strong ) NSURLSessionDownloadTask *sesssionDataTask;property (nonatomic , weak ) id sessioinDelegate;-(void )startBackground:( NSMutableURLRequest *)request;-(void )stopDownBackground;end/ SeesionConnection.m#import SeesionConnection.himplementation SeesionConnection synthesize urlSession,sesssionDataTask,se

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号