// 在AppDelete實(shí)現(xiàn)該方法
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//進(jìn)入后臺(tái)
}
// 在AppDelete實(shí)現(xiàn)該方法
- (void)applicationDidBecomeActive:(UIApplication *)application{
// app啟動(dòng)或者app從后臺(tái)進(jìn)入前臺(tái)都會(huì)調(diào)用這個(gè)方法
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// app從后臺(tái)進(jìn)入前臺(tái)都會(huì)調(diào)用這個(gè)方法
}
利用通知在控制器里監(jiān)聽(tīng)app進(jìn)入前臺(tái)或者后臺(tái)
// app啟動(dòng)或者app從后臺(tái)進(jìn)入前臺(tái)都會(huì)調(diào)用這個(gè)方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
// app從后臺(tái)進(jìn)入前臺(tái)都會(huì)調(diào)用這個(gè)方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecomeActive) name:UIApplicationWillEnterForegroundNotification object:nil];
// 添加檢測(cè)app進(jìn)入后臺(tái)的觀察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name: UIApplicationDidEnterBackgroundNotification object:nil];
在解決 App 防止抓包問(wèn)題的時(shí)候雹熬,有一種常見(jiàn)的解決方案就是:檢測(cè)是否存在代理服務(wù)器。其實(shí)現(xiàn)為:
+ (BOOL)getProxyStatus {
CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
const CFStringRef proxyCFstr = CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesHTTPProxy);
CFRelease(dicRef);
NSString *proxy = (__bridge NSString*)(proxyCFstr);
if(proxy) {
return YES;
}
return NO;
}
dispatch_sync : 同步谣膳,不具備開(kāi)啟線程的能力 dispatch_async : 異步竿报,具備開(kāi)啟線程的能力
并發(fā)隊(duì)列 :多個(gè)任務(wù)可以同時(shí)執(zhí)行 串行隊(duì)列 :一個(gè)任務(wù)執(zhí)行完后,再執(zhí)行下一個(gè)任務(wù)
1继谚、async -- 并發(fā)隊(duì)列(最常用)
/**
* async -- 并發(fā)隊(duì)列(最常用)
* 會(huì)不會(huì)創(chuàng)建線程:會(huì)烈菌,一般同時(shí)開(kāi)多條
* 任務(wù)的執(zhí)行方式:并發(fā)執(zhí)行
*/
- (void)asyncGlobalQueue {
// 獲得全局的并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 將 任務(wù) 添加 全局隊(duì)列 中去 異步 執(zhí)行
dispatch_async(queue, ^{
NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
});
}
2、async -- 串行隊(duì)列(有時(shí)候用)
/**
* async -- 串行隊(duì)列(有時(shí)候用)
* 會(huì)不會(huì)創(chuàng)建線程:會(huì)花履,一般只開(kāi)1條線程
* 任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))
*/
- (void)asyncSerialQueue {
// 1.創(chuàng)建一個(gè)串行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("com.asliving.queue", NULL);
// 2.將任務(wù)添加到串行隊(duì)列中 異步 執(zhí)行
dispatch_async(queue, ^{
NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
});
// 3.非ARC芽世,需要釋放創(chuàng)建的隊(duì)列
// dispatch_release(queue);
}
3、async -- 主隊(duì)列(很常用)
/**
* async -- 主隊(duì)列(很常用)
*/
- (void)asyncMainQueue {
// 1.主隊(duì)列(添加到主隊(duì)列中的任務(wù)诡壁,都會(huì)自動(dòng)放到主線程中去執(zhí)行)
dispatch_queue_t queue = dispatch_get_main_queue();
// 2.添加 任務(wù) 到主隊(duì)列中 異步 執(zhí)行
dispatch_async(queue, ^{
NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
});
}
4济瓢、sync -- 主隊(duì)列(不能用---會(huì)卡死)
/**
* sync -- 主隊(duì)列(不能用---會(huì)卡死)
*/
- (void)syncMainQueue {
NSLog(@"syncMainQueue----begin--");
// 1.主隊(duì)列(添加到主隊(duì)列中的任務(wù),都會(huì)自動(dòng)放到主線程中去執(zhí)行)
dispatch_queue_t queue = dispatch_get_main_queue();
// 2.添加 任務(wù) 到主隊(duì)列中 異步 執(zhí)行
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
});
NSLog(@"syncMainQueue----end--");
}
5妹卿、sync -- 并發(fā)隊(duì)列
/**
* sync -- 并發(fā)隊(duì)列
* 會(huì)不會(huì)創(chuàng)建線程:不會(huì)
* 任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))
* 并發(fā)隊(duì)列失去了并發(fā)的功能
*/
- (void)syncGlobalQueue {
// 獲得全局的并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 將 任務(wù) 添加到 全局并發(fā)隊(duì)列 中 同步 執(zhí)行
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
});
}
6葬荷、sync -- 串行隊(duì)列
/**
* sync -- 串行隊(duì)列
* 會(huì)不會(huì)創(chuàng)建線程:不會(huì)
* 任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))
*/
- (void)syncSerialQueue {
// 創(chuàng)建一個(gè)串行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);
// 將 任務(wù) 添加到 串行隊(duì)列 中 同步 執(zhí)行
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
});
}