APP如何跳转到系统的setting

iOS 8及以上版本可以跳转到系统setting中进行设置,用户可以根据APP的需要授权启用位置、通知、联系人、相机、日历以及健康等设置。

关键代码是这几句

// 跳到设置界面,进行设置
- (void)jumpToSetting{
    NSURL *urlSetting = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    
    if (urlSetting) {
        if ([[UIApplication sharedApplication] canOpenURL:urlSetting]) {
            
            [[UIApplication sharedApplication] openURL:urlSetting];
        }
    }
}

这一方法在iOS 9的系统中,应用效果更好!设置界面中将有一个返回按钮,能直接使用户返回到您的应用程序。非常方便!
下面是我写的一个Demo:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>

/**
 * 位置管理器
 */
@property (strong, nonatomic) CLLocationManager * locationManager;

- (IBAction)LocationBtnClick:(id)sender;
@end

@implementation ViewController

-(CLLocationManager *)locationManager{
    if (_locationManager == nil) {
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    
        _locationManager = locationManager;
    }
    return _locationManager;
}
- (void)viewDidLoad {
    [super viewDidLoad];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)LocationBtnClick:(id)sender {
    
    // 先判断授权状态
    CLAuthorizationStatus statue = [CLLocationManager authorizationStatus];
    switch (statue) {
        case kCLAuthorizationStatusNotDetermined:
                NSLog(@"kCLAuthorizationStatusNotDetermined,请求授权");
                [self requsetAuthorization];
                break;
        case kCLAuthorizationStatusDenied:
                NSLog(@"kCLAuthorizationStatusDenied");
                // 弹出提示用户是否去setting界面设置
                [self showTipsWhetherToSetting];
                break;
        case kCLAuthorizationStatusAuthorizedAlways:
                NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
                break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
                NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
                break;
        default:
                break;
    }
}

-(void)showTipsWhetherToSetting{
    
    NSInteger systemNo = [[UIDevice currentDevice].systemVersion integerValue];
    BOOL isMoreThan8 = systemNo > 8.0 ? YES : NO;
    
    if (isMoreThan8) {
        // UIAlertController
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"小樱桃需要你的位置" message:@"点击确定,前往设置" preferredStyle:UIAlertControllerStyleAlert];
        
        __weak typeof(self) weakSelf = self;
        UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
                [weakSelf jumpToSetting];
        }];
        
        [alert addAction:determineAction];
        
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
                NSLog(@"取消操作");
        }];
        
        [alert addAction:cancleAction];
        
        // modal出这个alerController
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        // UIActionsheet
        
    }
}


// 跳到设置界面,进行设置
- (void)jumpToSetting{
    NSURL *urlSetting = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    
    if (urlSetting) {
        if ([[UIApplication sharedApplication] canOpenURL:urlSetting]) {
            
            [[UIApplication sharedApplication] openURL:urlSetting];
        }
    }
}

- (void)requsetAuthorization{
    if ([CLLocationManager locationServicesEnabled]) {// 判断手机是否可以定位
        
        [self.locationManager setDelegate:self];
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];//设置精度
        self.locationManager.distanceFilter = 5.0f;
        
        // iOS8.0的API
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];// 请求授权
        }
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            [self.locationManager requestWhenInUseAuthorization];  //调用了这句,就会弹出允许框了.
        }
        
        [self.locationManager startUpdatingLocation];//启动位置管理器
        
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,666评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 《元气囝仔》是2008年由日本漫画家吉野五月创作的漫画,并于2014年以动画的形式播出。因为暑假是补番的“好季节”...
    四张纸阅读 1,264评论 0 2
  • 打卡和系统登录要求 打卡的总体要求就是每天到公司就要打卡,每天下班离开也要打卡。 超过9点打卡,公司罚款,一个月里...
    太虚见阅读 784评论 0 0