Xcode8的那些坑及iOS10的適配

客官,您里邊請~

前言:

在這說點(diǎn)題外話,現(xiàn)在到處都在說iOS寒冬來了,朋友也好、公眾號、新聞都在反應(yīng)一個情況,就是iOS工作不好找。確實(shí),我知道的三個朋友今年找iOS都花了差不多兩三個月。所以,在我開始找工作的時候,內(nèi)心還是極其擔(dān)憂的,不過,感覺自己很幸運(yùn),兩天半面了四家,拿了三個offer。不過,也深深感受到招聘者的苦惱和iOS應(yīng)聘者的尷尬處境。有的招聘方招一倆月都找不到一個合適的程序員,而程序員又都覺得他們要求太高或者同行太多,行情不好。各有各的擔(dān)憂吧,但記住一句話,只要有實(shí)力就不要怕,現(xiàn)在iOS行業(yè)正在洗盤,堅(jiān)持不了的、技術(shù)不好的都有可能被洗掉。所以,在我們杞人憂天的時候,我想還不如多提升一下自己。
好吧,講了這么多廢話,下面進(jìn)入主題,一進(jìn)公司,給分配了一個11年產(chǎn)的老得mac Pro,當(dāng)時就有點(diǎn)不開心,還好,昨天給換了個新的。還要吐槽一下,公司的網(wǎng)真是龜速啊。下載東西就沒超過100kb!額,額,好像現(xiàn)在說的也不是正題。好吧,然后前幾天回家下了Xcode8,發(fā)現(xiàn)有不少坑啊,本想今天自己總結(jié)一下,發(fā)現(xiàn)自己的太不全面,所以就參考了網(wǎng)上一些大牛的(自己總結(jié)的不全面,所以還是搬運(yùn)大牛們的了)。重在學(xué)技術(shù)嘛。

Xcode8的那些坑

也不能說是坑吧,就一些新特性

  • 證書Signing

這個變化就比較明顯了,它可以幫助我們自動的來管理證書,但我感覺卻帶來的麻煩反而多了,操作的不對會出現(xiàn)各種不同的錯誤。Automatically manage signing這個選項(xiàng),建議勾選上哈。見網(wǎng)上有人總結(jié)的很全面,我就直接搬過來了。

1.Xcode未設(shè)置開發(fā)者賬號情況下的截圖


QQ20160913-0.png-38.5kB

解決辦法是:大家在Xcode的偏好設(shè)置中,添加蘋果賬號,即可。
2.設(shè)備機(jī)器未添加進(jìn)開發(fā)者的Device情況下的截圖


QQ20160913-2.png-33.7kB

解決辦法是:大家在官網(wǎng)將設(shè)備添加進(jìn)開發(fā)機(jī)后,陪下描述文件重新下個描述文件即可。
3.正常情況:Xcode配置登錄開發(fā)者賬號后的圖片,耐心等待即可。


QQ20160913-1.png-25.1kB

等待完成之后的圖


QQ20160913-3.png-27kB

Xib文件的注意事項(xiàng)

使用Xcode8打開xib文件后,會出現(xiàn)下圖的提示。


QQ20160913-9.png-41.7kB

大家選擇Choose Device即可。之后大家會發(fā)現(xiàn)布局啊,frame亂了,只需要更新一下frame即可。如下圖


QQ20160913-11.png-113.2kB

注意:如果按上面的步驟操作后,在用Xcode7打開Xib會報一下錯誤,

QQ20160913-12.png-32.3kB

解決辦法:需要刪除Xib里面<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>

這句話,以及把< document >中的toolsVersion和< plugIn >中的version改成你正常的xib文件中的值,不過不建議這么做。

代碼注釋不能用的解決辦法

這個是因?yàn)樘O果解決xcode ghost,把插件屏蔽了。
解決方法
打開終端,命令運(yùn)行: sudo /usr/libexec/xpccachectl
然后必須重啟電腦后生效。
VVDocumenter-Xcode無法使用的解決辦法:
使用快捷鍵:option + command + /

權(quán)限以及相關(guān)設(shè)置

