//上傳圖片
//iOS 10 上傳圖片原生代碼
-(void)nativeiOSUpImage:(UIImage *)image
{
//此處應(yīng)設(shè)置單例?
NSURLSessionConfiguration * onConfiguration = [NSURLSessionConfigurationdefaultSessionConfiguration];
self.session = [NSURLSessionsessionWithConfiguration:onConfiguration delegate:selfdelegateQueue:[NSOperationQueuemainQueue]];
?//開始上傳
NSMutableURLRequest * request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:@"http://control.blog.sina.com.cn/blog_rebuild/profile/controllers/points_action.php"]];
[request addValue:@"image/png"forHTTPHeaderField:@"Content-Type"];
[request addValue:@"text/html"forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval:20];
NSData * imagedata = UIImageJPEGRepresentation(image,1.0);//或NSData *data = UIImagePNGRepresentation(image);//一個(gè)是轉(zhuǎn)換JPEG 一個(gè)是轉(zhuǎn)換PNG 看需求
NSURLSessionUploadTask * uploadtask = [self.sessionuploadTaskWithRequest:request fromData:imagedata completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[uploadtask resume];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
NSLog(@"%@",error.description);
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent: (int64_t)totalBytesSent totalBytesExpectedToSend: (int64_t)totalBytesExpectedToSend{
}
/////?GET
?// 1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://blog.sina.com.cn/s/articlelist_3095139511_0_1.html?name=%@&tell=%
",name,tell]];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求
NSURLRequest *request =[NSURLRequest requestWithURL:url];
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象,創(chuàng)建一個(gè)Task任務(wù):
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
}];
// 5.最后一步更米,執(zhí)行任務(wù)(resume也是繼續(xù)執(zhí)行):
[sessionDataTask resume];
?//POST
?// 1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://blog.sina.com.cn/s/articlelist_3095139511_0_1.html
"]];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求,分別設(shè)置請(qǐng)求方法瓦盛、請(qǐng)求參數(shù)
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *args = [NSString stringWithFormat:@"name=%@&tell=%@",yourname,tell];
request.HTTPBody = UTF-8(args);//此處為宏定義UTF-8編碼
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象运挫,創(chuàng)建一個(gè)Task任務(wù)
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
}];
//5.最后一步侵俗,執(zhí)行任務(wù)痛倚,(resume也是繼續(xù)執(zhí)行)。
[sessionDataTask resume];
//代理方法
pragma mark -- NSURLSessionDataDelegate// 1.接收到服務(wù)器的響應(yīng)
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
//【注意:此處需要允許處理服務(wù)器的響應(yīng)耸峭,才會(huì)繼續(xù)加載服務(wù)器的數(shù)據(jù)桩蓉。 如果在接收響應(yīng)時(shí)需要對(duì)返回的參數(shù)進(jìn)行處理(如獲取響應(yīng)頭信息等),那么這些處理應(yīng)該放在這個(gè)允許操作的前面±湍郑】
completionHandler(NSURLSessionResponseAllow);
}
// 2.接收到服務(wù)器的數(shù)據(jù)(此方法在接收數(shù)據(jù)過程會(huì)多次調(diào)用)
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
// 處理每次接收的數(shù)據(jù)院究,例如每次拼接到自己創(chuàng)建的數(shù)據(jù)receiveData
[self.receiveData appendData:data];
}
// 3.任務(wù)完成時(shí)調(diào)用(如果成功,error == nil)
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if(error == nil){
}
else{
NSLog(@"請(qǐng)求失敗:%@",error);
}
}
//NSURLSessionDownloadTask:文件下載任務(wù)
// 1.創(chuàng)建網(wǎng)路路徑
NSURL *url = [NSURL URLWithString:@"http://blog.sina.com.cn/s/articlelist_3095139511_0_1.html
"] ;
// 2.獲取會(huì)話
NSURLSession *session = [NSURLSession sharedSession];
// 3.根據(jù)會(huì)話本涕,創(chuàng)建任務(wù)
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
NSURL *pathUrl = [NSURL fileURLWithPath:path];
// 剪切文件
[[NSFileManager defaultManager] moveItemAtURL:location toURL:pathUrl error:nil];
}];
// 4.啟動(dòng)任務(wù)
[task resume];
//NSURLSessionDownloadDelegate代理方法
// 1.每次寫入調(diào)用(會(huì)調(diào)用多次)
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
// 可在這里通過已寫入的長(zhǎng)度和總長(zhǎng)度算出下載進(jìn)度
CGFloat progress = 1.0 * totalBytesWritten / totalBytesExpectedToWrite; NSLog(@"%f",progress);
}
// 2.下載完成時(shí)調(diào)用
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
// 這里的location也是一個(gè)臨時(shí)路徑,需要自己移動(dòng)到需要的路徑(caches下面)
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
[[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:nil];
}
// 3.請(qǐng)求成功/失斠堤(如果成功,error為nil)
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if(error == nil){
}
else{
NSLog(@"請(qǐng)求失敗:%@",error);
}
}
?
// 1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://172.16.2.254/php/phonelogin?yourname=%@&yourpas=%@&btn=login",yourname,yourpass]];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求
NSURLRequest *request =[NSURLRequest requestWithURL:url];
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象偏友,創(chuàng)建一個(gè)Task任務(wù):
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
}];
// 5.最后一步蔬胯,執(zhí)行任務(wù)(resume也是繼續(xù)執(zhí)行):
[sessionDataTask resume];
// 1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://172.16.2.254/php/phonelogin?yourname=%@&yourpas=%@&btn=login",yourname,yourpass]];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求
NSURLRequest *request =[NSURLRequest requestWithURL:url];
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象对供,創(chuàng)建一個(gè)Task任務(wù):
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
}];
// 5.最后一步位他,執(zhí)行任務(wù)(resume也是繼續(xù)執(zhí)行):
[sessionDataTask resume];
// 1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://172.16.2.254/php/phonelogin"]];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求,分別設(shè)置請(qǐng)求方法产场、請(qǐng)求參數(shù)
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *args = [NSString stringWithFormat:@"yourname=%@&yourpass=%@&btn=login",yourname,yourpass];
request.HTTPBody = [args dataUsingEncoding:NSUTF8StringEncoding];
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象鹅髓,創(chuàng)建一個(gè)Task任務(wù)
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
}];
//5.最后一步,執(zhí)行任務(wù)京景,(resume也是繼續(xù)執(zhí)行)窿冯。
[sessionDataTask resume];
// 1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://172.16.2.254/php/phonelogin"]];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求,分別設(shè)置請(qǐng)求方法确徙、請(qǐng)求參數(shù)
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *args = [NSString stringWithFormat:@"yourname=%@&yourpass=%@&btn=login",yourname,yourpass];
request.HTTPBody = [args dataUsingEncoding:NSUTF8StringEncoding];
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象醒串,創(chuàng)建一個(gè)Task任務(wù)
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
}];
//5.最后一步,執(zhí)行任務(wù)鄙皇,(resume也是繼續(xù)執(zhí)行)芜赌。
[sessionDataTask resume];