Open Settings app from another app programmatically in iPhone

前端 未结 6 915
陌清茗
陌清茗 2020-12-07 18:55

I have to open settings app from my app if gps is not enabled in iPhone. I have used the following code. It works well in iOS simulator but it does not work in iPhone. May I

6条回答
  •  长情又很酷
    2020-12-07 19:52

    Opening settings apps programmatically is possible only from iOS 8. So, use the following code...

    if([CLLocationManager locationServicesEnabled]&&
       [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
    {
      //...Location service is enabled
    }
    else
    {
        if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
        {
           UIAlertView* curr1=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
          [curr1 show];
        }
        else
        {
           UIAlertView* curr2=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Settings", nil];
           curr2.tag=121;
           [curr2 show];
        }
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
       if (alertView.tag == 121 && buttonIndex == 1)
     {
      //code for opening settings app in iOS 8
       [[UIApplication sharedApplication] openURL:[NSURL  URLWithString:UIApplicationOpenSettingsURLString]];
     }
    }
    

提交回复
热议问题