首先,需要在Info.plist
配置文件中,增加键:UIViewControllerBasedStatusBarAppearance
,并设置为YES
;
然后,在UIViewController
子类中实现以下两个方法:
objc
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
swift
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}
override func prefersStatusBarHidden() -> Bool {
return false;
}
最后,在需要刷新状态栏样式的时候,调用[self setNeedsStatusBarAppearanceUpdate]
方法即可刷新
注意:
[self setNeedsStatusBarAppearanceUpdate]
在push
或者present
的controller
里面调用才起作用
如果,你的老项目在iOS8下运行,打开就闪退(iOS8之前没问题),那么“恭喜你”,你中招了,比如下面我遇到的,是因为旧版本的高德地图引用了 iOS8 里面不能用的api,如果你也需要类似的问题,那么是时候升级需要升级的第三方库了。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDevice asUniqueDeviceIdentifier]: unrecognized selector sent to instance 0x7c020080'
之前版本的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
//添加键值如下
//对应的key为 NSLocationWhenInUseUsageDescription
//对应类型 String
//对应值为 定位提示的信息
//表示始终 开启定位
- (void)requestAlwaysAuthorization
//添加键值如下
//对应的key为 NSLocationAlwaysUsageDescription
//对应类型 String
//对应值为 定位提示的信息
其中,NSLocationWhenInUseUsageDescription
(或者NSLocationAlwaysUsageDescription
) 对应的文字会在第一次请求用户同意定位的时候出现,还有 设置 > 隐私 > 定位 > your app 里面也会看到
这个不用多说,直接看代码就明白了
//注册消息通知
if (IOS8After) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];
}else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
}
如果你像这样取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;
}
在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
在iOS 8中,[UIScreen bounds] 、[UIScreen applicationFrame] 、Status bar、Keyboard这些frame都是根据设备真实方向来返回frame的,而在iOS 7中,不过是横屏还是竖屏,iOS总是返回竖屏的frame,如以下输出:
iOS7
竖屏:
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
横屏:
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
iOS8
竖屏:
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
横屏:
UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0)
这就对某些支持横屏的App造成了困扰,其实也只需要加两个宏或者改造一下就行了:
#define SCREEN_WIDTH (getScreenSize().width)
#define SCREEN_HEIGHT (getScreenSize().height)
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
属性的解释不一样:
另外当你还需要手动调用 contentInset
的时候,iOS7 似乎就不会自动调整了。
解决办法就是将 automaticallyAdjustsScrollViewInsets
设置为 NO
,然后自己控制 contentInset
。
Info.plist
中添加NSAppTransportSecurity
类型Dictionary
。NSAppTransportSecurity
下添加NSAllowsArbitraryLoads
类型Boolean
,值设为YES
或者直接添加一下配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
详细介绍 请查看
详细介绍 请查看
最近更新版本时无论提交几次 在构建版本
里都不显示
iOS 10 的设备权限需要在plist 文件里面添加字段。
iOS 10 的设备权限 需要添加的字段有:
<!-- 相册 -->
<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>