iOS 8 適配的幾點總結

自從蘋果公司在9月17號開放升級以來,官方顯示iOS8已經有46%的份額,微信,支付寶,新浪微博等也已經兼容iOS8,對于開發者來說,兼容iOS8 也是遲早的事情。下面說幾點我在兼容iOS8時,發現的幾點問題。

1、SDK 里面的某些API不能在iOS8下使用

如果,你的老項目在iOS8下運行,打開就閃退(iOS8之前沒問題),那么“恭喜你”,你中招了,比如下面我遇到的,是因為舊版本的高德地圖引用了 iOS8 里面不能用的api,如果你也需要類似的問題,那么是時候升級需要升級的第三方庫了。
<pre><code>2014-09-28 14:32:25.576 WoZaiXianChang[4505:140022] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDevice asUniqueDeviceIdentifier]: unrecognized selector sent to instance 0x7c020080' </code></pre>

2、iOS8 下面定位功能使用改變了

之前版本的SDk是這樣啟動系統定位的
<pre><code> // 判斷定位操作是否被允許
if([CLLocationManager locationServicesEnabled]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}else {
//提示用戶無法進行定位操作
} </code></pre>如果在iOS8下用這樣的方式,你會發現無法定位,那是因為iOS8下添加了新的方法
<pre><code> /表示使用應用程序期間 開啟定位

  • (void)requestWhenInUseAuthorization
    //表示始終 開啟定位
  • (void)requestAlwaysAuthorization </code></pre>

兩者區別在于,iOS7 開始,有更強大的后臺運行功能,如果 用 requestAlwaysAuthorization 方法,則表示后臺運行時也會用到定位
iOS8 下使用系統定位如下:
<pre><code> // 判斷定位操作是否被允許
if([CLLocationManager locationServicesEnabled]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//兼容iOS8定位
SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
[locationManager respondsToSelector:requestSelector]) {
[locationManager requestWhenInUseAuthorization];
} else {
[locationManager startUpdatingLocation];
}
return YES;
}else {
//提示用戶無法進行定位操作
}
return NO; </code></pre>
同時還需要添加新的方法,其他的都一樣
<pre><code> - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager startUpdatingLocation];
} else if (status == kCLAuthorizationStatusAuthorized) {
// iOS 7 will redundantly call this line.
[locationManager startUpdatingLocation];
} else if (status > kCLAuthorizationStatusNotDetermined) {
//...
[locationManager startUpdatingLocation];
}
} </code></pre>
除了這些,你還需要在 info.plist 里面添加新的鍵值,否則 也是無法定位的
<pre><code>//表示使用應用程序期間 開啟定位

  • (void)requestWhenInUseAuthorization 對應 NSLocationWhenInUseUsageDescription key
    //表示始終 開啟定位
  • (void)requestAlwaysAuthorization 對應 NSLocationAlwaysUsageDescription key</code></pre>

其中,NSLocationWhenInUseUsageDescription(或者NSLocationAlwaysUsageDescription) 對應的文字會在第一次請求用戶同意定位的時候出現,還有 設置 > 隱私 > 定位 > your app 里面也會看到,比如下面就是開啟app時出現的


3、iOS8 下注冊通知的改變

這個不用多說,直接看代碼就明白了,有一點需要注意的是,藍色部分必須要加,不然即便能取的token值,app 接受到的推送也是無聲的。
<pre><code>//注冊消息通知
if (IOS8After) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
<span style="color:#3333ff;">[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];</span>
}
else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
} </code></pre>

4、iOS8 cell 層級的改變

如果你像這樣取cell 的row 的話,那你又要加個判斷方法了,在iOS8下cell的層級又改了,基本上每升級一個版本,蘋果都會對cell的結構進行調整,在此建議不要用這樣的方式取cell 的row,而是用屬性的方式保存 indexPath
<pre><code>NSUInteger curRow = 0;
if ([[MetaData getOSVersion] integerValue] == 7)
{
curRow = [(UITableView *)[[self superview] superview] indexPathForCell:self].row;
}
else
{
curRow = [(UITableView *)[self superview] indexPathForCell:self].row;
} </code></pre>

5、UIActionSheet and UIAlertView 的升級

在iOS8里面,官方提供了新的類UIAlertController來替換UIActionSheet and UIAlertView。
示例代碼如下:
<pre><code>
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];

[self presentViewController:alert animated:YES completion:nil];
</pre></code>
至于為什么為加這個類,本人猜測是和iOS8新加的size classes有關,目的是統一屏幕在各個尺寸各個方向上的顯示。如果你在iOS 8 里面使用UIActionSheet and UIAlertView 可能會出現一些很奇怪的問題,建議在iOS 8 里面使用UIAlertController,iOS 8 之前使用UIActionSheet and UIAlertView
這是目前為止發現的 iOS8 兼容性問題,大家發現別的兼容性問題,一起討論哦
本文在csdn上面也有發布:http://blog.csdn.net/wangyangyangcc/article/details/39637787

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,259評論 4 61
  • 如何適配iOS8 1、新特性Size Class介紹 隨著iOS8系統的發布,一個全新的頁面UI布局概念出現,這個...
    SunshineBrother閱讀 3,933評論 0 23
  • 你說 我愛你 你愛我 這算賤嗎? 是的...就是犯賤 如果你和我還是一個月以前的我們就是相愛的戀人 然而沒有如果現...
    愛上王子的丑小鴨閱讀 351評論 0 1
  • 活著,首先是能夠感知這個世界的先決條件??墒瞧鋵嵑芏嗳说剿蓝疾恢廊绾位钪?,為何而活。自小到大,自己也從來沒有認真...
    阿同學閱讀 247評論 0 0
  • 這里是yoyo切克鬧,我的第一篇簡書。 生活中介紹自己都說,我是yoyo,在這里,你可以叫我切克鬧。 習慣了這個名...
    heyyoheyyoyo閱讀 325評論 0 1