IOS development-get rid of the disabled methods (continuous update)

Source: Internet
Author: User

IOS development-get rid of the disabled methods (continuous update)

With the rapid development of the iOS platform, various interfaces are constantly updated. With the release of iOS9, another batch of old methods are not recommended. If you call these methods, the running results will be correct, but the warning "*** is deprecated: first deprecated in iOS 9.0-Use *******". like:

 

In actual project development, we need to adhere to the belief that every warning should be handled as an error and every warning should be solved. Think about it. Even if you run a project successfully, are you in a bad mood if you have dozens or even hundreds of yellow warnings? I will use this blog with my development experience to list the methods that are not recommended in iOS9 and change them to the latest recommended method by Apple. This blog will be updated continuously.

1. [prompt Dialog Box]

Before iOS9, we used AlertView to bring up the dialog box. Now we recommend using AlertController. For this change, please refer to another blog titled correct implementation of iOS9 prompt box.

 

2. Replace stringByAddingPercentEncodingWithAllowedCharacters with stringByAddingPercentEscapesUsingEncoding]

This method is really long... We use this method to change the string encoding method. The most common method is to call this method to change the encoding method to utf8 if the parameters of the sent link contain Chinese characters during Http network requests, because the server generally uses UTF-8 encoding. The two functions are the same.

 

// The following method is not recommended; // NSString * urlStr = [@ http://v.juhe.cn/weather/index? Format = 2 & cityname = Beijing & key = 88e194ce72b455563c3bed01d5f967c5stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

// It is recommended to use this method stringByAddingPercentEncodingWithAllowedCharacters, stringByAddingPercentEscapesUsingEncoding is not recommended; NSString * urlStr2 = [@ http://v.juhe.cn/weather/index? Format = 2 & cityname = Beijing & key = 88e194ce72b455563c3bed01d5f967c5 stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet];


 

3. [NSURLSession replaces NSURLConnection]

The NSURLSession has gradually entered the stage of history. When [NSURLConnection sendAsynchronousRequest] was recently used, it was warned that this method is not recommended. So let's change it to the dataTaskWithRequest method in NSURLSession.

 

// The following methods have been disabled: NSOperationQueue * queue = [[NSOperationQueue alloc] init]; [NSURLConnection failed: urlRequest queue: queue completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * error) {if ([data length]> 0 & error = nil) {NSString * html = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; resault = [html copy]; NSLog (@ returned server data =%@, html);} else if ([da Ta length] = 0 & error = nil) {NSLog (@ Nothing was downloaded.);} else if (error! = Nil) {NSLog (@ error =%@, error) ;}}];

// This request method is recommended. NSURLSession * session = [NSURLSession sharedSession]; _ block NSString * result = @; NSURLSessionDataTask * dataTask = [session dataTaskWithRequest: urlRequest completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {if (! Error) {// no error. The returned result is correct. result = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ return correct: % @, result );} else {// error; NSLog (@ error message: % @, error) ;}}]; [dataTask resume];


 

 

 

 

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.