美文网首页
APP如何跳转到系统的setting

APP如何跳转到系统的setting

作者: 爱掏蜂窝的熊 | 来源:发表于2015-12-21 22:11 被阅读324次

    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];//启动位置管理器
            
        }
    }
    

    相关文章

      网友评论

          本文标题:APP如何跳转到系统的setting

          本文链接:https://www.haomeiwen.com/subject/fnimhttx.html