demo地址: https://github.com/Pangmanli1/Demos.git
在iOS10 之前 , 计步器, 推送通知等系统权限的获取, 只需在代码中申请, 不会出现系统弹窗. 但是iOS10 之后, 就全部会弹出, 加上 网络权限, siri权限,healthkit等权限的获取 弹窗. 导致程序首次启动时, 叠了一堆的弹窗,用户甚至都来不及点击允许还是不允许, 就弹出了下一个窗口,体验极差.
为了解决这个问题, 这里我借鉴 "乐动力"的首次启动界面(关于别的应用资源怎么拿到的,如果你忘记了,也可以在评论mark一下,我会详细说明) , 做了个demo.目的旨在 让主要的系统权限的获取 (主要是 定位权限, 计步器功能权限, 推送通知权限), 待用户点击后逐个弹出,无论用户点击的是允许还是不允许获取权限,都进入下一权限的获取. 这就需要监听用户的点击,这个我们是很难做到的,所以只能找到系统给出的用户授权的回调函数,再在回调函数中, 加入自己的跳转逻辑.
对于iOS10 的首次启动应用 网络权限获取, 本身存在坑(移步http://www.cocoachina.com/ios/20161125/18181.html), 我就不累述. 不过经过引导页的缓冲时间, 可以避开这个坑. 没有反复试验, 有试验过的欢迎补充.
进入正题, 首先视频的播放,我选用的AVFoundation 框架,上代码
//获取视频路径
NSString * path = [[NSBundle mainBundle]pathForResource:@"ledongliGuide.mp4" ofType:nil];
//路径转化为URL
NSURL *sourceMovieUrl = [NSURL fileURLWithPath:path];
//放入视频资源盒
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieUrl options:nil];
//用视频资源盒中的资源创建播放器选项playerItem
self.playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
//用playerItem创建播放器AVPlayer
player = [AVPlayer playerWithPlayerItem:self.playerItem];
//新建播放图层playerLayer
AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.frame = self.view.frame;
//设置视频填充屏幕模式(保持原视频比例,多余部分留黑)
layer.videoGravity = AVLayerVideoGravityResizeAspect;
//把播放器视图层加入到控制器视图层上
[self.view.layer addSublayer:layer];
[player play];
利用系统播放完成通知AVPlayerItemDidPlayToEndTimeNotification, 实现循环播放
#pragma mark- 添加播放器完成通知
-(void)addFinishPlayNotification{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playback:) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
}
#pragma mark- 播放完成重播
-(void)playback:(NSNotification*)notification {
[self.playerItem seekToTime:kCMTimeZero];
[player play];
}
#pragma mark- 移除通知
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
}
视图层级关系,从下到上依次为 播放器图层-> scrollView ->分别带有一个botton的3个view.
1. 获取定位权限
#pragma mark - 获取定位权限
-(void)locationAuthorize:(UIButton*)sender{
pageControl.userInteractionEnabled = NO;
//定位服务是否可用
BOOL enable=[CLLocationManager locationServicesEnabled];
//是否具有定位权限
int status=[CLLocationManager authorizationStatus];
self.manager = [[CLLocationManager alloc] init];
self.manager.delegate = self;
//请求在使用应用期间开启定位服务
[self.manager requestWhenInUseAuthorization];
//请求总是开启定位
[self.manager requestAlwaysAuthorization];
if (enable) {
if (status == 0) {
NSLog(@"status%d", status);
}else {
NSLog(@"status%d", status);
}
} else {
NSLog(@"去设置打开定位服务");
}
}
上面的requestWhenInUseAuthorization和 requestAlways...只是提出定位请求. 我们的跳转逻辑要写在CLLocation 的代理方法里(授权状态变化的代理方法).
@interface ViewController ()<CLLocationManagerDelegate> //遵守代理协议
@property (nonatomic, strong)CMPedometer *Pedometer ;
@property (nonatomic, strong)CLLocationManager *manager ;
@property (nonatomic, strong)AVPlayerItem * playerItem;
@end
代理方法 内写跳转逻辑
#pragma mark- 地图定位授权状态改变
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
//如果status = 0 说明用户点击了"不允许",定位授权获取失败
NSLog(@"地图状态改变%@",status);
//无论用户点击的是"允许"还是"不允许"(只要进入这个方法,就说明状态更新了), 都滚动到下一页
[_scrollView setContentOffset:CGPointMake(careCommonWidth*1, 0) animated:YES];
}
获取计步器权限
计步器要设置为全局属性(原因自行百度)
#pragma mark - 获取计步器健康数据权限
-(void)healthKitAuthorize:(UIButton*)sender{
sender.selected = !sender.selected;
_scrollView.scrollEnabled = NO;
pageControl.userInteractionEnabled = YES;
if ([CMPedometer isStepCountingAvailable]) {
[self.Pedometer queryPedometerDataFromDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*2] toDate:[NSDate dateWithTimeIntervalSinceNow:60] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
if (error) {
//用户点击了系统弹窗 "不允许" 按钮,获取授权失败
NSLog(@"error====%@",error);
}else {
//用户点击了"允许", 成功获取授权
NSLog(@"步数====%@",pedometerData.numberOfSteps);
NSLog(@"距离====%@",pedometerData.distance);
}
//无论用户是否允许授权,都进入下一页
[_scrollView setContentOffset:CGPointMake(careCommonWidth*2, 0) animated:YES];
}];
}else{
NSLog(@"计步功能不可用");
}
#pragma mark- 懒加载计步器
-(CMPedometer *)Pedometer {
if (_Pedometer == nil) {
_Pedometer = [[CMPedometer alloc]init];
}
return _Pedometer;
}
获取推送通知权限
#pragma mark- 获取推送通知权限
-(void)allowPush:(UIButton*)sender{
pageControl.userInteractionEnabled = YES;
UIApplication * app = [UIApplication sharedApplication];
//各版本适配
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//用户点击了"允许",成功授权,进入首页
pageControl.userInteractionEnabled = NO;
[player pause];
[self goToLoginPage];
} else {
//用户点击了"不允许",授权失败,进入首页
pageControl.userInteractionEnabled = NO;
[player pause];
[self goToLoginPage];
}
}];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
//iOS8 及以下,都不会弹窗, 只要代码里注册,就有权限了
[app registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
pageControl.userInteractionEnabled = NO;
[player pause];
[self goToLoginPage];
}else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
//iOS8系统以下
[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
pageControl.userInteractionEnabled = NO;
[player pause];
[self goToLoginPage];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
#pragma mark- 进入首页
-(void)goToLoginPage {
//回到主线程跳转页面
dispatch_async(dispatch_get_main_queue(), ^{
PMLFirstViewController * loginVC = [[PMLFirstViewController alloc]init];
UIApplication * app = [UIApplication sharedApplication];
app.keyWindow.rootViewController = loginVC;
});
}
如有错误不严谨之处,欢迎指正, 补充.