隨著iOS10發布的鄰近,大家的App都需要適配iOS10,下面是我總結的1些關于iOS10適配方面的問題,如果有毛病,歡迎指出.
1.系統判斷方法失效:
在你的項目中,當需要判斷系統版本的話,不要使用下面的方法:
#define isiOS10 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=10)
它會永久返回
NO
,substringToIndex:1
在iOS 10 會被檢測成 iOS 1了,
應當使用下面的這些方法:
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}
有時候會缺少1些常量,
NSFoundationVersionNumber
是在NSObjCRuntime.h
中定義的,作為Xcode7.3.1的1部份,我們設定常熟范圍從iPhone OS 2到#define NSFoundationVersionNumber_iOS_8_4 1144.17
,在iOS 10(Xcode 8)中,蘋果補充了缺少的數字,設置有未來的版本.#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 {
// 代碼塊
}
你的項目中訪問了隱私數據,比如:相機,相冊,聯系人等,在Xcode8中打開編譯的話,統統會crash,控制臺會輸出下面這樣的日志:
這是由于iOS對用戶的安全和隱私的增強,在申請很多私有權限的時候都需要添加描寫,但是,在使用Xcode 8之前的Xcode還是使用系統的權限通知框.
要想解決這個問題,只需要在info.plist
添加NSContactsUsageDescription
的key, value自己隨便填寫就能夠,這里羅列出對應的key(Source Code模式下):
<!-- 相冊 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能訪問相冊</string>
<!-- 相機 -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能訪問相機</string>
<!-- 麥克風 -->
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能訪問麥克風</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>
<!-- 提示事項 -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能訪問提示事項</string>
<!-- 運動與健身 -->
<key>NSMotionUsageDescription</key> <string>App需要您的同意,才能訪問運動與健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能訪問健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能訪問健康分享</string>
<!-- 藍牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能訪問藍牙</string>
<!-- 媒體資料庫 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能訪問媒體資料庫</string>
如果不起作用,可以要求后臺權限,類似于這樣:
<key>UIBackgroundModes</key>
<array>
<!-- 在這里寫上你在后臺模式下要使用權限對應的key -->
<string>location</string>
...
</array>
或在Xcode里選中當前的target
,選擇Capabilities
,找到Background Modes
,打開它,在里面選擇對應權限
官方文檔中說:大多數
core
開頭的圖形框架和AVFoundation
都提高了對擴大像素和寬色域色采空間的支持.通過圖形堆棧擴大這類方式比以往支持廣色域的顯示裝備更加容易。現在對UIKit擴大可以在sRGB的色采空間下工作,性能更好,也能夠在更廣泛的色域來搭配sRGB色彩.如果你的項目中是通太低級別的api自己實現圖形處理的,建議使用sRGB,也就是說在項目中使用了RGB轉化色彩的建議轉換為使用sRGB,在UIColor
類中新增了兩個api:- (UIColor *)initWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0); + (UIColor *)colorWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0);
4.真彩色的顯示
真彩色的顯示會根據光感應器來自動的調理到達特定環境下顯示與性能的平衡效果,如果需要這個功能的話,可以在
info.plist
里配置(在Source Code模式下):<key>UIWhitePointAdaptivityStyle</key>
它有5種取值,分別是:
<string>UIWhitePointAdaptivityStyleStandard</string> // 標準模式 <string>UIWhitePointAdaptivityStyleReading</string> // 瀏覽模式 <string>UIWhitePointAdaptivityStylePhoto</string> // 圖片模式 <string>UIWhitePointAdaptivityStyleVideo</string> // 視頻模式 <string>UIWhitePointAdaptivityStyleStandard</string> // 游戲模式
也就是說如果你的項目是瀏覽類的,就選擇
UIWhitePointAdaptivityStyleReading
這個模式,5種模式的顯示效果是從上往下遞減,也就是說如果你的項目是圖片處理類的,你選擇的是瀏覽模式,給選擇太好的效果會影響性能.5.ATS的問題
1.在iOS 9的時候,默許非HTTS的網絡是被制止的,我們可以在
info.plist
文件中添加NSAppTransportSecurity
字典,將NSAllowsArbitraryLoads
設置為YES
來禁用ATS;
2.從2017年1月1日起,,所有新提交的 app 默許不允許使用NSAllowsArbitraryLoads
來繞過ATS的限制,默許情況下你的 app 可以訪問加密足夠強的(TLS V1.2以上)HTTPS內容;
3.可以選擇使用NSExceptionDomains
設置白名單的方式對特定的域名開放HTTP內容來通過審核,比如說你的利用集成了第3方的登錄分享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 來保護你的app;
5.安全傳輸不再支持SSLv3
, 建議盡快停用SHA1
和3DES
算法;6.UIStatusBar的問題:
在iOS10中,如果還使用之前設置UIStatusBar類型或控制隱藏還是顯示的方法,會報正告,方法過期,以下圖:
UIStatusBar的正告.png
上面方法到 iOS 10 不能使用了,要想修改UIStatusBar的樣式或狀態使用下圖中所示的屬性或方法:@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
類型,它是1個枚舉,作用是可以指定輸入框的類型,以便系統可以分析出用戶的語義.是電話類型就建議1些電話,是地址類型就建議1些地址.可以在#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 中將通知相干的 API 都統1了,在此基礎上很多用戶定義的通知,并且可以捕捉到各個通知狀態的回調.之前通知的概念是:大家想接受的提早做好準備,然后1下全兩分發,沒收到也不管了,也不關心發送者,現在的用戶通知做成了類似于網絡要求,先發1個
request
得到response
的流程,還封裝了error
,可以在各個狀態的方法中做1些額外的操作,并且能取得1些字段,比如發送者之類的.這個功能的頭文件是:#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>
在iOS 10 之前,UICollectionView上面如果有大量cell,當用戶活動很快的時候,全部UICollectionView的卡頓會很明顯,為何會造成這樣的問題,這里觸及到了iOS 系統的重用機制,當cell準備加載進屏幕的時候,全部cell都已加載完成,等待在屏幕外面了,也就是整整1行cell都已加載終了,這就是造成卡頓的主要緣由,專業術語叫做:掉幀.
要想讓用戶感覺不到卡頓,我們的app必須幀率到達60幀/秒,也就是說每幀16毫秒要刷新1次.
prepareForReuse
方法,在這個方法里面,可以重置cell的狀態,加載新的數據;cellForItemAtIndexPath
方法,在這個方法里面給cell賦值模型,然后返回給系統;willDisplayCell
方法,在這個方法里面我們還可以修改cell,為進入屏幕做最后的準備工作;willDisplayCell
方法后,cell就進去屏幕了.當cell完全離開屏幕以后,會調用didEndDisplayingCell
方法.prepareForReuse
方法,在這里當cell還沒有進去屏幕的時候,就已提早調用這個方法了,對照之前的區分是之前是cell的上邊沿馬上進去屏幕的時候就會調用該方法,而iOS 10 提早到cell還在屏幕外面的時候就調用;cellForItemAtIndexPath
中創建cell,填充數據,刷新狀態等操作,相比于之前也提早了;willDisplayCell
方法,原則就是:什么時候需要顯示,什么時候再去調用willDisplayCell
方法;didEndDisplayingCell
方法,跟之前1樣,cell會進入重用隊列.cellForItemAtIndexPath
創建或生成1個cell.willDisplayCell
方法就能夠重新出現在屏幕中了.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一樣適用.
在iOS 10 中, UIRefreshControl可以直接在UICollectionView和UITableView中使用,并且脫離了UITableViewController.現在RefreshControl是UIScrollView的1個屬性.
使用方法:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
collectionView.refreshControl = refreshControl;
大家遇到問題歡迎向我提出,我會不斷完善這個項目.