自從蘋果公司在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