how to programmatically open the WIFI settings in

2019-01-14 03:49发布

I'm trying to access the WIFI settings through my application using Objective-C. But can not find any way. Could someone help me?

Already tested with:

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

Does not work on iOS 9.

8条回答
不美不萌又怎样
2楼-- · 2019-01-14 04:08

This is my code

if (&UIApplicationOpenSettingsURLString != NULL) { 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]]; 
} 

Try to add prefs to URL schemes like https://stackoverflow.com/a/31253743/3668465 did

查看更多
在下西门庆
3楼-- · 2019-01-14 04:11

As per Apple's New Review standards, we are not supposed to use this way to open Wi-Fi Settings. I have been using this for long time in my app and recently Apple rejected with the below comment.

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.

So you can just navigate to settings of the app by using UIApplicationOpenSettingsURLString.

Swift Code:

if let settingsUrl = URL.init(string: UIApplicationOpenSettingsURLString), UIApplication.shared.canOpenURL(settingsUrl) {
                    UIApplication.shared.openURL(settingsUrl)
                }
查看更多
Evening l夕情丶
4楼-- · 2019-01-14 04:18
//Pre iOS 10
NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
if (![[UIApplication sharedApplication] canOpenURL:url])
{   //iOS 10+
    url = [NSURL URLWithString:@"App-Prefs:root=WIFI"];
}
[[UIApplication sharedApplication] openURL:url];
查看更多
放荡不羁爱自由
5楼-- · 2019-01-14 04:20

All conditions:

    NSURL * urlCheck1 = [NSURL URLWithString:@"App-Prefs:root=WIFI"];
    NSURL * urlCheck2 = [NSURL URLWithString:@"prefs:root=WIFI"];
    NSURL * urlCheck3 = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

    if ([[UIApplication sharedApplication] canOpenURL:urlCheck1])
    {
        [[UIApplication sharedApplication] openURL:urlCheck1];
    }
    else if ([[UIApplication sharedApplication] canOpenURL:urlCheck2])
    {
        [[UIApplication sharedApplication] openURL:urlCheck2];
    }
    else if ([[UIApplication sharedApplication] canOpenURL:urlCheck3])
    {
        [[UIApplication sharedApplication] openURL:urlCheck3];
    }
    else
    {
        //Unable to open settings app.
    }
查看更多
欢心
6楼-- · 2019-01-14 04:22

You can't get straight to wifi setting with openURL. All you can do is to open settings for your own app.

if (&UIApplicationOpenSettingsURLString != nil) {
   NSURL *URL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
   [[UIApplication sharedApplication] openURL:URL];
} else {
  ...
}
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-14 04:25

You can use this option:

iOS >= 4.1 it's possible to obtain SSID of wireless network that device is currenctly connected to.

For this you'd use function CNCopyCurrentNetworkInfo

Details on implemenation: iPhone get SSID without private library

查看更多
登录 后发表回答