多線程
- NSThread
- GCD
- NSOperation
- RunLoop
- 同一時間只能選擇一個模式運行
- 常用模式
- Default:默認
- Tracking:拖拽UIScrollView
網絡
HTTP請求
// URL
NSString *urlStr = @"http://xxxxx.com";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// 請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// URL
NSString *urlStr = @"http://xxxxx.com";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// 請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
數據解析
- JSON
- XML
- SAX: NSXMLParser
- DOM: GDataXML
NSURLConnection(iOS9已經過期)
NSURLSession
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[task resume];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
}];
[task resume];
// 創建session
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
// 設置請求頭(告訴服務器第1024個字節開始下載)
[request setValue:@"bytes=1024-" forHTTPHeaderField:@"Range"];
// 創建任務
NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
// 開始任務
[task resume];
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
// 打開NSOutputStream
// 獲得文件的總長度
// 存儲文件的總長度
// 回調
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
// 利用NSOutputStream寫入數據
// 計算下載進度
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
// 關閉并且清空NSOutputStream
// 清空任務task
}
- 文件上傳
- 設置請求頭
- 拼接請求體
- NSURLSessionUploadTask
NSURLSessionUploadTask *task = [[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[task resume];
AFNetworking
// AFHTTPSessionManager內部包裝了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it"
};
[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"請求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"請求失敗---%@", error);
}];
// AFHTTPSessionManager內部包裝了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it"
};
[mgr POST:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"請求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"請求失敗---%@", error);
}];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"123"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在這個block中設置需要上傳的文件
// NSData *data = [NSData dataWithContentsOfFile:@"/Users/xiaomage/Desktop/placeholder.png"];
// [formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"];
// [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
UIWebView
- 加載請求
- JS和OC的互相調用
- 利用NSInvocation實現performSelector無限參數
- 異常捕捉