天天看點

相容iOS 10 _更新xcode8_适配(四)

1.Notification(通知)

自從 

Notification

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

UserNotifications

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

  • iOS 9 以前的通知

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

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

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

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

  • iOS 10 開始的通知

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

    UserNotifications.framework

     架構 中。

    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。

如圖:

id="iframe_0.9657231628881202" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg0.tuicool.com%2F3yY3Urz.png!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.9657231628881202" frame scrolling="no" height="321" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

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

3.iOS 10 隐私權限設定

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

info.plist

 檔案添加對應的 

Key

 - 

Value

 就可以了。

id="iframe_0.22104028496188577" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg2.tuicool.com%2FaQRFRff.png!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.22104028496188577" frame scrolling="no" height="204" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

以上 

Value

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

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

id="iframe_0.7514099695004363" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg1.tuicool.com%2FERRFbmq.jpg!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.7514099695004363" frame scrolling="no" height="223" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

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

Xcode 8

 的問題,

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

OS_ACTIVITY_MODE : disable

 如下圖:

id="iframe_0.22055615255279548" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg2.tuicool.com%2F3IBvMrr.jpg!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.22055615255279548" frame scrolling="no" height="307" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

5.iOS 10 UIStatusBar方法過期:

id="iframe_0.3417676222375011" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg2.tuicool.com%2FjY77ve2.jpg!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.3417676222375011" frame scrolling="no" height="334" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

在我們開發中有可能用到 

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

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

sRGB

 , 

UIColor

 類中新增了兩個 

Api

 如下:

8.iOS 10 UITextContentType

在iOS 10 

UITextField

 添加了 

textContentType

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

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

uitextcontenttypeemailaddress

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

9.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;      

10.iOS 10 UIScrollView新增refreshControl

id="iframe_0.2872943740169914" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg2.tuicool.com%2F6za6ZbE.png!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.2872943740169914" frame scrolling="no" height="240" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

iOS 10 以後隻要是繼承 

UIScrollView

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

11.iOS 10 判斷系統版本正确姿勢

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

id="iframe_0.2766822983132954" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg2.tuicool.com%2FqYFbuau.jpg!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.2766822983132954" frame scrolling="no" height="241" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

我們得到了答案是:

//值為 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

 分别測試了下,如下圖:

id="iframe_0.05373455008066208" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg0.tuicool.com%2Fm2M7N3Y.jpg!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.05373455008066208" frame scrolling="no" height="223" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

Xcode 8

id="iframe_0.0011046005627244337" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg0.tuicool.com%2FF7Bbyiu.jpg!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.0011046005627244337" frame scrolling="no" height="224" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

Xcode7 

建立一個         Label                然後讓它自适應大小,字型大小都是         17                最後輸出的寬度是不一樣的,我們再看一下,
下面的資料就知道為什麼更新         iOS 10                 之後         App                中有的文字顯示不全了:

 id="iframe_0.5960106159323463" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg2.tuicool.com%2F2YjaAbM.png!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.5960106159323463" frame scrolling="no" height="327" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

英文字母會不會也有這種問題,我又通過測試,後來發現英文字母沒有問題,隻有漢字有問題。
目前隻有一個一個修改控件解決這個問題,暫時沒有其他好辦法來解決。      

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

在 

Xcode 8

 之前我們使用 

Xib

 初始化 

- (void)awakeFromNib {}

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

Xcode 8

 會有如下警告:

id="iframe_0.6586377860795032" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg0.tuicool.com%2FE3EBbyN.png!web&maxWidth=1159&origin=http://www.cnblogs.com&iframeId=iframe_0.6586377860795032" frame scrolling="no" height="74" style="margin: 0px; padding: 0px; border-style: none; width: 1159px;">

官方解釋:
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方法中的任何時候超級實作。
           

最怕你一生碌碌無為 還安慰自己平凡可貴