国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 永康市| 长沙市| 仁布县| 黄浦区| 盐津县| 金川县| 烟台市| 景洪市| 临江市| 东至县| 疏勒县| 高安市| 汤阴县| 平塘县| 平昌县| 天峨县| 高雄市| 洛川县| 福泉市| 开封市| 灵寿县| 遂宁市| 邵武市| 日喀则市| 宕昌县| 尤溪县| 禄丰县| 托里县| 华容县| 牟定县| 柳江县| 饶阳县| 洪雅县| 乐安县| 衡东县| 成武县| 阿尔山市| 松桃| 石渠县| 青阳县| 仙游县|