NSURLConnection
- 最古老的方案NSURLSession
- iOS7推出的新技術CFNetworking
- NSURL的底層,純C語言ASIHttpRequest
- 外號「HTTP終結者」,功能強大,惋惜已經中止更新AFNetworking
- 維護使用者多MKNetworkKit
- 印度,維護使用者少NSURLConnection
(瞭解便可)// 發送同步請求,會一直等待, 直到接收到數據 - (void)requestSynch { // 1 建立請求鏈接 NSURL *url = [NSURL URLWithString:@"http://www.lala.com/login?username=123&pwd=123"]; // 2 建立請求 NSURLRequest *request = [NSURLRequest requestWithURL:url ]; NSHTTPURLResponse *response = nil; NSError *error = nil; // 3 發送同步請求 // endSynchronousRequest阻塞式的方法,等待服務器返回數據 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // 4.解析服務器返回的數據(解析成字符串) NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"--%@--%@-- %zd",str,response.allHeaderFields,response.statusCode); }
// 發送異步請求 - (void)requestAsync { // 1 建立請求鏈接 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/login?username=123&pwd=123"]; // 2 建立請求 NSURLRequest *request = [NSURLRequest requestWithURL:url ]; // 3 發送異步請求 [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 4.解析服務器返回的數據(解析成字符串) NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSHTTPURLResponse *httpRes = (NSHTTPURLResponse* )response; NSLog(@"--%@--%zd--%@--",str,httpRes.statusCode,httpRes.allHeaderFields); }]; }
NSURLConnectionDataDelegate
// 1 建立請求鏈接 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/login?username=520it&pwd=520it"]; // 2 建立請求 NSURLRequest *request = [NSURLRequest requestWithURL:url ]; // 3 代理請求模式,要遵照協議並實現代理方法 [NSURLConnection connectionWithRequest:request delegate:self]; ///-----------------------------------------------------------------/// // 經常使用代理方法 // 接收服務器響應 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { self.localData = [NSMutableData data]; NSLog(@"-didReceiveResponse-%zd",((NSHTTPURLResponse *)response).statusCode); } // 接收到服務器的數據(若是數據量比較大,這個方法會被調用屢次) - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.localData appendData:data]; NSLog(@"-didReceiveData-"); } // 完成數據下載 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *str = [[NSString alloc] initWithData:self.localData encoding:NSUTF8StringEncoding]; NSLog(@"-connectionDidFinishLoading-%@",str); } // 請求失敗:請求超時等 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"-didFailWithError-"); }
- (IBAction)loginBtn:(id)sender { // 獲取控件數據和網絡數據進行比對 NSString *userName = self.userText.text; if (userName.length == 0 ) { [SVProgressHUD showErrorWithStatus:@"用戶名不能爲空"]; return; } NSString *pwd = self.pwdText.text; if (pwd.length == 0) { [SVProgressHUD showErrorWithStatus:@"密碼不能爲空"]; return; } // 顯示陰影 [SVProgressHUD showWithStatus:@"正在登錄中" maskType:SVProgressHUDMaskTypeBlack]; // NSString *format = [NSString stringWithFormat:@"http://123.123.123.123/login?username=%@&pwd=%@",userName,pwd]; NSLog(@"%@",format); // 1 建立請求鏈接 NSURL *url = [NSURL URLWithString:format]; // 2 建立請求 NSURLRequest *request = [NSURLRequest requestWithURL:url ]; // 3 發送異步請求 [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // JSON:{"success":"登陸成功"} // 4.解析服務器返回的數據(解析成字符串) NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // 對字符串進行分割 NSInteger loc = [str rangeOfString:@"\":\""].location + 3; NSInteger len = [str rangeOfString:@"\"}"].location - loc; NSString *result = [str substringWithRange:NSMakeRange(loc, len)]; // 顯示結果 if ([result containsString:@"success"]) { [SVProgressHUD showSuccessWithStatus:result]; } else { [SVProgressHUD showErrorWithStatus:result]; } }];
NSString *format = [NSString stringWithFormat:@"http://123.123.123.123/login"]; // 1 建立請求鏈接 NSURL *url = [NSURL URLWithString:format]; // 2 建立請求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url ]; // 更改請求方法 request.HTTPMethod = @"POST"; // 設置請求體 request.HTTPBody = [@"username=520it&pwd=520it" dataUsingEncoding:NSUTF8StringEncoding]; // 設置超時 request.timeoutInterval = 5; // 設置請求頭 // [request setValue:@"ios9.0" forHTTPHeaderField:@"User-agent"]; // 3 發送請求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError) { NSLog(@"失敗"); } else { NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } }];
format = [format stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 設置請求體 request.HTTPBody = [@"username=小碼哥&pwd=520it" dataUsingEncoding:NSUTF8StringEncoding];
JSON | OC |
---|---|
{ } | 字典 NSDictionry |
[] | 數組 NSArray |
「 」 | 字符串 NSString |
10,10.4 | NSNumber |
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; // data轉JSON NSString *str = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) { NSJSONReadingMutableContainers = (1UL << 0), // 可變容器 NSJSONReadingMutableLeaves = (1UL << 1), // 子節點也是可變的,也就是說轉換的全部數據都是可變的 NSJSONReadingAllowFragments = (1UL << 2) // 接收零散數據,好比說單個的‘10’,'false' } NS_ENUM_AVAILABLE(10_7, 5_0);
NSJSONReadingAllowFragments
使用以下// 參數NSJSONReadingAllowFragments 用來讀取單個元素 NSString *str = @"10"; NSData *data = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil]; NSLog(@"-- %@ --",data);
#import <MJExtension.h> // 得到視頻的模型數組 self.videos = [SLQVideo objectArrayWithKeyValuesArray:dict[@"videos"]];
// 播放視頻 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123"]; // MPMoviePlayerViewController // 視圖控制器 // MPMoviePlayerController // 內部沒有view,不能直接彈出 MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[url URLByAppendingPathComponent:video[@"url"]]]; // modal 出視頻並播放 [self presentViewController:vc animated:YES completion:nil];
// 初始化方法 - (instancetype)initWithContentsOfURL:(NSURL *)url; // initializes the parser with the specified URL. - (instancetype)initWithData:(NSData *)data; // create the parser from data
// 1 使用蘋果的XML類解析xml文檔 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; parser.delegate = self; // 2 設置代理 [parser parse]; // 3 啓動掃描
#pragma mark -NSXMLParserDelegate代理方法 // 讀取文檔開始 - (void)parserDidStartDocument:(NSXMLParser *)parser { // NSLog(@"parserDidStartDocument"); // self.videoArray = [NSMutableArray array]; } // 讀取文檔結束 - (void)parserDidEndDocument:(NSXMLParser *)parser { // NSLog(@"parserDidEndDocument--%@",self.videoArray); } // 開始讀取元素,attributes 參數表示要讀取的元素 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ // NSLog(@"didStartElement---%@",attributeDict); // 全部的元素都會通過這裏進行讀取,包括根 // 因此要進行判斷,把根給排除 if ([elementName isEqualToString:@"videos"]) { return; } // 獲取模型數據,放入數組 SLQVideoItem *video = [SLQVideoItem objectWithKeyValues:attributeDict]; [self.videoArray addObject:video]; } // 讀取元素結束 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { }
Header Search Paths
添加/usr/include/libxml2
Other Linker Flags contain
添加 -lxml2
-fno -objc -arc
// 1 使用GDataXML解析xml文件 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; // 2 獲取全部video元素,首先要得到根節點 NSArray *elements = [doc.rootElement elementsForName:@"video"]; // 3 遍歷全部元素 for (GDataXMLElement *ele in elements) { SLQVideoItem *video = [[SLQVideoItem alloc] init]; // 4 獲取元素屬性,轉換成字符串 video.name = [[ele attributeForName:@"name"] stringValue]; video.image = [[ele attributeForName:@"image"] stringValue]; video.url = [[ele attributeForName:@"url"] stringValue]; video.length = [[[ele attributeForName:@"length"] stringValue] intValue]; [self.videoArray addObject:video]; }
// NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/weather?place=beijing&place=shanghai"]; NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/weather?place=beijing,shanghai"];
@implementation NSDictionary (Log) - (NSString *)descriptionWithLocale:(id)locale { // {"weathers":[{"city":"beijing,shanghai","status":"晴轉多雲"}]}\ // 將這句話格式化輸出 NSMutableString *str =[NSMutableString string]; [str appendString:@"{\n"]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { // 拼接字符串 [str appendFormat:@"\t%@",key]; [str appendString:@" : "]; [str appendFormat:@"%@,\n",obj]; }]; [str appendString:@"}"]; // 取出最後一個逗號 NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch]; if (range.location != NSNotFound) { [str deleteCharactersInRange:range]; } return str; } @end /* 2015-07-15 18:43:08.137 05-掌握-多值參數[65936:116425] { weathers : [ { status : 晴轉多雲, city : Beijing }, { status : 晴轉多雲, city : Shanghai } ] } */ @implementation NSArray (Log) - (NSString *)descriptionWithLocale:(id)locale { // 將這句話格式化輸出 NSMutableString *str =[NSMutableString string]; [str appendString:@"[\n"]; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [str appendFormat:@"\t%@,\n",obj]; }]; [str appendString:@"\t]"]; // 取出最後一個逗號 NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch]; if (range.location != NSNotFound) { [str deleteCharactersInRange:range]; } return str; } @end
直接使用NSData下載html
NSData *data = [NSData dataWithContentsOfURL:url];
NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/images/minion_13.png"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"---%zd---",data.length); }];
- (void)viewDidLoad { [super viewDidLoad]; // 創建鏈接 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/videos/minion_02.mp4"]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { NSLog(@"didReceiveResponse:%@",response); // 初始化 self.data = [NSMutableData data]; self.movieCount = [response.allHeaderFields[@"Content-Length"] integerValue]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 緩存到內存 [self.data appendData:data]; CGFloat progress = 1.0 * self.data.length / self.movieCount; self.progressView.progress = progress; NSLog(@"%f",progress * 100 ); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // 這裏進行文件的保存,保存到cache裏 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; [self.data writeToFile:[filePath stringByAppendingPathComponent:@"1.mp4"] atomically:YES]; self.data = nil; }
// 接收到響應的時候:建立一個空的文件 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { // 獲取文件長度 self.movieCount = [response.allHeaderFields[@"Content-Length"] integerValue]; // 建立一個空文件 [[NSFileManager defaultManager] createFileAtPath:SLQFilePath contents:nil attributes:nil]; // 建立文件句柄 self.handle = [NSFileHandle fileHandleForWritingAtPath:SLQFilePath]; } // 接收到具體數據:立刻把數據寫入一開始建立好的文件 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 移動到文件末尾 [self.handle seekToEndOfFile]; // 寫入數據 [self.handle writeData:data]; // self.currentCount += data.length; CGFloat progress = 1.0 * self.currentCount / self.movieCount; self.progressView.progress = progress; NSLog(@"%f",progress * 100 ); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { self.movieCount = 0; // 關閉文件 [self.handle closeFile]; self.handle = nil; }
SSZipArchive
// 1 使用指定文件,建立一個壓縮文件 NSArray *paths = @[ @"/Users/song/Desktop/test/1.png", @"/Users/song/Desktop/test/2.png", @"/Users/song/Desktop/test/3.png", @"/Users/song/Desktop/test/4.png", @"/Users/song/Desktop/test/5.png" ]; [Main createZipFileAtPath:@"/Users/song/Desktop/test.zip" withFilesAtPaths:paths]; // 2 使用指定目錄建立一個壓縮文件 [Main createZipFileAtPath:@"/Users/song/Desktop/test121212.zip" withContentsOfDirectory:@"/Users/song/Desktop/test"]; // 3 解壓縮一個文件到指定目錄 [Main unzipFileAtPath:@"/Users/song/Desktop/test.zip" toDestination:@"/Users/song/Desktop"];
// 必定要注意這個格式是固定的 /* 文件參數格式 --分割線\r\n Content-Disposition: form-data; name="參數名"; filename="文件名"\r\n Content-Type: 文件的MIMEType\r\n \r\n 文件數據 \r\n */ // 文件上傳 // 一、建立請求 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/upload"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 二、設置協議 request.HTTPMethod = @"POST"; // 三、設置請求頭 [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",SLQBoundary] forHTTPHeaderField:@"Content-Type"]; // 四、設置請求體 NSMutableData *body = [NSMutableData data]; // 設置文件參數 // 設置邊界 [body appendData:SLQUTF(@"--")]; [body appendData:SLQUTF(SLQBoundary)]; [body appendData:SLQEnter]; // 文件參數名 [body appendData:SLQUTF([NSString[] stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"1.png\""])]; [body appendData:SLQEnter]; // 文件類型 [body appendData:SLQUTF([NSString stringWithFormat:@"Content-Type: image/png"])]; [body appendData:SLQEnter]; // 文件內容 [body appendData:SLQEnter]; UIImage *image = [UIImage imageNamed:@"1"]; [body appendData:UIImagePNGRepresentation(image)]; [body appendData:SLQEnter]; /* 非文件參數格式 --分割線\r\n Content-Disposition: form-data; name="參數名"\r\n \r\n 參數值 \r\n */ // 設置非文件參數 [body appendData:SLQUTF(@"--")]; [body appendData:SLQUTF(SLQBoundary)]; [body appendData:SLQEnter]; [body appendData:SLQUTF([NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\""])]; [body appendData:SLQEnter]; [body appendData:SLQEnter]; [body appendData:SLQUTF(@"bulabulabula")]; [body appendData:SLQEnter]; /* 結束標記 --分割--線\r\n \r\n */ // 結束標記 [body appendData:SLQUTF(@"--")]; [body appendData:SLQUTF(SLQBoundary)]; [body appendData:SLQUTF(@"--")]; [body appendData:SLQEnter]; request.HTTPBody = body; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 這個方法會調用descriptionWithLocale方法,能夠在這裏解決輸出到控制檯顯示中文亂碼的問題 NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }];
- (NSString *)getMIMEType:(NSString *)path { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]; NSURLResponse *response = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; return response.MIMEType; }
// 要包含頭文件MobileCoreServices.h + (NSString *)mimeTypeForFileAtPath:(NSString *)path { if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { return nil; } CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL); CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType); CFRelease(UTI); if (!MIMEType) { return @"application/octet-stream"; } return (__bridge NSString *)(MIMEType); }
- (void)viewDidLoad { [super viewDidLoad]; // 創建鏈接 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/videos/minion_02.mp4"]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self]; } /** * 接收到響應的時候:建立一個空的文件 */ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { // 獲取服務器那裏給出的建議名字 response.suggestedFilename); NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename]; // 建立文件流 self.stream = [[NSOutputStream alloc] initToFileAtPath:path append:YES]; // 打開文件流 [self.stream open]; } /** * 接收到具體數據:立刻把數據寫入一開始建立好的文件 */ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 參數1要求是bytes [self.stream write:[data bytes] maxLength:data.length]; NSLog(@"---"); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // 關閉文件流 [self.stream close]; }
// 創建鏈接 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/images/minion_05.png"]; NSURLConnection *con = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self]; // 決定代理方法在哪一個隊列中執行 [con setDelegateQueue:[[NSOperationQueue alloc] init]]; // 開啓子線程runloop self.runloop = CFRunLoopGetCurrent(); CFRunLoopRun();
在代理方法中使用完畢,中止runloophtml5
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"--connectionDidFinishLoading--%@",[NSThread currentThread]); CFRunLoopStop(self.runloop); }
NSURLSession
主要由兩部分組成,一個是Session實例對象,一個是任務。dataTaskWithRequest
),啓動task(resume
)NSURLSessionDataTask
、NSURLSessionDownloadTask
、NSURLSessionUploadTask
)NSURLSession
代理
<NSURLSessionDataDelegate>
didReceivedResponse
didReceivedData
didComplete
NSURLSession
的方法:suspend、resume、cancel
resumeData
保存暫停時程序數據狀態,取消任務後要根據狀態恢復下載NSURLSessionUploadTast
methodBody
爲上傳的參數fromData
NSURLSessionConfiguration
統一配置(好比能夠在這裏設置是否容許設置程序使用蜂窩移動網絡)NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/login?username=123&pwd=4324"]; // 1 得到NSURLSession單例對象 NSURLSession *session = [NSURLSession sharedSession]; // 2 建立任務 NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // 4 處理數據 NSLog(@"----%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }]; // 3 啓動任務 [task resume];
// post請求 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/login?username=123&pwd=4324"]; // 得到NSURLSession單例對象 NSURLSession *session = [NSURLSession sharedSession]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 設置請求頭 request.HTTPMethod = @"POST"; // 設置請求體 NSMutableData *body = [NSMutableData data]; [body appendData:[@"username=123&pwd=234" dataUsingEncoding:NSUTF8StringEncoding]]; request.HTTPBody = body; // 建立任務 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"----%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }]; // 啓動任務 [task resume];
downloadTaskWithURL:url
進行下載,不過下載成功的文件是放在tmp臨時目錄裏面的,必定要及時把文件給移出來。NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/videos/minion_02.mp4"]; // 得到NSURLSession單例對象 NSURLSession *session = [NSURLSession sharedSession]; // 建立任務 NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename]; // 這個方式下載的文件在tmp文件夾下,要把下載的文件移動到cache中永久保存,參數是 fileURLWithPath,看清了 // loaction 表示在本地臨時存儲區的路徑 [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:nil]; }]; // 啓動任務 [task resume];
- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/videos/minion_02.mp4"]; // 建立 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; // 建立任務 NSURLSessionDataTask *task = [session dataTaskWithURL:url]; // 啓動任務 [task resume]; } // 接收服務器響應,必須手動執行後續的執行方式(容許接收數據仍是不容許接收) - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { NSLog(@"%s",__func__); // 必須手動指定接下來的數據要不要接收 completionHandler(NSURLSessionResponseAllow); } // 接收數據,屢次調用 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { NSLog(@"%s",__func__); } // 下載完畢後調用,若是失敗,看參數error的值 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { NSLog(@"%s",__func__); } typedef NS_ENUM(NSInteger, NSURLSessionResponseDisposition) { NSURLSessionResponseCancel = 0,/* Cancel the load, this is the same as -[task cancel] */ NSURLSessionResponseAllow = 1,/* Allow the load to continue */ NSURLSessionResponseBecomeDownload = 2,/* Turn this request into a download */ } NS_ENUM_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0);
NSURLSession
,使用dataWithTask來進行下載,而且手動控制器下載文件的去向。- (NSURLSessionDataTask *)dataTask { if (!_dataTask) { // 獲取下載進度,直接從沙盒中讀取文件長度 NSInteger total = [[NSMutableDictionary dictionaryWithContentsOfFile:SLQDownloadFilePath][SLQFileName] integerValue]; NSInteger current = [[[NSFileManager defaultManager] attributesOfItemAtPath:SLQFilePath error:nil][NSFileSize] integerValue]; if (total && total == current ) { NSLog(@"已經下載完畢"); return nil; } // 若是長度和 NSURL *url = [NSURL URLWithString:@"http://123.123.123.123/resources/videos/minion_02.mp4"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 指定要從服務器獲取的數據的範圍 NSString *range = [NSString stringWithFormat:@"bytes=%zd-",current]; [request setValue:range forHTTPHeaderField:@"Range"]; // 建立任務 _dataTask = [self.session dataTaskWithRequest:request]; } return _dataTask; } - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSHTTPURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { // NSLog(@"----%@",[response class]); // 開啓輸出流 [self.stream open]; // 計算總得數據長度,response會返回請求的數據長度,不包括已經下載的數據長度,因此要累加起來 self.totalCount = [response.allHeaderFields[@"Content-Length"] integerValue] + SLQFileLength; NSString *name = response.suggestedFilename; // 存儲總長度,將所要下載的文件長度保存起來 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:SLQDownloadFilePath]; if (dict == nil) { dict = [NSMutableDictionary dictionary]; } dict[SLQFileName] = @(self.totalCount); [dict writeToFile:SLQDownloadFilePath atomically:YES]; // 繼續下載 completionHandler(NSURLSessionResponseAllow); } - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { // 寫入數據到沙盒 [self.stream write:[data bytes]maxLength:data.length]; // 獲取下載進度,直接從沙盒中讀取文件長度 NSLog(@"---%f",1.0 * SLQFileLength / self.totalCount); } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { // 清空 self.dataTask = nil; [self.stream close]; self.stream = nil; }
// 必定要注意這個格式是固定的 /* 文件參數格式 --分割線\r\n Content-Disposition: form-data; name="file"; filename="文件名"\r\n Content-Type: 文件的MIMEType\r\n \r\n 文件數據 \r\n // 結束標記 \r\n --分割線--\r\n \r\n */ // 主要是參數第二個參數要傳入 **`請求體`** [[self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"---%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }] resume];
AFHTTPRequestManager
AFHTTPSessionManager
- (void)GET { // GET AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; // 將數據做爲參數傳入 NSDictionary *dict = @{ @"username":@"12", @"pwd":@"13" }; [mgr GET:[NSString stringWithFormat:@"http://123.123.123.123/login"] parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"success:%@",responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"failure:%@",error); }]; } - (void)POST { // POST AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; // 將數據做爲參數傳入 NSDictionary *dict = @{ @"username":@"12", @"pwd":@"13" }; [mgr POST:[NSString stringWithFormat:@"http://123.123.123.123/login"] parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"success:%@",responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"failure:%@",error); }]; }
- (void)upload { // 文件上傳 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; // 將數據做爲參數傳入 [mgr POST:@"http://123.123.123.123/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { // formdata 爲要上傳的數據 // [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/song/Desktop/test.png"] name:@"file" error:nil]; [formData appendPartWithFileData:[NSData dataWithContentsOfFile:@"/Users/song/Desktop/test.png"] name:@"file" fileName:@"wer.png" mimeType:@"image/png"]; } success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"success:%@",responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"failure:%@",error); }]; }
- (void)download { // 下載 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; [[mgr downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://123.123.123.123/resources/images/minion_02.png"]] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { // 下載文件須要返回一個保存路徑,還須要手動啓動resume NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; return [NSURL fileURLWithPath:[path stringByAppendingPathComponent:response.suggestedFilename]]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"%@",filePath.path); }] resume]; }
responseSerializer
屬性設置爲ADHTTPResonseSericlizer
,服務器返回什麼就接受什麼類型的數據。-(void)returnType { // 默認返回的數據時JSON,若是想返回XML,設置屬性responseSerializer // 若是想返回服務器上文件原本的類型,設置AFHTTPResponseSerializer AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; // responseSerializer 用來解析服務器返回的數據 mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; // 直接使用「服務器原本返回的數據」,不作任何解析 // 告訴AFN,以XML形式解析服務器返回的數據 // mgr.responseSerializer = [AFXMLParserResponseSerializer serializer]; // 將數據做爲參數傳入 [mgr GET:[NSString stringWithFormat:@"http://123.123.123.123/resources/images/minion_02.png"] parameters:nil success:^(NSURLSessionDataTask *task,id response) { NSLog(@"success:%zd",[response length]); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"failure:%@",error); }]; }
- 手機聯網狀態:`AFNetWorkReachabityManager` - 蘋果自帶:`Reachability` ,經過通知監聽系統狀態
AFNetWorkReachabityManager
- (void)monitor { // 監控網絡狀態 AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager]; // 網絡狀態改變就會調用這個block [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"網絡狀態改變:%zd",status); }]; // 打開監聽器 [mgr startMonitoring]; /* typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) { AFNetworkReachabilityStatusUnknown = -1, // 未知 AFNetworkReachabilityStatusNotReachable = 0, // 未聯網 AFNetworkReachabilityStatusReachableViaWWAN = 1, // 蜂窩網絡 AFNetworkReachabilityStatusReachableViaWiFi = 2, // wifi }; */ }
Reachability
/**通知*/ @property (nonatomic, strong) Reachability *reach; - (void)viewDidLoad { [super viewDidLoad]; // 添加通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNetworkStatus) name:kReachabilityChangedNotification object:nil]; // 接收通知 self.reach = [Reachability reachabilityForInternetConnection]; [self.reach startNotifier]; } - (void)getNetworkStatus { /* typedef enum : NSInteger { NotReachable = 0, // 網絡不可知 ReachableViaWiFi, // WIFI ReachableViaWWAN // 移動網絡 } NetworkStatus; */ // 獲取手機網絡狀態 if([Reachability reachabilityForLocalWiFi].currentReachabilityStatus != NotReachable) { NSLog(@"wifi"); } else if([Reachability reachabilityForInternetConnection].currentReachabilityStatus != NotReachable) { NSLog(@"3G?4G"); } else { NSLog(@"Nothing at all!"); } } - (void)dealloc { // 中止監聽器 [self.reach startNotifier]; // 移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil]; }
使用https要實現代理方法 didReceiveChallenge
ios
1 建立HTTPS連接web
_dataTask = [self.session dataTaskWithURL:[NSURL URLWithString:@"https://www.apple.com/"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); }];
didReceiveChallenge
/** 代理方法 * challenge : 挑戰、質詢 * completionHandler : 經過調用這個block,來告訴URLSession要不要接收這個證書 */ - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { // NSURLSessionAuthChallengeDisposition : 如何處理這個安全證書 // NSURLCredential :安全證書 // NSLog(@"%@",challenge); if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { return; } if(completionHandler) { // 利用這個block說明使用這個證書 completionHandler(NSURLSessionAuthChallengeUseCredential,challenge.proposedCredential); } }
<UIWebViewDelegate>
shouldStartLoadWithRequest
: 請求以前判斷是否容許訪問(過濾某些網址)loadHTMLString
loadData:
scalesPageToFit
屏幕自適應dataDetectorTypes
自動檢測網頁中出現的電話號碼,網址等,添加下劃線和連接。// 始發送請求(加載數據)時調用這個方法 - (void)webViewDidStartLoad:(UIWebView *)webView; // 請求完畢(加載數據完畢)時調用這個方法 - (void)webViewDidFinishLoad:(UIWebView *)webView; // 請求錯誤時調用這個方法 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; // UIWebView在發送請求以前,都會調用這個方法,若是返回NO,表明中止加載請求,返回YES,表明容許加載請求 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
/* * 每當webView即將發送一個請求以前,都會調用這個方法 * 返回YES:容許加載這個請求 * 返回NO:禁止加載這個請求 */ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSLog(@"%s",__func__); if ([request.URL.absoluteString containsString:@"life"]) { return NO; } return YES; }
-alert(10); // 彈框
document.getElementById(‘test’); // 根據ID得到某個DOM元素
stringByEvaluatingJavaScriptFromString
function
shouldStartLoadWithRequest
loaction.href = 'slq://sendMessage_?參數1&參數2';
stringByEvaluatingJavaScriptFromString
[webView stringByEvaluatingJavaScriptFromString:@"alert(100)"]; // 調用JS中的函數, NSLog(@"%@",[webView stringByEvaluatingJavaScriptFromString:@"login();"]); self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title;"];
經過代理法方法 shouldStartLoadWithRequest數據庫
無參數傳遞json
/** * 經過這個方法完成JS調用OC * JS和OC交互的第三方框架:WebViewJavaScriptBridge */ // location.href = 'slq://call'; - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // NSLog(@"%@",request.URL); NSString *url = request.URL.absoluteString; NSString *pre = @"slq://"; if([url hasPrefix:pre]) { // 調用OC方法 // NSLog(@"調用OC方法"); NSString *method = [url substringFromIndex:pre.length]; // NSSelectorFromString 將字符串轉換成方法名 [self performSelector:NSSelectorFromString(method) withObject:nil]; return NO; } // NSLog(@"發送請求"); return YES; }
// location.href = 'slq://call2_number2_?&100'; - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *url = request.URL.absoluteString; NSString *pre = @"slq://"; if([url hasPrefix:pre]) { // 調用OC方法 NSString *method = [url substringFromIndex:pre.length]; // method = call2_number2_?200&300 // 分割字符串 NSArray *arr = [method componentsSeparatedByString:@"?"]; // call2:number2: NSString *methodName = [[arr firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"]; // 200&300 NSString *paramStr = [arr lastObject]; NSArray *params = nil; if (arr.count == 2 && [paramStr containsString:@"&"]) { params = [paramStr componentsSeparatedByString:@"&"]; } NSString *param1 = [params firstObject]; NSString *param2 = params.count <= 1 ? nil : [params lastObject]; NSLog(@"%@",methodName); [self performSelector:NSSelectorFromString(methodName) withObject:param1 withObject:param2]; // 兩個參數 // [self performSelector:NSSelectorFromString(methodName) withObject:para];// 一個參數 return NO; } return YES; }
- (id)performSelector:(SEL)selector withObjects:(NSArray *)params { // 設置方法簽名 NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:selector]; // if (sig == nil) { NSLog(@"方法沒找到"); } // NSInvocation 包裝對象利用一個NSInvocation對象包裝一次方法調用(方法調用者、方法名、方法參數、方法返回值) NSInvocation *invo = [NSInvocation invocationWithMethodSignature:sig]; invo.target = self; invo.selector = selector; // 設置參數 NSInteger paramNum = sig.numberOfArguments - 2; // 包含兩個隱含參數:self and _cmd // 取最小值做爲參數, paramNum = MIN(paramNum, params.count); for (int i = 0 ; i < paramNum; i ++) { id obj = params[i]; if ([obj isKindOfClass:[NSNull class]]) { continue; } [invo setArgument:&obj atIndex:i + 2]; // 從第三個參數開始 } // 設置回調 [invo invoke]; // 返回值 id returnVal = 0; if (sig.methodReturnLength) { [invo getReturnValue:&returnVal]; } return returnVal; }
void handleException(NSException *exception) { [[UIApplication sharedApplication].delegate performSelector:@selector(handle)]; } - (void)handle { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"哈哈" message:@"崩潰了把" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil]; [alertView show]; // 從新啓動RunLoop [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"-------點擊了好的"); exit(0); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 設置捕捉異常的回調 NSSetUncaughtExceptionHandler(handleException); return YES; }
// 去除Xcode編譯警告 //#pragma clang diagnostic push // 開始 #pragma clang diagnostic ignored "-Warc-performSelector-leaks" //#pragma clang diagnostic pop // 結束
@throw
@try @catch @finally