zoukankan      html  css  js  c++  java
  • (转)iOS开发——来改掉那些被禁用的方法吧(持续更新中)

     

    iOS平台在快速的发展,各种接口正在不断的更新。随着iOS9的发布,又有一批老方法不推荐使用了,你若调用这些方法,运行的结果是没有问题的,但是会出现警告“***is deprecated :first deprecated in iOS 9.0 - Use *******”.就像如图所示:

    在实际项目开发中,我们要秉承一个信念就是:要把每一个警告当做错误来处理,并解决每一个警告。你想想,你运行一个项目,就算运行成功了,但是出现几十个、甚至几百个黄黄的警告,心情是不是很糟糕呢?我将在这篇博客结合我的开发经验,罗列出iOS9中不推荐使用的方法,并换成苹果最新推荐的方式。本篇博客将会持续更新。

    1.【弹出提示对话框】

    在iOS9之前我们使用AlertView来弹出对话框,现在推荐使用AlertController,对于这个变化,请参考我的另一篇博客《iOS9使用提示框的正确实现方式》。

    2.【stringByAddingPercentEncodingWithAllowedCharacters替换stringByAddingPercentEscapesUsingEncoding】

    这个方法真的好长。。。我们使用这个方法来进行字符串编码方式的更改。最常用的地方就是进行Http网络请求的时候,发送的链接的参数中如果带有中文,那么首先就需要调用这个方法把编码方式改为utf8,因为服务器端一般都使用utf8编码。两者实现的功能一样。

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

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

    3.【NSURLSession替换NSURLConnection】

    NSURLSession已经渐渐走上历史舞台了。最近使用[NSURLConnection sendAsynchronousRequest]时已经警告为不推荐使用了,那我们就换成NSURLSession中的dataTaskWithRequest方法吧。

      //以下方法已经被禁用;
        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);
         }
         
       }];
      //推荐使用这种请求方法;
      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];
  • 相关阅读:
    Python从文件中读取数据
    Python中类的继承
    Python中的类(2)
    Python中的类
    自动登陆抽屉(1)
    爬取汽车之家新闻
    Django简介
    web应用,http协议简介,web框架
    CentOS7安装python3.6
    MySQL之索引
  • 原文地址:https://www.cnblogs.com/hello-Huashan/p/5171403.html
Copyright © 2011-2022 走看看