iOS开发 适配iOS10

iOS开发 适配iOS10-----------------------  转载自"鸿鹄当高远" 的博客园

 

2016年9月7日,苹果发布iOS 10。2016年9月14日,全新的操做系统iOS 10将正式上线。html

做为开发者,如何适配iOS10呢?json

1.Notification(通知)

自从Notification被引入以后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至如今iOS 10开始真正的进行大改重构,这让开发者也体会到UserNotifications的易用,功能也变得很是强大。数组

  • iOS 9 之前的通知安全

    1.在调用方法时,有些方法让人很难区分,容易写错方法,这让开发者有时候很苦恼。性能优化

    2.应用在运行时和非运行时捕获通知的路径还不一致。网络

    3.应用在前台时,是没法直接显示远程通知,还须要进一步处理。app

    4.已经发出的通知是不能更新的,内容发出时是不能改变的,而且只有简单文本展现方式,扩展性根本不是很好。框架

  • iOS 10 开始的通知dom

    1.全部相关通知被统一到了UserNotifications.framework框架中。iphone

    2.增长了撤销、更新、中途还能够修改通知的内容。

    3.通知不在是简单的文本了,能够加入视频、图片,自定义通知的展现等等。

    4.iOS 10相对以前的通知来讲更加好用易于管理,而且进行了大规模优化,对于开发者来讲是一件好事。

    5.iOS 10开始对于权限问题进行了优化,申请权限就比较简单了(本地与远程通知集成在一个方法中)。

    若是使用了推送,修改如图:

   

   

2.ATS的问题

iOS 9中默认非HTTS的网络是被禁止的,固然咱们也能够把NSAllowsArbitraryLoads设置为YES禁用ATS。不过iOS 10从2017年1月1日起苹果不容许咱们经过这个方法跳过ATS,也就是说强制咱们用HTTPS,若是不这样的话提交App可能会被拒绝。可是咱们能够经过NSExceptionDomains来针对特定的域名开放HTTP能够容易经过审核。

NSExceptionDomains方式 设置域。能够简单理解成,把不支持https协议的接口设置成http的接口。

具体方法:

1)、在项目的info.plist中添加一个Key:App Transport Security Settings,类型为字典类型。

2)、而后给它添加一个Exception Domains,类型为字典类型;

3)、把须要的支持的域添加給Exception Domains。其中域做为Key,类型为字典类型。

4)、每一个域下面须要设置3个属性:NSIncludesSubdomains、NSExceptionRequiresForwardSecrecy、NSExceptionAllowsInsecureHTTPLoads。

如图:

细节提示:在iOS9之后的系统中若是使用到网络图片,也要注意网络图片是不是HTTP的哦,若是是,也要把图片的域设置哦!

3.iOS 10 隐私权限设置

iOS 10 开始对隐私权限更加严格,若是你不设置就会直接崩溃,如今不少遇到崩溃问题了,通常解决办法都是在info.plist文件添加对应的Key-Value就能够了。

 

以上Value值,圈出的红线部分的文字是展现给用户看的,必须添加。

4.Xcode 8 运行一堆没用的logs解决办法

 

上图咱们看到,本身新建的一个工程啥也没干就打印一堆烂七八糟的东西,我以为这个应该是Xcode 8的问题,

具体也没细研究,解决办法是设置OS_ACTIVITY_MODE : disable以下图:

 

第一步:

第二步:

 

第三步:

添加参数:

Name :OS_ACTIVITY_MODE 

Value :  disable

在Environment Variables中添加DYLD_PRINT_STATISTICS字段,并设置为YES,在控制台就会打印加载时长。

 

5.iOS 10 UIStatusBar方法过时:

 

在咱们开发中有可能用到UIStatusBar一些属性,在iOS 10 中这些方法已通过期了,若是你的项目中有用的话就得须要适配。

上面的图片也能发现,若是在iOS 10中你须要使用preferredStatusBar好比这样:

//iOS 10 - (UIStatusBarStyle)preferredStatusBarStyle {    return UIStatusBarStyleDefault;
}

 

6.iOS 10 UICollectionView 性能优化

