Use NSURLSession and NSURLConnection to obtain the MIMEType and nsurl of the file.

Source: Internet
Author: User

Use NSURLSession and NSURLConnection to obtain the MIMEType and nsurl of the file.

Both NSURLSession and NSURLConnection are Apple's built-in classes for network requests. NSURLSession was launched after iOS 7.0 to replace NSURLConnection. The following describes how to obtain the file MIMEType using these two classes.
1 # pragma mark get the mimeType 2 of the file // NSURLSession Version 3-(void) getMIMEType {4 // use NSBundle to obtain the file path in the project. 5 NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "a" ofType: @ "png"]; 6 // create NSURL object 7 NSURL * fileUrl = [NSURL fileURLWithPath: filePath]; 8 // CREATE request 9 NSURLRequest * request = [NSURLRequest requestWithURL: fileUrl]; 10 // create NSURLSession Singleton 11 NSURLSession * session = [NSURLSession sharedSession]; 12 // create a dataTask request data 13 NSURLSessionDataTask * dataTask = [session dataTaskWithRequest: request completionHandler: ^ (NSData * data, NSURLResponse * response, NSError * error) {14 // response. MIMEType is the MIMEType15 NSLog (@ "% @", response. MIMEType); 16}]; 17 // session tasks are suspended by default. You need to manually enable 18 [dataTask resume]; 19} 20 // NSURLConnection version 21-(void) getMIMEType1 {22 // use NSBundle to obtain the file path 23 NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "a" ofType: @ "png"]; 24 // create NSURL object 25 NSURL * fileUrl = [NSURL fileURLWithPath: filePath]; 26 // CREATE request 27 NSURLRequest * request = [NSURLRequest requestWithURL: fileUrl]; 28 NSURLResponse * response = nil; 29 // synchronous request 30 [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; 31 // response. MIMEType is the MIMEType32 NSLog (@ "% @", response. MIMEType); 33}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.