How to open WIFI setting in Swift 3

半城伤御伤魂 提交于 2019-11-30 17:29:26

问题


I want to open WIFI setting section from my iOS application, my code was working well before Swift 3 with iOS 9.2

if let settingsURL = URL(string: AppSettingsWifiUrl) {
    UIApplication.shared.openURL(settingsURL)
}

But after updating it is not working with Xcode 8 + Swift 3 + iOS 10, can anybody help here?


回答1:


We can't do this anymore on iOS 11, we can just open the settings :

if let url = URL(string:UIApplicationOpenSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
       let url =  UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}



回答2:


 let url=URL(string: "App-Prefs:root=Privacy&path=Location")
        if UIApplication.shared.canOpenURL(url!)
        {
            UIApplication.shared.open(url!, options: [:], completionHandler: {sucess in

            })

        }
        else{
            UIApplication.shared.open(url!, options: [:], completionHandler: {sucess in

            })
        }



回答3:


It's possible on iOS 10 and Swift 3

let url = URL(string: "App-Prefs:root=WIFI") //for WIFI setting app
let app = UIApplication.shared
app.openURL(url!)



回答4:


iOS 9+

You can't directly open the Wi-Fi settings tab from your app. You are just allowed to open the settings app in general.

The following code works with Swift 4 + iOS 9+:

func openWifiSettings() {
    print("Opening Wi-Fi settings.")

    let shared = UIApplication.shared
    let url = URL(string: UIApplicationOpenSettingsURLString)!

    if #available(iOS 10.0, *) {
        shared.open(url)
    } else {
        shared.openURL(url)
    }
}

Source: Apple developer documentation open and openURL


It was actually possible in iOS 9 to open the Wi-Fi settings tab directly (using private API URL schemes) but this was considered as bug usage.

Therefore: Don't use App-Prefs:root or pref:root URL schemes as they will lead to rejection of your app by Apple's review check.

Source: Apple developer forum post from Apple's eskimo.


iOS 11+

If you need to connect to a certain Wi-Fi network NEHotspotConfigurationManager maybe helps.

Source: Apple technical Q&A QA1942




回答5:


Just use:

UIApplication.shared.openURL(URL(string:"prefs:root=WIFI")!)



回答6:


Use the following for iOS 10 and above:

if let url = URL(string:"App-Prefs:root=Settings&path=General") { 
    UIApplication.shared.openURL(url)
}



回答7:


This thing will work with iOS 10 also but you have to add URL types in ur project setting info. URL schemes should be prefs If you need I will share the screen shot so that you can easily achieve.

Thanks 😊😊



来源:https://stackoverflow.com/questions/39764553/how-to-open-wifi-setting-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!