1、SDK 裏面的某些API不能在iOS8下使用
如果,你的老項目在iOS8下運行,打開就閃退(iOS8之前沒問題),那麼“恭喜你”,你中招了,比如下面我遇到的,是因爲舊版本的高德地圖引用了 iOS8 裏面不能用的api,如果你也需要類似的問題,那麼是時候升級需要升級的第三方庫了。
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'
2、iOS8 下面定位功能使用改變了
之前版本的SDk是這樣啓動系統定位的
// 判斷定位操作是否被允許
if([CLLocationManager locationServicesEnabled]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}else {
//提示用戶無法進行定位操作
}
如果在iOS8下用這樣的方式,你會發現無法定位,那是因爲iOS8下添加了新的方法
/表示使用應用程序期間 開啓定位
- (void)requestWhenInUseAuthorization
//表示始終 開啓定位
- (void)requestAlwaysAuthorization
兩者區別在於,iOS7 開始,有更強大的後臺運行功能,如果 用 requestAlwaysAuthorization 方法,則表示後臺運行時也會用到定位
iOS8 下使用系統定位如下:
// 判斷定位操作是否被允許
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;
同時還需要添加新的方法,其他的都一樣
- (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];
}
}
除了這些,你還需要在 info.plist 裏面添加新的鍵值,否則 也是無法定位的
//表示使用應用程序期間 開啓定位
- (void)requestWhenInUseAuthorization 對應 NSLocationWhenInUseUsageDescription key
//表示始終 開啓定位
- (void)requestAlwaysAuthorization 對應 NSLocationAlwaysUsageDescription key
其中,NSLocationWhenInUseUsageDescription(或者NSLocationAlwaysUsageDescription) 對應的文字會在第一次請求用戶同意定位的時候出現,還有 設置 > 隱私 > 定位 > your app 裏面也會看到,比如下面就是開啓app時出現的
3、iOS8 下注冊通知的改變
這個不用多說,直接看代碼就明白了,有一點需要注意的是,藍色部分必須要加,不然即便能取的token值,app 接受到的推送也是無聲的。
//註冊消息通知
if (IOS8After) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];
}
else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
}
4、iOS8 cell 層級的改變
如果你像這樣取cell 的row 的話,那你又要加個判斷方法了,在iOS8下cell的層級又改了,基本上每升級一個版本,蘋果都會對cell的結構進行調整,在此建議不要用這樣的方式取cell 的row,而是用屬性的方式保存 indexPath
NSUInteger curRow = 0;
if ([[MetaData getOSVersion] integerValue] == 7)
{
curRow = [(UITableView )[[self superview] superview] indexPathForCell:self].row;
}
else
{
curRow = [(UITableView )[self superview] indexPathForCell:self].row;
}
5、UIActionSheet and UIAlertView 的升級
在iOS8裏面,官方提供了新的類UIAlertController
來替換UIActionSheet and UIAlertView。
示例代碼如下:
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];
至於爲什麼爲加這個類,本人猜測是和iOS8新加的size classes有關,目的是統一屏幕在各個尺寸各個方向上的顯示。如果你在iOS 8 裏面使用UIActionSheet and UIAlertView
可能會出現一些很奇怪的問題,建議在iOS 8 裏面使用UIAlertController
,iOS 8 之前使用UIActionSheet and UIAlertView
轉載: