Summarize the new method in IOS9

Source: Internet
Author: User

iOS platform in the rapid development of various interfaces are constantly updated. With the release of IOS9, there are a number of old methods are not recommended, if you call these methods, the results of the operation is not a problem, but there will be a warning "***is deprecated:first deprecated in IOS 9.0-use *******". Like:

In actual project development, we must uphold the belief that every warning should be treated as an error and every warning should be resolved. You think, you run a project, even if the operation is successful, but there are dozens of, or even hundreds of yellow warning, the mood is not very bad? I will be in this blog combined with my development experience, the list of IOS9 not recommended methods, and replaced by Apple's latest recommended way. This blog post will be updated continuously.

1. "Pop-up prompt dialog box"

We used Alertview to pop up the dialog before iOS9, now we recommend using Alertcontroller

2. "Stringbyaddingpercentencodingwithallowedcharacters Replacement Stringbyaddingpercentescapesusingencoding"

This method is really very long ... We use this method to change the way strings are encoded. The most common place is to make an HTTP network request, the parameters of the sent link if with Chinese, then the first need to call this method to change the encoding mode to UTF8, because the server side is generally used UTF8 encoding. The function is the same for both implementations.

?
1 2 //以下方法已经不推荐使用;  //  NSString *urlStr = [@http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

?
1 2 //建议使用这个方法stringByAddingPercentEncodingWithAllowedCharacters,不推荐使用stringByAddingPercentEscapesUsingEncoding; NSString *urlStr2 = [@http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];



3. "Nsurlsession Replacement Nsurlconnection"

Nsurlsession has gradually embarked on the stage of history. The recent use of [Nsurlconnection Sendasynchronousrequest] has been warned that is not recommended to use, then we will replace the Datataskwithrequest method in Nsurlsession.

?
123456789Ten One A - - the - - - + - + A at - - - - //以下方法已经被禁用;  NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest: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(@返回的服务器数据 = %@, html);   }   else if ([data length] == 0 &&            error == nil){     NSLog(@Nothing was downloaded.);   }   else if (error != nil){     NSLog(@发生错误 = %@, error);   }     }];

?
1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - //推荐使用这种请求方法;NSURLSession *session = [NSURLSession sharedSession]; __block  NSString *result = @;NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {     if (!error) {    //没有错误,返回正确;    result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@返回正确:%@,result);       }else{    //出现错误;    NSLog(@错误信息:%@,error);  }   }];  [dataTask resume];

Summarize the new method in IOS9

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.