注意,添加的時候,末尾不要有空格我們需要打開info.plist文件添加相應(yīng)權(quán)限的說明,否則程序在iOS10上會出現(xiàn)崩潰。具體如下圖:

QQ20160914-0.png

麥克風(fēng)權(quán)限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風(fēng)?
相機(jī)權(quán)限: Privacy - Camera Usage Description 是否允許此App使用你的相機(jī)?
相冊權(quán)限: Privacy - Photo Library Usage Description 是否允許此App訪問你的媒體資料庫?
通訊錄權(quán)限: Privacy - Contacts Usage Description 是否允許此App訪問你的通訊錄?
藍(lán)牙權(quán)限:Privacy - Bluetooth Peripheral Usage Description 是否許允此App使用藍(lán)牙?
語音轉(zhuǎn)文字權(quán)限:Privacy - Speech Recognition Usage Description 是否允許此App使用語音識別?
日歷權(quán)限:Privacy - Calendars Usage Description 是否允許此App使用日歷?
定位權(quán)限:Privacy - Location When In Use Usage Description 我們需要通過您的地理位置信息獲取您周邊的相關(guān)數(shù)據(jù)
定位權(quán)限: Privacy - Location Always Usage Description 我們需要通過您的地理位置信息獲取您周邊的相關(guān)數(shù)據(jù)定位的需要這么寫,防止上架被拒。

推送

如下圖的部分,不要忘記打開。所有的推送平臺,不管是極光還是什么的,要想收到推送,這個是必須打開的喲??


QQ20160914-4.png

之后就應(yīng)該可以收到推送了。另外,極光推送也推出新版本了,大家也可以更新下。
PS.蘋果這次對推送做了很大的變化,希望大家多查閱查閱,處理推送的代理方法也變化了。
// 推送的代理[<UNUserNotificationCenterDelegate>]

iOS10收到通知不再是在[application: didReceiveRemoteNotification:]
方法去處理, iOS10推出新的代理方法,接收和處理各類通知(本地或者遠(yuǎn)程)

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { //應(yīng)用在前臺收到通知 NSLog(@"========%@", notification);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { //點(diǎn)擊通知進(jìn)入應(yīng)用 NSLog(@"response:%@", response);}

屏蔽雜亂無章的bug

更新Xcode8之后,新建立工程,都會打印一堆莫名其妙看不懂的Log.如這些
subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1,

屏蔽的方法如下:Xcode8里邊 Edit Scheme-> Run -> Arguments, 在Environment Variables里邊添加OS_ACTIVITY_MODE = Disable


QQ20160914-8.png

如果寫了之后還是打印log,請重新勾選對勾,就可以解決了
Ps.考慮到添加上述內(nèi)容在Xcode8后,真機(jī)調(diào)試可能出現(xiàn)異常,大家可以自定義一個宏定義,來做日志輸出。

iOS10的適配

1.系統(tǒng)判斷方法失效:

在你的項(xiàng)目中,當(dāng)需要判斷系統(tǒng)版本的話,不要使用下面的方法:

#define isiOS10 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=10)

它會永遠(yuǎn)返回NO,substringToIndex:1
在iOS 10 會被檢測成 iOS 1了,應(yīng)該使用下面的這些方法:Objective-C 中這樣寫:

#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

或者使用:

if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){
.majorVersion = 9, 
.minorVersion = 1, 
.patchVersion = 0
}]) { 
   NSLog(@"Hello from > iOS 9.1");
}if ([NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){
   9,3,0
}]) {
   NSLog(@"Hello from > iOS 9.3");
}

