How to open WIFI setting in Swift 3
Asked Answered
B

7

8

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?

Beneficiary answered 29/9, 2016 at 7:57 Comment(2)
#28153026Petition
@SinaKH: Thank you for your reply, but it will open application setting I want to open WIFI setting.Beneficiary
H
6

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)
    }
}
Hardie answered 15/2, 2018 at 14:43 Comment(1)
and not directly to wifi screen ?Desinence
V
4

Edited 26-02-20, Pay attention, in versions later than ios 10 your binary will be rejected

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!)
Vigen answered 23/3, 2017 at 19:6 Comment(6)
Where did you get this information from? Could you please provide a source?Propend
@Propend I found this info on this wiki of ios dev iphonedevwiki.net/index.php/Preferences.app and more info medium.com/@thanhvtrn/…Vigen
recently gave an app into review and "App-Prefs:root=Wifi" was a reason for apple to block the release. Here a part of the Mail: Your app uses the "prefs:root=" non-public URL scheme, which is a private entity. The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change.Pestalozzi
@MarcelT According to a comment from the second source Lito provided (you have to scroll down to the comment section), Apple shouldn't reject the app?!Propend
According to this forum post from Apples' eskimo, prefs:root= or App-Prefs:root= is considered as private API and therefore its usage is forbidden and leads to rejection of apps.Propend
My binary was rejected for review on the app store because of use of prefs:rootVaccaro
V
3
 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
                
            })
        }
Venereal answered 22/3, 2017 at 11:0 Comment(0)
P
3

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

Propend answered 19/8, 2018 at 17:39 Comment(1)
Yes, my app was rejected too because of Themis workaround. The explanation is pretty clear, even Apple won’t guarantee that it will work in the future. IMHO a application shouldn’t have Access to the WiFi settings. It should only be allowed to change settings for the use of the application, which is also possible, for WiFi use, in the settings tab of your application.Pestalozzi
B
0

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 😊😊

Bebe answered 29/9, 2016 at 7:58 Comment(0)
P
-1

Just use:

UIApplication.shared.openURL(URL(string:"prefs:root=WIFI")!)
Petition answered 29/9, 2016 at 9:34 Comment(2)
This "bug" will only work on iOS 9 and was fixed as of iOS 10. The usage of this bug leads to rejection of your app. Source: this forum post from Apples' eskimo.Propend
Apple will reject your app if you use "prefs:root="Paolo
C
-1

Use the following for iOS 10 and above:

if let url = URL(string:"App-Prefs:root=Settings&path=General") { 
    UIApplication.shared.openURL(url)
}
Caplin answered 21/2, 2017 at 9:7 Comment(2)
By the way: App-Prefs: and prefs: URL schemes are considered private API. It's usage was only possible as a "bug" on iOS9. Just use UIApplicationOpenSettingsURLString as URL string. Source: this forum post from Apples' eskimo.Propend
Apple will reject the app too using Private API.Azo

© 2022 - 2024 — McMap. All rights reserved.