Open Settings app from another app programmatically in iPhone

前端 未结 6 918
陌清茗
陌清茗 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:26

    Here is a Swift2 version that worked for me including an Alert that instructs the user in what to do when the settings opens.

    func initLocationManager() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
    
    
    // If there isn't a Lat/Lon then we need to see if we have access to location services
    // We are going to ask for permission to use location if the user hasn't allowed it yet.
    let status = CLLocationManager.authorizationStatus()
    if(status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied)  {
    
        //println(locationManager)
    
        //  check that locationManager is even avaiable.  If so, then ask permission to use it
        if locationManager != nil {
            locationManager.requestAlwaysAuthorization()
    
            //open the settings to allow the user to select if they want to allow for location settings.
            let alert = UIAlertController(title: "I Can't find you.", message: "To let my App figure out where you are on the map click 'Find Me' and change your location to 'Always' and come back to MyMobi.", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler:nil))
            alert.addAction(UIAlertAction(title: "Find Me", style: UIAlertActionStyle.Default, handler: {
                (alert: UIAlertAction!) in
                UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
            }))
            self.presentViewController(alert, animated: true, completion: nil)
    
    
        }
    }
    }
    

提交回复
热议问题