或者使用:

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_0) { // do stuff for iOS 9 and newer} else { // do stuff for older versions than iOS 9}

有時候會缺少一些常量,NSFoundationVersionNumber是在NSObjCRuntime.h中定義的,作為Xcode7.3.1的一部分,我們設(shè)定常熟范圍從iPhone OS 2到#define NSFoundationVersionNumber_iOS_8_4 1144.17,在iOS 10(Xcode 8)中,蘋果補(bǔ)充了缺少的數(shù)字,設(shè)置有未來的版本.

#define NSFoundationVersionNumber_iOS_9_0 1240.1#define NSFoundationVersionNumber_iOS_9_1 1241.14#define NSFoundationVersionNumber_iOS_9_2 1242.12#define NSFoundationVersionNumber_iOS_9_3 1242.12#define NSFoundationVersionNumber_iOS_9_4 1280.25#define NSFoundationVersionNumber_iOS_9_x_Max 1299

Swift中這樣寫:

if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 10, minorVersion: 0, patchVersion: 0)) { // 代碼塊}

或者使用
if #available(iOS 10.0, *) { // 代碼塊} else { // 代碼塊}

2.隱私數(shù)據(jù)訪問問題:

你的項(xiàng)目中訪問了隱私數(shù)據(jù),比如:相機(jī),相冊,聯(lián)系人等,在Xcode8中打開編譯的話,統(tǒng)統(tǒng)會crash,控制臺會輸出下面這樣的日志:


Snip20160905_1.png

這是因?yàn)閕OS對用戶的安全和隱私的增強(qiáng),在申請很多私有權(quán)限的時候都需要添加描述,但是,在使用Xcode 8之前的Xcode還是使用系統(tǒng)的權(quán)限通知框.要想解決這個問題,只需要在info.plist
添加NSContactsUsageDescription
的key, value自己隨意填寫就可以,這里列舉出對應(yīng)的key(Source Code模式下):

<!-- 相冊 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能訪問相冊</string> 
<!-- 相機(jī) --> <key>NSCameraUsageDescription</key> <string>App需要您的同意,才能訪問相機(jī)</string> 
<!-- 麥克風(fēng) --> <key>NSMicrophoneUsageDescription</key> <string>App需要您的同意,才能訪問麥克風(fēng)</string> 
<!-- 位置 --> <key>NSLocationUsageDescription</key> <string>App需要您的同意,才能訪問位置</string> 
<!-- 在使用期間訪問位置 --><key>NSLocationWhenInUseUsageDescription</key> <string>App需要您的同意,才能在使用期間訪問位置</string> 
<!-- 始終訪問位置 --> <key>NSLocationAlwaysUsageDescription</key> <string>App需要您的同意,才能始終訪問位置</string> 
<!-- 日歷 --> <key>NSCalendarsUsageDescription</key> <string>App需要您的同意,才能訪問日歷</string> 
<!-- 提醒事項(xiàng) --> <key>NSRemindersUsageDescription</key> <string>App需要您的同意,才能訪問提醒事項(xiàng)</string> 
<!-- 運(yùn)動與健身 --> <key>NSMotionUsageDescription</key> <string>App需要您的同意,才能訪問運(yùn)動與健身</string> 
<!-- 健康更新 --> <key>NSHealthUpdateUsageDescription</key> <string>App需要您的同意,才能訪問健康更新 </string> 
<!-- 健康分享 --> <key>NSHealthShareUsageDescription</key> <string>App需要您的同意,才能訪問健康分享</string> 
<!-- 藍(lán)牙 --> <key>NSBluetoothPeripheralUsageDescription</key> <string>App需要您的同意,才能訪問藍(lán)牙</string> 
<!-- 媒體資料庫 --> <key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能訪問媒體資料庫</string>

如果不起作用,可以請求后臺權(quán)限,類似于這樣:
<key>UIBackgroundModes</key><array> <string>location</string>...</array>

或者在Xcode里選中當(dāng)前的target,選擇Capabilities,找到Background Modes,打開它,在里面選擇對應(yīng)權(quán)限


后臺模式的操作.png

3.UIColor的問題

官方文檔中說:大多數(shù)core
開頭的圖形框架和AVFoundation
都提高了對擴(kuò)展像素和寬色域色彩空間的支持.通過圖形堆棧擴(kuò)展這種方式比以往支持廣色域的顯示設(shè)備更加容易。現(xiàn)在對UIKit擴(kuò)展可以在sRGB的色彩空間下工作,性能更好,也可以在更廣泛的色域來搭配sRGB顏色.如果你的項(xiàng)目中是通過低級別的api自己實(shí)現(xiàn)圖形處理的,建議使用sRGB,也就是說在項(xiàng)目中使用了RGB轉(zhuǎn)化顏色的建議轉(zhuǎn)換為使用sRGB,在UIColor類中新增了兩個api:

