How to open Location services screen from setting screen?

前端 未结 11 2062
别那么骄傲
别那么骄傲 2020-12-04 14:01

I want to open location service screen programmatically to turn on service.

相关标签:
11条回答
  • 2020-12-04 14:50

    After adding prefs as a url type, use the following code to go directly to the location settings of an application.

    if let url = URL(string: "App-prefs:root=LOCATION_SERVICES") {
         UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
    
    0 讨论(0)
  • 2020-12-04 14:51

    0 讨论(0)
  • 2020-12-04 14:53

    I have tried all the above answers,it's not working on iOS11..it just opens settings page and not the app settings .. Finally this works..

    UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
    

    Swift 4.2:

    UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
    

    Refer: https://developer.apple.com/documentation/uikit/uiapplicationopensettingsurlstring?language=swift

    0 讨论(0)
  • 2020-12-04 14:54

    Actually there's much simpler solution to that. It'll show your app settings with loction services/camera access, etc.:

    func showUserSettings() {
        guard let urlGeneral = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }
        UIApplication.shared.open(urlGeneral)
    }
    
    0 讨论(0)
  • 2020-12-04 14:55

    First:

    Add URL

    Go to Project settings --> Info --> URL Types --> Add New URL Schemes

    See image below:

    Second:

    Use below code to open Location settings:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
    

    referred from: https://stackoverflow.com/a/35987082/5575752

    0 讨论(0)
  • 2020-12-04 14:56

    You can open it directly like using below code,

    But first set URL Schemes in Info.plist's URL Type Like:

    Then write below line at specific event:

    In Objective - C :

    [[UIApplication sharedApplication] openURL:
     [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
    

    In Swift :

    UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root=LOCATION_SERVICES")!)
    

    Hope this will help you.

    0 讨论(0)
提交回复
热议问题