随着开发者对UICollectionView的信赖,项目中用的地方也比较多,可是仍是存在一些问题,好比有时会卡顿、加载慢等。因此iOS 10 对UICollectionView进一步的优化。

  • UICollectionView cell pre-fetching预加载机制
  • UICollectionView and UITableView prefetchDataSource 新增的API
  • 针对self-sizing cells 的改进
  • Interactive reordering

  在iOS 10 以前,UICollectionView上面若是有大量cell,当用户活动很快的时候,整个UICollectionView的卡顿会很明显,为何会形成这样的问题,这里涉及到了iOS 系统的重用机制,当cell准备加载进屏幕的时候,整个cell都已经加载完成,等待在屏幕外面了,也就是整整一行cell都已经加载完毕,这就是形成卡顿的主要缘由,专业术语叫作:掉帧.
要想让用户感受不到卡顿,咱们的app必须帧率达到60帧/秒,也就是说每帧16毫秒要刷新一次.

  iOS 10 以前UICollectionViewCell的生命周期是这样的:
  • 1.用户滑动屏幕,屏幕外有一个cell准备加载进来,把cell从reusr队列拿出来,而后调用prepareForReuse方法,在这个方法里面,能够重置cell的状态,加载新的数据;
  • 2.继续滑动,就会调用cellForItemAtIndexPath方法,在这个方法里面给cell赋值模型,而后返回给系统;
  • 3.当cell立刻进去屏幕的时候,就会调用willDisplayCell方法,在这个方法里面咱们还能够修改cell,为进入屏幕作最后的准备工做;
  • 4.执行完willDisplayCell方法后,cell就进去屏幕了.当cell彻底离开屏幕之后,会调用didEndDisplayingCell方法.
  iOS 10 UICollectionViewCell的生命周期是这样的:
  • 1.用户滑动屏幕,屏幕外有一个cell准备加载进来,把cell从reusr队列拿出来,而后调用prepareForReuse方法,在这里当cell尚未进去屏幕的时候,就已经提早调用这个方法了,对比以前的区别是以前是cell的上边缘立刻进去屏幕的时候就会调用该方法,而iOS 10 提早到cell还在屏幕外面的时候就调用;
  • 2.在cellForItemAtIndexPath中建立cell,填充数据,刷新状态等操做,相比于以前也提早了;
  • 3.用户继续滑动的话,当cell立刻就须要显示的时候咱们再调用willDisplayCell方法,原则就是:什么时候须要显示,什么时候再去调用willDisplayCell方法;
  • 4.当cell彻底离开屏幕之后,会调用didEndDisplayingCell方法,跟以前同样,cell会进入重用队列.
    在iOS 10 以前,cell只能从重用队列里面取出,再走一遍生命周期,并调用cellForItemAtIndexPath建立或者生成一个cell.
    在iOS 10 中,系统会cell保存一段时间,也就是说当用户把cell滑出屏幕之后,若是又滑动回来,cell不用再走一遍生命周期了,只须要调用willDisplayCell方法就能够从新出如今屏幕中了.
    iOS 10 中,系统是一个一个加载cell的,二之前是一行一行加载的,这样就能够提高不少性能;
    iOS 10 新增长的Pre-Fetching预加载
    这个是为了下降UICollectionViewCell在加载的时候所花费的时间,在 iOS 10 中,除了数据源协议和代理协议外,新增长了一个UICollectionViewDataSourcePrefetching协议,这个协议里面定义了两个方法:
  - (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0);   - (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0);

   在ColletionView prefetchItemsAt indexPaths这个方法是异步预加载数据的,当中的indexPaths数组是有序的,就是item接收数据的顺序;
   CollectionView cancelPrefetcingForItemsAt indexPaths这个方法是可选的,能够用来处理在滑动中取消或者下降提早加载数据的优先级.
   注意:这个协议并不能代替以前读取数据的方法,仅仅是辅助加载数据.
   Pre-Fetching预加载对UITableViewCell一样适用.


 

7.iOS 10 UIColor 新增方法

如下是官方文档的说明:

Most graphics frameworks throughout the system, including Core Graphics, Core Image, Metal, and AVFoundation, have substantially improved support for extended-range pixel formats and wide-gamut color spaces. By extending this behavior throughout the entire graphics stack, it is easier than ever to support devices with a wide color display. In addition, UIKit standardizes on working in a new extended sRGB color space, making it easy to mix sRGB colors with colors in other, wider color gamuts without a significant performance penalty.