-(UIColor *)initWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha 
+(UIColor *)colorWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha

色的顯示
真彩色的顯示會根據(jù)光感應(yīng)器來自動的調(diào)節(jié)達(dá)到特定環(huán)境下顯示與性能的平衡效果,如果需要這個功能的話,可以在info.plist
里配置(在Source Code模式下):<key>UIWhitePointAdaptivityStyle</key>
它有五種取值,分別是:

<string>UIWhitePointAdaptivityStyleStandard</string> // 標(biāo)準(zhǔn)模式<string>UIWhitePointAdaptivityStyleReading</string> // 閱讀模式<string>UIWhitePointAdaptivityStylePhoto</string> // 圖片模式<string>UIWhitePointAdaptivityStyleVideo</string> // 視頻模式<string>UIWhitePointAdaptivityStyleStandard</string> // 游戲模式

也就是說如果你的項(xiàng)目是閱讀類的,就選擇UIWhitePointAdaptivityStyleReading
這個模式,五種模式的顯示效果是從上往下遞減,也就是說如果你的項(xiàng)目是圖片處理類的,你選擇的是閱讀模式,給選擇太好的效果會影響性能.
**5.ATS的問題
1.在iOS 9的時候,默認(rèn)非HTTS的網(wǎng)絡(luò)是被禁止的,我們可以在info.plist
文件中添加NSAppTransportSecurity字典,將NSAllowsArbitraryLoads設(shè)置為YES來禁用ATS;
2.從2017年1月1日起,,所有新提交的 app 默認(rèn)不允許使用NSAllowsArbitraryLoads
來繞過ATS的限制,默認(rèn)情況下你的 app 可以訪問加密足夠強(qiáng)的(TLS V1.2以上)HTTPS內(nèi)容;
3.可以選擇使用NSExceptionDomains
設(shè)置白名單的方式對特定的域名開放HTTP內(nèi)容來通過審核,比如說你的應(yīng)用集成了第三方的登錄分享SDK,可以通過這種方式來做,下面以新浪SDK作為示范(Source Code 模式下):

<key>NSAppTransportSecurity</key> <dict>
<key>NSExceptionDomains</key> <dict> 
<key>sina.cn</key> <dict> <key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string> 
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
<false/> <key>NSIncludesSubdomains</key> <true/> </dict> 
<key>weibo.cn</key> <dict> 
<key>NSThirdPartyExceptionMinimumTLSVersion</key> 
<string>TLSv1.0</string> 
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
<false/> <key>NSIncludesSubdomains</key> <true/> </dict> 
<key>weibo. com</key> <dict> 
<key>NSThirdPartyExceptionMinimumTLSVersion</key> 
<string>TLSv1.0</string> 
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
<false/> <key>NSIncludesSubdomains</key> <true/> </dict> 
<key>sinaimg.cn</key> <dict> 
<key>NSThirdPartyExceptionMinimumTLSVersion</key> 
<string>TLSv1.0</string> 
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
<false/> <key>NSIncludesSubdomains</key> <true/> </dict> 
<key>sinajs.cn</key> <dict> 
<key>NSThirdPartyExceptionMinimumTLSVersion</key> 
<string>TLSv1.0</string> 
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
<false/> <key>NSIncludesSubdomains</key> <true/> </dict> 
<key>sina.com.cn</key> <dict> 
<key>NSThirdPartyExceptionMinimumTLSVersion</key> 
<string>TLSv1.0</string> 
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
<false/> <key>NSIncludesSubdomains</key> <true/> </dict> 
</dict> </dict>
4.在iOS 10 中info.plist

文件新加入了NSAllowsArbitraryLoadsInWebContent鍵,允許任意web頁面加載,同時蘋果會用 ATS 來保護(hù)你的app;

5.安全傳輸不再支持SSLv3, 建議盡快停用SHA1和3DES算法;
6.UIStatusBar的問題:

