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,268评论 4 61
  • 如何适配iOS8 1、新特性Size Class介绍 随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个...
    SunshineBrother阅读 3,938评论 0 23
  • 你说 我爱你 你爱我 这算贱吗? 是的...就是犯贱 如果你和我还是一个月以前的我们就是相爱的恋人 然而没有如果现...
    爱上王子的丑小鸭阅读 351评论 0 1
  • 活着,首先是能够感知这个世界的先决条件。可是其实很多人到死都不知道如何活着,为何而活。自小到大,自己也从来没有认真...
    阿同学阅读 251评论 0 0
  • 这里是yoyo切克闹,我的第一篇简书。 生活中介绍自己都说,我是yoyo,在这里,你可以叫我切克闹。 习惯了这个名...
    heyyoheyyoyo阅读 327评论 0 1