2015-05-22 75 views
1

我的應用程序使用用戶位置的背景下,但有時,用戶不允許應用程序始終收集的GPS數據。該應用只能處理前景,我想將其設置爲後備。是否有可能退回到AuthorizedWhenInUse當用戶不允許的iOS AuthorizedAlways?

有沒有一種優雅的方式,一旦iOS的用戶已經拒絕我的要求AuthorizedAlways,再提示用戶給AuthorizedWhenInUse許可?

+0

給用戶兩個選項前面。解釋AuthorizedAlways的好處和缺點。個人如果我被要求總是我通常垃圾應用程序,除非我有一個很好的理由,爲什麼這將有利於我。我實際上重視我的隱私和更好的生活。 – zaph

回答

0

你不能強迫它,但你可以:

1)知道用戶被拒絕權限 2),並顯示一條警告,要求:「請去設置和啓用它。」
3)進入iOS設置([[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];

事情是這樣的:

- (void)checkLocation 
{ 
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; 

    if (status == kCLAuthorizationStatusAuthorizedAlways){ 
     NSLog(@"ok"); 
    } else if(status == kCLAuthorizationStatusDenied || 
       status == kCLAuthorizationStatusAuthorizedWhenInUse){ 
     [self showRequestLocationMessage]; 
    } else if(status == kCLAuthorizationStatusNotDetermined){ 
     //request auth 
    } 

} 

- (void)showRequestLocationMessage 
{ 
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Settings" 
                style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * alertAction){ 
                 [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]]; 
                }]; 

    NSString *title = @"Service Enable"; 
    NSString *text = @"Please enable your location.. bla bla bla"; 

    UIAlertController *alertController = [UIAlertController 
              alertControllerWithTitle:title 
              message:text 
              preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
                  style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action){ 
                  [alertController dismissViewControllerAnimated:YES completion:nil]; 
                 }]; 

    [alertController addAction:cancelAction]; 
    [alertController addAction:action]; 

    [self presentViewController:alertController animated:YES completion:nil]; 
} 
相關問題