在iOS10中,如果還使用以前設(shè)置UIStatusBar類型或者控制隱藏還是顯示的方法,會報警告,方法過期,如下圖:


UIStatusBar的警告.png

上面方法到 iOS 10 不能使用了,要想修改UIStatusBar的樣式或者狀態(tài)使用下圖中所示的屬性或方法:

@property(nonatomic, readonly) UIStatusBarStyle preferredStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarStyleDefault@property(nonatomic, readonly) BOOL prefersStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to NO- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarStyleDefault- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to NO// Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden.- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarAnimationFade
7.UITextField

在iOS 10 中,UITextField
新增了textContentType
字段,是UITextContentType
類型,它是一個枚舉,作用是可以指定輸入框的類型,以便系統(tǒng)可以分析出用戶的語義.是電話類型就建議一些電話,是地址類型就建議一些地址.可以在#import <UIKit/UITextInputTraits.h>
文件中,查看textContentType
字段,有以下可以選擇的類型:

UIKIT_EXTERN UITextContentType const UITextContentTypeName NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeNamePrefix NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeGivenName NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeMiddleName NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeFamilyName NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeNameSuffix NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeNickname NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeJobTitle NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeOrganizationName NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeLocation NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeFullStreetAddress NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeStreetAddressLine1 NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeStreetAddressLine2 NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeAddressCity NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeAddressState NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeAddressCityAndState NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeSublocality NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeCountryName NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypePostalCode NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeTelephoneNumber NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeEmailAddress NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeURL NS_AVAILABLE_IOS(10_0);UIKIT_EXTERN UITextContentType const UITextContentTypeCreditCardNumber NS_AVAILABLE_IOS(10_0);
8.UserNotifications(用戶通知)

iOS 10 中將通知相關(guān)的 API 都統(tǒng)一了,在此基礎(chǔ)上很多用戶定義的通知,并且可以捕捉到各個通知狀態(tài)的回調(diào).以前通知的概念是:大家想接受的提前做好準(zhǔn)備,然后一下全兩分發(fā),沒收到也不管了,也不關(guān)心發(fā)送者,現(xiàn)在的用戶通知做成了類似于網(wǎng)絡(luò)請求,先發(fā)一個request得到response的流程,還封裝了error,可以在各個狀態(tài)的方法中做一些額外的操作,并且能獲得一些字段,比如發(fā)送者之類的.這個功能的頭文件是:#import <UserNotifications/UserNotifications.h>
主要有以下文件:

#import <UserNotifications/NSString+UserNotifications.h>
#import <UserNotifications/UNError.h>
#import <UserNotifications/UNNotification.h>
#import <UserNotifications/UNNotificationAction.h>
#import <UserNotifications/UNNotificationAttachment.h>
#import <UserNotifications/UNNotificationCategory.h>
#import <UserNotifications/UNNotificationContent.h>
#import <UserNotifications/UNNotificationRequest.h>
#import <UserNotifications/UNNotificationResponse.h>
#import <UserNotifications/UNNotificationSettings.h>
#import <UserNotifications/UNNotificationSound.h>
#import <UserNotifications/UNNotificationTrigger.h>
#import <UserNotifications/UNUserNotificationCenter.h>
#import <UserNotifications/UNNotificationServiceExtension.h>
9.UICollectionViewCell的的優(yōu)化

在iOS 10 之前,UICollectionView上面如果有大量cell,當(dāng)用戶活動很快的時候,整個UICollectionView的卡頓會很明顯,為什么會造成這樣的問題,這里涉及到了iOS 系統(tǒng)的重用機(jī)制,當(dāng)cell準(zhǔn)備加載進(jìn)屏幕的時候,整個cell都已經(jīng)加載完成,等待在屏幕外面了,也就是整整一行cell都已經(jīng)加載完畢,這就是造成卡頓的主要原因,專業(yè)術(shù)語叫做:掉幀.要想讓用戶感覺不到卡頓,我們的app必須幀率達(dá)到60幀/秒,也就是說每幀16毫秒要刷新一次.
iOS 10 之前UICollectionViewCell的生命周期是這樣的:
1.用戶滑動屏幕,屏幕外有一個cell準(zhǔn)備加載進(jìn)來,把cell從reusr隊(duì)列拿出來,然后調(diào)用prepareForReuse
方法,在這個方法里面,可以重置cell的狀態(tài),加載新的數(shù)據(jù);
2.繼續(xù)滑動,就會調(diào)用cellForItemAtIndexPath
方法,在這個方法里面給cell賦值模型,然后返回給系統(tǒng);
3.當(dāng)cell馬上進(jìn)去屏幕的時候,就會調(diào)用willDisplayCell
方法,在這個方法里面我們還可以修改cell,為進(jìn)入屏幕做最后的準(zhǔn)備工作;
4.執(zhí)行完willDisplayCell
方法后,cell就進(jìn)去屏幕了.當(dāng)cell完全離開屏幕以后,會調(diào)用didEndDisplayingCell
方法.

