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

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

1.【弹出提示对话框】

在iOS9之前我们使用AlertView来弹出对话框,现在推荐使用AlertController

2.【stringByAddingPercentEncodingWithAllowedCharacters替换stringByAddingPercentEscapesUsingEncoding】

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

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替换NSURLConnection】

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//以下方法已经被禁用;
  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
10
11
12
13
14
15
16
17
18
19
20
//推荐使用这种请求方法;
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];

最新文章

  1. CSS画猪
  2. React-Native性能优化点
  3. Linux 调节屏幕亮度
  4. 玩转EasyUi弹出框
  5. Scala List的排序函数sortWith
  6. 定义设置颜色的RGB值的宏
  7. Linux中的随机数文件 /dev/random /dev/urandom
  8. Apache 虚拟主机 VirtualHost 配置
  9. SQL Server 中使用参数化Top语句
  10. LV在系统重启后不能自动激活(boot.lvm&after.loca)
  11. Mac下Cordova开发环境搭建
  12. CentOS7配置更新国内yum源
  13. 【转】ADO.Net对Oracle数据库的操作
  14. PHP 依赖注入和控制反转再谈(二)
  15. Graham 扫描法找凸包(convexHull)
  16. Java笔记(十九) 反射
  17. Linux /etc/profile文件详解
  18. 第一个 mac 程序 Create-JSON-Model
  19. Spring.Net Aop 学习
  20. Latex文件如何拆分进行独立编译?

热门文章

  1. 织梦(DEDE)CMS V5.3 覆盖任意变量导致远程包含漏洞
  2. SQL Server基础知识三十三问 (1-7)
  3. CSS 中的强制换行和禁止换行
  4. hadoop压缩框架
  5. Virtualbox中Linux添加新磁盘并创建分区
  6. dede 怎样调用其它栏目的文章或者缩略图列表且有分页效果?
  7. easyui messager alert 三秒后自动关闭提示
  8. Linux 重启Tomcat脚本
  9. 基于Struts2、Spring、Hibernate实现的包括多条件查询分页的基础Dao层帮助jar包实现
  10. Call to a member function select() on string错误