记录对iOS10的适配工作ʕ •ᴥ•ʔ

iOS10正式版开始推送,赶着ddl做的iOS10适配工作也算是完成了,这里记录下iOS10适配中做的工作。

仅是适配,不包括利用iOS10新特性做的新功能。做的工作有这样几个方面:

  • Swift转换
  • Today extension UI适配
  • entitlements文件增加aps
  • info.plist增加权限字段
  • UITableView生命周期适配
  • pbxproj文件适配证书部分
  • IDFA可被用户禁用

现在依次来介绍一下


Swift转换



使用Xcode8 beta打开项目后,Xcode8会提示转换项目中的Swift代码。

按照Xcode的提示,转换至Swift 2.3就ok了。转换过程中,Xcode会自动更新pbxproj中纪录的Swift版本。

这部分比较顺利,没有遇到什么坑。


Today extension UI适配



iOS10对today extension做了一些调整,主要体现在:

  • 视觉调整,背景色变浅,去除了左侧margin
  • 支持“展开”和“收起”操作,展开和收起两种状态分别有最高高度限制
视觉调整



用Xcode8构建的app,可以看到today extension的背景色,由深灰色变成了浅灰色,左侧的margin也取消了。

支持展开和收起操作



iOS10中,today extension支持展开/收起操作,收起时视图高度固定,展开时视图高度可变。

iOS10中,NCWidgetProviding协议新增了一个回调:

// If implemented, called when the active display mode changes.
// The widget may wish to change its preferredContentSize to better accommodate the new display mode.
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize NS_AVAILABLE_IOS(10_0);

用户点击展开/收起后,这个方法会调用。注释中建议在这个方法中设置today extension的preferredContentSize

展开和收起模式下高度的限制可以通过

- (CGSize)widgetMaximumSizeForDisplayMode:(NCWidgetDisplayMode)displayMode

方法获得。

有人讨论说,收起模式下调整高度无效( How to resize the height of widget in iOS 10? ),个人理解NCWidgetDisplayMode的注释中,已经表示了收起模式下的视图高度是固定的:

typedef NS_ENUM(NSInteger, NCWidgetDisplayMode) {
    NCWidgetDisplayModeCompact, // Fixed height
    NCWidgetDisplayModeExpanded, // Variable height
} NS_ENUM_AVAILABLE_IOS(10_0);

展开模式下,不同机型/横竖屏,视图的最高高度限制也都是不同的。比如iPhone SE下测试,竖屏最大高度为396,iPhone 6s下测试,竖屏最大高度为528。


entitlements文件增加aps



升级到Xcode8后,发现构建的包,在注册remote notification的,获取device token的时候失败了,错误信息为:

no valid 'aps-environment' 
entitlement string found for application

原来在Xcode7中,push notifications开关只有一个步骤:将push notifications加入到app id。

但是Xcode8中,打开push notifications开关有两个步骤:
多了一步写entitlements文件的步骤

所以,用Xcode8打开工程后,应该重新打开一次push notification开关,之后entitlements文件中会多出两行:

aps-environment
development

有了这两行之后,才可以正常注册device token。

但是为什么这个值是development呢?难道正式上线前不需要改为production么?

神奇的是,如果用

codesign --display --entitlements :- ./myApp.app

命令,可以看到app文件内的entitlement。如果用production证书签名,可以看到:

aps-environment
production

可能是苹果在签名过程中,会自动更新aps-environment字段?

网上发现的一个关于这个问题的讨论
和苹果的一个replyXcode 8 Beta: aps-environment entitlement is missing


Info.plist增加权限字段



使用Xcode8构建的app,在使用相册等权限时会崩溃。错误信息为:

This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

官方文档中解释:

Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and which accesses the user’s photo library, must statically declare the intent to do so. Include the NSPhotoLibraryUsageDescription key in your app’s Info.plist file and provide a purpose string for this key. If your app attempts to access the user’s photo library without a corresponding purpose string, your app exits.

必须在Info.plist文件中增加NSPhotoLibraryUsageDescription键,并且填入合适的描述文本

还有很多其他的权限,如相机等,也需要在Info.plist文件中声明,具体可参考官方文档。

有一个坑就是,如果string的值为空,这行权限的声明就会无效,遇到的时候还是崩溃:

NSPhotoLibraryUsageDescription


UITableView、UICollectionView生命周期适配



这章WWDC介绍了UITableViewUICollectionView的生命周期在iOS10中发生了改变。

在iOS9及之前,一个UICollectionViewCell被重用的过程是这样的:

  • 一个cell即将出现的屏幕上
  • prepareForReuse
  • cellForItemAtIndexPath
  • willDisplayCell
  • ……
  • 一个cell完全离开屏幕
  • didEndDisplayCell
  • 进入reuse queue

但在iOS10中,一个UICollectionViewCell被重用的过程变成了这样:

  • 一个cell还没出现在屏幕上
  • prepareForReuse
  • cellForItemAtIndexPath
  • 一个cell即将出现的屏幕上
  • ……
  • 一个cell完全离开屏幕
  • didEndDisplayCell
  • 一段时间后再进入reuse queue

所以,iOS10后,cellForItemAtIndexPath被调用时,并不表示这个cell即将要出现在了屏幕上。还是得老老实实使用willDisplayCelldidEndDisplayCell方法。


pbxproj文件适配证书部分



Xcode8支持自动管理配置文件,按照文档,只需要勾选Automatically manage signing,并配置Code Signing Identity为iOS Developer,Provisioning Profile为Automatic就可以了。

官方文档在这里:
How do I configure my Xcode project to use automatic provisioning?


IDFA可被用户禁用



iOS10中,用户可以在设置-隐私-广告-限制广告追踪中禁止app读取IDFA,这时app读取到的IDFA就是一串0。

官方文档中这样说:

In iOS 10.0 and later, the value of advertisingIdentifier is all zeroes when the user has limited ad tracking.

无法读取IDFA的问题,看来暂时难以解决,求支招(╥﹏╥)

你可能感兴趣的:(记录对iOS10的适配工作ʕ •ᴥ•ʔ)