国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

用NSURLSession和NSURLConnection獲取文件的MIMEType

2019-11-14 19:38:37
字體:
供稿:網(wǎng)友
NSURLsession和NSURLConnection都是蘋果自帶的用于網(wǎng)絡(luò)請求的類,NSURLSession是iOS 7.0之后推出的用于替代NSURLConnection的。下面分享一下這兩個類獲取文件MIMEType的方法。
 1 #PRagma mark 獲取文件的mimeType 2 // NSURLSession版 3 - (void)getMIMEType { 4     // 用NSBundle獲取工程中文件路徑 5     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"png"]; 6     // 創(chuàng)建NSURL對象 7     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 8     // 創(chuàng)建請求 9     NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];10     // 創(chuàng)建NSURLSession的單例11     NSURLSession *session = [NSURLSession sharedSession];12     // 創(chuàng)建一個dataTask請求數(shù)據(jù)13     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {14         // response.MIMEType就是文件的MIMEType15         NSLog(@"%@", response.MIMEType);16     }];17     // session的任務(wù)默認是掛起的,需要手動啟用18     [dataTask resume];19 }20 // NSURLConnection版21 - (void)getMIMEType1 {22     // 用NSBundle獲取工程中文件路徑23     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"png"];24     // 創(chuàng)建NSURL對象25     NSURL *fileUrl = [NSURL fileURLWithPath:filePath];26     // 創(chuàng)建請求27     NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];28     NSURLResponse *response = nil;29     // 同步請求30     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];31     // response.MIMEType就是文件的MIMEType32     NSLog(@"%@", response.MIMEType);33 }

 

 

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 余庆县| 延长县| 五河县| 大石桥市| 深州市| 葵青区| 项城市| 青岛市| 抚松县| 增城市| 博客| 玉环县| 宣恩县| 长兴县| 五寨县| 星座| 麟游县| 安吉县| 瑞丽市| 泗水县| 桐城市| 西吉县| 湘阴县| 汶上县| 惠来县| 长泰县| 廊坊市| 肥西县| 徐闻县| 民丰县| 互助| 江华| 格尔木市| 昌平区| 平江县| 雅安市| 肃南| 临汾市| 白山市| 乌拉特前旗| 娱乐|