Here are some best practices to adopt as you start working with Wide Color.

  • In iOS 10, the UIColor class uses the extended sRGB color space and its initializers no longer clamp raw component values to between 0.0 and 1.0. If your app relies on UIKit to clamp component values (whether you’re creating a color or asking a color for its component values), you need to change your app’s behavior when you link against iOS 10.

  • When performing custom drawing in a UIView on an iPad Pro (9.7 inch), the underlying drawing environment is configured with an extended sRGB color space.

  • If your app renders custom image objects, use the new UIGraphicsImageRenderer class to control whether the destination bitmap is created using an extended-range or standard-range format.

  • If you are performing your own image processing on wide-gamut devices using a lower level API, such as Core Graphics or Metal, you should use an extended range color space and a pixel format that supports 16-bit floating-point component values. When clamping of color values is necessary, you should do so explicitly.

  • Core Graphics, Core Image, and Metal Performance Shaders provide new options for easily converting colors and images between color spaces.

由于以前咱们都是用RGB来设置颜色,反正用起来也不是特别多样化,此次新增的方法应该就是一个弥补吧。因此在iOS 10 苹果官方建议咱们使用sRGB,由于它性能更好,色彩更丰富。若是你本身为UIColor写了一套分类的话也可尝试替换为sRGBUIColor类中新增了两个Api以下:





+ (UIColor *)colorWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0); - (UIColor *)initWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0);

8.iOS 10 UITextContentType

// The textContentType property is to provide the keyboard with extra information about the semantic intent of the text document.@property(nonatomic,copy) UITextContentType textContentType NS_AVAILABLE_IOS(10_0); // default is nil

在iOS 10 UITextField添加了textContentType枚举,指示文本输入区域所指望的语义意义。

使用此属性能够给键盘和系统信息,关于用户输入的内容的预期的语义意义。例如,您能够指定一个文本字段,用户填写收到一封电子邮件确认uitextcontenttypeemailaddress。当您提供有关您指望用户在文本输入区域中输入的内容的信息时,系统能够在某些状况下自动选择适当的键盘,并提升键盘修正和主动与其余文本输入机会的整合。

 

9.iOS 10 字体随着手机系统字体而改变

当咱们手机系统字体改变了以后,那咱们Applabel也会跟着一块儿变化,这须要咱们写不少代码来进一步处理才能实现,可是iOS 10 提供了这样的属性adjustsFontForContentSizeCategory来设置。由于没有真机,具体实际操做还没去实现,若是理解错误帮忙指正。

 UILabel *myLabel = [UILabel new];   /*
    UIFont 的preferredFontForTextStyle: 意思是指定一个样式,并让字体大小符合用户设定的字体大小。
   */
    myLabel.font =[UIFont preferredFontForTextStyle: UIFontTextStyleHeadline]; /*
 Indicates whether the corresponding element should automatically update its font when the device’s UIContentSizeCategory is changed.
 For this property to take effect, the element’s font must be a font vended using +preferredFontForTextStyle: or +preferredFontForTextStyle:compatibleWithTraitCollection: with a valid UIFontTextStyle.
 */
     //是否更新字体的变化
    myLabel.adjustsFontForContentSizeCategory = YES;

 

10.iOS 10 UIScrollView新增refreshControl

 

iOS 10 之后只要是继承UIScrollView那么就支持刷新功能:




@property (nonatomic, strong, nullable) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(10_0) __TVOS_PROHIBITED;

11.iOS 10 判断系统版本正确姿式

判断系统版本是咱们常常用到的,尤为是如今你们都有可能须要适配iOS 10,那么问题就出现了,以下图:

咱们获得了答案是:

//值为 1 [[[[UIDevice currentDevice] systemVersion] substringToIndex:1] integerValue]

//值为10.000000 [[UIDevice currentDevice] systemVersion].floatValue,

//值为10.0 [[UIDevice currentDevice] systemVersion]

因此说判断系统方法最好仍是用后面的两种方法,哦~我忘记说了[[UIDevice currentDevice] systemVersion].floatValue这个方法也是不靠谱的,好像在8.3版本输出的值是8.2,记不清楚了反正是不靠谱的,因此建议你们用[[UIDevice currentDevice] systemVersion]这个方法!

Swift判断以下:

 if #available(iOS 10.0, *) {
            // iOS 10.0
            print("iOS 10.0");
        } else { }

12.Xcode 8 插件不能用的问题