iOS 10 UICollectionViewCell的生命周期是這樣的:
1.用戶滑動屏幕,屏幕外有一個cell準(zhǔn)備加載進(jìn)來,把cell從reusr隊(duì)列拿出來,然后調(diào)用prepareForReuse
方法,在這里當(dāng)cell還沒有進(jìn)去屏幕的時候,就已經(jīng)提前調(diào)用這個方法了,對比之前的區(qū)別是之前是cell的上邊緣馬上進(jìn)去屏幕的時候就會調(diào)用該方法,而iOS 10 提前到cell還在屏幕外面的時候就調(diào)用;
2.在cellForItemAtIndexPath
中創(chuàng)建cell,填充數(shù)據(jù),刷新狀態(tài)等操作,相比于之前也提前了;
3.用戶繼續(xù)滑動的話,當(dāng)cell馬上就需要顯示的時候我們再調(diào)用willDisplayCell
方法,原則就是:何時需要顯示,何時再去調(diào)用willDisplayCell
方法;
4.當(dāng)cell完全離開屏幕以后,會調(diào)用didEndDisplayingCell
方法,跟之前一樣,cell會進(jìn)入重用隊(duì)列.在iOS 10 之前,cell只能從重用隊(duì)列里面取出,再走一遍生命周期,并調(diào)用cellForItemAtIndexPath
創(chuàng)建或者生成一個cell.在iOS 10 中,系統(tǒng)會cell保存一段時間,也就是說當(dāng)用戶把cell滑出屏幕以后,如果又滑動回來,cell不用再走一遍生命周期了,只需要調(diào)用willDisplayCell
方法就可以重新出現(xiàn)在屏幕中了.iOS 10 中,系統(tǒng)是一個一個加載cell的,二以前是一行一行加載的,這樣就可以提升很多性能;iOS 10 新增加的Pre-Fetching預(yù)加載
這個是為了降低UICollectionViewCell在加載的時候所花費(fèi)的時間,在 iOS 10 中,除了數(shù)據(jù)源協(xié)議和代理協(xié)議外,新增加了一個UICollectionViewDataSourcePrefetching
協(xié)議,這個協(xié)議里面定義了兩個方法:

- (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
這個方法是異步預(yù)加載數(shù)據(jù)的,當(dāng)中的indexPaths
數(shù)組是有序的,就是item接收數(shù)據(jù)的順序;CollectionView cancelPrefetcingForItemsAt indexPaths
這個方法是可選的,可以用來處理在滑動中取消或者降低提前加載數(shù)據(jù)的優(yōu)先級.注意:這個協(xié)議并不能代替之前讀取數(shù)據(jù)的方法,僅僅是輔助加載數(shù)據(jù).Pre-Fetching預(yù)加載對UITableViewCell同樣適用.

10. UIRefreshControl的使用

在iOS 10 中, UIRefreshControl可以直接在UICollectionView和UITableView中使用,并且脫離了UITableViewController.現(xiàn)在RefreshControl是UIScrollView的一個屬性.使用方法:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged]; collectionView.refreshControl = refreshControl;

文/Eternaldream(簡書作者)原文鏈接:http://www.itdecent.cn/p/f8151d556930
文/徐不同(簡書作者)原文鏈接:http://www.itdecent.cn/p/9756992a35ca

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容