iOS适配

iOS7适配

状态栏设置

首先,需要在Info.plist配置文件中,增加键:UIViewControllerBasedStatusBarAppearance,并设置为YES

然后,在UIViewController子类中实现以下两个方法:

objc

1
2
3
4
5
6
7
8
9
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden
{
return NO;
}

swift

1
2
3
4
5
6
7
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}

override func prefersStatusBarHidden() -> Bool {
return false;
}

最后,在需要刷新状态栏样式的时候,调用[self setNeedsStatusBarAppearanceUpdate]方法即可刷新

注意:
[self setNeedsStatusBarAppearanceUpdate]push或者 presentcontroller里面调用才起作用

iOS8适配

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

如果,你的老项目在iOS8下运行,打开就闪退(iOS8之前没问题),那么“恭喜你”,你中招了,比如下面我遇到的,是因为旧版本的高德地图引用了 iOS8 里面不能用的api,如果你也需要类似的问题,那么是时候升级需要升级的第三方库了。

1
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDevice asUniqueDeviceIdentifier]: unrecognized selector sent to instance 0x7c020080'

iOS8 定位

之前版本的SDk是这样启动系统定位的

1
2
3
4
5
6
7
8
// 判断定位操作是否被允许
if([CLLocationManager locationServicesEnabled]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}else {
//提示用户无法进行定位操作
}

如果在iOS8下用这样的方式,你会发现无法定位,那是因为iOS8下添加了新的方法

1
2
3
4
//表示使用应用程序期间  开启定位  
- (void)requestWhenInUseAuthorization
//表示始终 开启定位
- (void)requestAlwaysAuthorization

两者区别在于,iOS7 开始,有更强大的后台运行功能,如果 用 requestAlwaysAuthorization 方法,则表示后台运行时也会用到定位
iOS8 下使用系统定位如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 // 判断定位操作是否被允许  
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;

同时还需要添加新的方法,其他的都一样

1
2
3
4
5
6
7
8
9
10
11
 - (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 里面添加新的键值,否则 也是无法定位的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//表示使用应用程序期间  开启定位  
- (void)requestWhenInUseAuthorization
//添加键值如下
//对应的key为 NSLocationWhenInUseUsageDescription
//对应类型 String
//对应值为 定位提示的信息


//表示始终 开启定位
- (void)requestAlwaysAuthorization
//添加键值如下
//对应的key为 NSLocationAlwaysUsageDescription
//对应类型 String
//对应值为 定位提示的信息

其中,NSLocationWhenInUseUsageDescription(或者NSLocationAlwaysUsageDescription) 对应的文字会在第一次请求用户同意定位的时候出现,还有 设置 > 隐私 > 定位 > your app 里面也会看到

iOS8 下注册通知的改变

这个不用多说,直接看代码就明白了

1
2
3
4
5
6
7
//注册消息通知
if (IOS8After) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];
}else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
}

iOS8 cell 层级的改变

如果你像这样取cell 的row 的话,那你又要加个判断方法了,在iOS8下cell的层级又改了,基本上每升级一个版本,苹果都会对cell的结构进行调整,在此建议不要用这样的方式取cell 的row,而是用属性的方式保存 indexPath

1
2
3
4
5
6
NSUInteger curRow = 0;
if ([[MetaData getOSVersion] integerValue] == 7){
curRow = [(UITableView )[[self superview] superview] indexPathForCell:self].row;
}else{
curRow = [(UITableView )[self superview] indexPathForCell:self].row;
}

UIActionSheet and UIAlertView 的升级

在iOS8里面,官方提供了新的类UIAlertController来替换UIActionSheet and UIAlertView。
示例代码如下:

1
2
3
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

bounds和contentInset

在iOS 8中,[UIScreen bounds] 、[UIScreen applicationFrame] 、Status bar、Keyboard这些frame都是根据设备真实方向来返回frame的,而在iOS 7中,不过是横屏还是竖屏,iOS总是返回竖屏的frame,如以下输出:

iOS7

1
2
3
4
竖屏:  
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
横屏:
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)

iOS8

1
2
3
4
竖屏:  
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
横屏:
UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0)

这就对某些支持横屏的App造成了困扰,其实也只需要加两个宏或者改造一下就行了:

1
2
#define SCREEN_WIDTH        (getScreenSize().width)  
#define SCREEN_HEIGHT (getScreenSize().height)
1
2
3
4
5
6
7
8
CGSize getScreenSize() {  
CGSize screenSize = [UIScreen mainScreen].bounds.size;
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) &&
UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
return CGSizeMake(screenSize.height, screenSize.width);
}
return screenSize;
}

虽然 contentInset 不属于屏幕适配的内容,但是我还是放在屏幕适配里说一下。
iOS8 和 iOS7 对 automaticallyAdjustsScrollViewInsets 属性的解释不一样:

  • iOS8 会把该属性的影响作用到 controller 的 view 的 subviews 上
  • iOS7 仅会作用到 self.view 上

另外当你还需要手动调用 contentInset 的时候,iOS7 似乎就不会自动调整了。
解决办法就是将 automaticallyAdjustsScrollViewInsets 设置为 NO,然后自己控制 contentInset

iOS9适配

http无法请求

  • Info.plist中添加NSAppTransportSecurity类型Dictionary
  • NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

或者直接添加一下配置

1
2
3
4
5
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

详细介绍 请查看

其他适配

详细介绍 请查看

iOS10适配

设备权限添加提示信息

最近更新版本时无论提交几次 在构建版本里都不显示

iOS 10 的设备权限需要在plist 文件里面添加字段。

iOS 10 的设备权限 需要添加的字段有:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!-- 相册 --> 
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<!-- 相机 -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
<!-- 麦克风 -->
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能访问麦克风</string>
<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<!-- 在使用期间访问位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期间访问位置</string>
<!-- 始终访问位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<!-- 日历 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能访问日历</string>
<!-- 提醒事项 -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能访问提醒事项</string>
<!-- 运动与健身 -->
<key>NSMotionUsageDescription</key> <string>App需要您的同意,才能访问运动与健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能访问健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能访问健康分享</string>
<!-- 蓝牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能访问蓝牙</string>
<!-- 媒体资料库 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>

其他适配