你们都升级了Xcode 8,可是对于插件依赖的开发者们,一边哭着一边去网上寻找解决办法。那么下面是解决办法:
让你的 Xcode8 继续使用插件(http://vongloo.me/2016/09/10/Make-Your-Xcode8-Great-Again/?utm_source=tuicool&utm_medium=referral )

可是看到文章最后的解释,咱们知道若是用插件的话,可能安全上会有问题、而且提交审核会被拒绝,因此建议你们仍是不要用了,解决办法老是有的,好比在Xcode中添加注释的代码块也是很方便的。

 

13.iOS 10开始项目中有的文字显示不全问题

我用Xcode 8 和Xcode 7.3分别测试了下,以下图:

Xcode 8



Xcode7

建立一个Label而后让它自适应大小,字体大小都是17最后输出的宽度是不同的,咱们再看一下,
下面的数据就知道为何升级iOS 10 以后App中有的文字显示不全了:



英文字母会不会也有这种问题,我又经过测试,后来发现英文字母没有问题,只有汉字有问题。
目前只有一个一个修改控件解决这个问题,暂时没有其余好办法来解决。

Label17iOS 10App

14.Xcode 8使用Xib awakeFromNib的警告问题

Xcode 8以前咱们使用Xib初始化- (void)awakeFromNib {}都是这么写也没什么问题,可是在Xcode 8会有以下警告:

官方解释:
You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require.
Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations.
You may call the super implementation at any point during your own awakeFromNib method.


你必须调用父类实现awakeFromNib来给父类来执行它们须要的任何额外的初始化的机会。
虽然这种方法的默认实现不作任何事情,许多UIKit类提供非空的实现。
你能够调用本身的awakeFromNib方法中的任什么时候候超级实现。

1五、推送的时候,开启Remote notifications

You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:],
but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.

解决方案:须要在Xcode 中修改应用的 Capabilities 开启Remote notifications,请参考下图:


1六、One of the two will be used. Which one is undefined.”

  objc[5114]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x1109a5910) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x110738210). One of the two will be used. Which one is undefined.

  在模拟器中、发现“One of the two will be used. Which one is undefined.”日志

  查找资料发现缘由:objc runtime 对所用app使用同一个命名空间(flat namespace),运行机制以下:
  1. 首先二进制映像被加载,检查程序依赖关系
  2. 每个二进制映像被加载的同时,程序的objc classes在objc runtime命名空间中注册
  3. 若是具备相同名称的类被再次加载,objc runtime的行为是不可预知的。一种可能的状况是任意一个程序的该类会被加载(这应该也是默认动做)
1七、Invalid Bundle - The asset catalog at 'Payload/XXXXX/Assets.car' 
can't contain 16-bit or P3 assets if the app supports iOS 9.3 or earlier


在 Xcode 8 中,当你资源文件中[含有16位图]或者[图片显示模式γ值为'P3']且iOS targets设定为iOS 9.3如下就会出现这个问题. 若是你的app须要支持广色域显示的话,那你必须得把target设置成iOS 9.3+,相反,若是你的app不须要支持广色域且你想兼容 iOS 9.3 以前的项目,你就得把全部的16位的或者显示模式为'P3'图片全都替换成8位模式的SRGB颜色的图片。

 

你能够经过运行“assetutil”在iTunes Connect的错误信息中找到16-bit 或 P3 资源文件。离线的解决方案以下:

1.导出项目的 ipa 文件

2.定位到该ipa文件修改后缀名.ipa 为 .zip.

3. 解压该 .zip 文件. 解压后的目录里面会有一个包含着你的 app bundle 文件的 Payload 文件夹.

4. 打开终端病切换到你的app的Payload文件夹下的 .app bundle 文件夹内,形式以下:

cd path/to/Payload/your.app

5. 用 find 命令定位到 Assets.car 文件 .app bundle , 形式以下:

find . -name 'Assets.car'

6. 使用 assetutil 命令找到任何包含着 16-bit or P3 的资源文件, 对每一个 Assets.car 之行如下命令 :

sudo xcrun --sdk iphoneos assetutil --info /path/to/a/Assets.car > /tmp/Assets.json

7.  打开上一步生成的 /tmp/Assets.json 文件并查找包含有 “DisplayGamut": “P3” 或者相关的内容.  这段json的"Name"字段对应的值就是16位或显示的γ值为P3的资源文件名.


8.  找到这个资源文件修改成 8位的sRGB形式,从新编译上传你的app便可. 


1八、This version does not support documents saved in the Xcode 8 format. Open this document with Xcode 8 or later

  编辑项目时默认使用Xcode8打开,致使我用Xcode7打开Xib是报错:

This version does not support documents saved in the Xcode 8 format. Open this document with Xcode 8.0 or later



   致使用Xcode8打开的Xib所有打不开,只能用编辑器将Xib里面的下面一句话删除掉才能打开:

<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>未完待续,持续更新~