天天看點

iOS開發--iOS 10适配(轉)

1.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。

如圖:

iOS開發--iOS 10适配(轉)

細節提示:在iOS9以後的系統中如果使用到網絡圖檔,也要注意網絡圖檔是否是HTTP的,如果是,也要把圖檔的域設定!

2.iOS 10 隐私權限設定

iOS 10 開始對隐私權限更加嚴格,如果你不設定就會直接崩潰,現在很多遇到崩潰問題了,一般解決辦法都是在

info.plist

檔案添加對應的

Key

-

Value

就可以了。

iOS開發--iOS 10适配(轉)

以上

Value

值,圈出的紅線部分的文字是展示給使用者看的,必須添加

<!-- 相冊 --> 
<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>      

3.iOS 10 UIStatusBar方法過期:

iOS開發--iOS 10适配(轉)

在我們開發中有可能用到

UIStatusBar

一些屬性,在iOS 10 中這些方法已經過期了,如果你的項目中有用的話就得需要适配。

上面的圖檔也能發現,如果在iOS 10中你需要使用

preferredStatusBar

比如這樣:

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

4.Xcode 8 運作一堆沒用的logs解決辦法

iOS開發--iOS 10适配(轉)

上圖我們看到,自己建立的一個工程啥也沒幹就列印一堆爛七八糟的東西,我覺得這個應該是

Xcode 8

的問題,

具體也沒細研究,解決辦法是設定

OS_ACTIVITY_MODE : disable

如下圖:

第一步:

iOS開發--iOS 10适配(轉)

第二步:

iOS開發--iOS 10适配(轉)

第三步:

添加參數:

Name :OS_ACTIVITY_MODE 

Value :  disable

iOS開發--iOS 10适配(轉)

5.Notification(通知)

自從

Notification

被引入之後,蘋果就不斷的更新優化,但這些更新優化隻是小打小鬧,直至現在iOS 10開始真正的進行大改重構,這讓開發者也體會到

UserNotifications

的易用,功能也變得非常強大。

  • iOS 9 以前的通知

    1.在調用方法時,有些方法讓人很難區分,容易寫錯方法,這讓開發者有時候很苦惱。

    2.應用在運作時和非運作時捕獲通知的路徑還不一緻。

    3.應用在前台時,是無法直接顯示遠端通知,還需要進一步處理。

    4.已經發出的通知是不能更新的,内容發出時是不能改變的,并且隻有簡單文本展示方式,擴充性根本不是很好。

  • iOS 10 開始的通知

    1.所有相關通知被統一到了

    UserNotifications.framework

    架構中。

    2.增加了撤銷、更新、中途還可以修改通知的内容。

    3.通知不在是簡單的文本了,可以加入視訊、圖檔,自定義通知的展示等等。

    4.iOS 10相對之前的通知來說更加好用易于管理,并且進行了大規模優化,對于開發者來說是一件好事。

    5.iOS 10開始對于權限問題進行了優化,申請權限就比較簡單了(本地與遠端通知內建在一個方法中)。

    如果使用了推送,修改如圖:

   

iOS開發--iOS 10适配(轉)

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

    協定,這個協定裡面定義了兩個方法:

   在

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

寫了一套分類的話也可嘗試替換為

sRGB

UIColor

類中新增了兩個

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 字型随着手機系統字型而改變

當我們手機系統字型改變了之後,那我們

App

label

也會跟着一起變化,這需要我們寫很多代碼來進一步處理才能實作,但是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;
           

9.iOS 10 UITextContentType

在iOS 10 

UITextField

添加了

textContentType

枚舉,訓示文本輸入區域所期望的語義意義。

使用此屬性可以給鍵盤和系統資訊,關于使用者輸入的内容的預期的語義意義。例如,您可以指定一個文本字段,使用者填寫收到一封電子郵件确認

uitextcontenttypeemailaddress

。當您提供有關您期望使用者在文本輸入區域中輸入的内容的資訊時,系統可以在某些情況下自動選擇适當的鍵盤,并提高鍵盤修正和主動與其他文本輸入機會的整合。

10.iOS 10 判斷系統版本正确方式

判斷系統版本是我們經常用到的,尤其是現在大家都有可能需要适配iOS 10,那麼問題就出現了,如下圖:

iOS開發--iOS 10适配(轉)

我們得到了答案是:

是以說判斷系統方法最好還是用後面的兩種方法,哦~我忘記說了

[[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 { }

           

11.iOS 10 UIScrollView新增refreshControl

iOS開發--iOS 10适配(轉)

iOS 10 以後隻要是繼承

UIScrollView

那麼就支援重新整理功能:

14.Xcode 8使用Xib awakeFromNib的警告問題

Xcode 8

之前我們使用

Xib

初始化

- (void)awakeFromNib {}

都是這麼寫也沒什麼問題,但是在

Xcode 8

會有如下警告:

iOS開發--iOS 10适配(轉)

15、推送的時候,

開啟Remote notifications

  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的行為是不可預知的。一種可能的情況是任意一個程式的該類會被加載(這應該也是預設動作)

在 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的資源檔案名.

iOS開發--iOS 10适配(轉)
8.  找到這個資源檔案修改為 8位的sRGB形式,重新編譯上傳你的app即可.  18、This version does not support documents saved in the Xcode 8 format. Open this document with Xcode 8 or later

  編輯項目時預設使用Xcode8打開,導緻我用Xcode7打開Xib是報錯:

<code class="hljs livecodeserver has-numbering" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);"> This </span><span class="hljs-built_in" style="margin: 0px; padding: 0px; color: rgb(0, 0, 255);"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">version does </span><span class="hljs-operator" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">not support documents saved </span><span class="hljs-operator" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">in </span><span class="hljs-operator" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">the Xcode </span><span class="hljs-number" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">8 </span><span class="hljs-built_in" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">format. Open this document </span><span class="hljs-operator" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">with Xcode </span><span class="hljs-number" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">8.0 </span><span class="hljs-operator" style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">or later</span><br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /><img src="http://images2015.cnblogs.com/blog/575661/201609/575661-20160921142554277-537167035.png" alt="" width="582" height="225" style="margin: 0px; padding: 0px; border: none; max-width: 800px;" /><br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /></span></span></span></span></span></span></span></span></span></code>      

   導緻用Xcode8打開的Xib全部打不開,隻能用編輯器将Xib裡面的下面一句話删除掉才能打開:

<code class="language-object-c hljs xml has-numbering" style="margin: 0px; padding: 0px;"><span class="hljs-tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 255);"><<span class="hljs-title" style="margin: 0px; padding: 0px; color: rgb(163, 21, 21);">capability <span class="hljs-attribute" style="margin: 0px; padding: 0px;">name=<span class="hljs-value" style="margin: 0px; padding: 0px;">"documents saved in the Xcode 8 format" <span class="hljs-attribute" style="margin: 0px; padding: 0px;">minToolsVersion=<span class="hljs-value" style="margin: 0px; padding: 0px;">"8.0"/></span></span></span></span></span></span></code>      

持續更新...

繼續閱讀