how to programmatically open the WIFI settings in objective c in iOS9

蓝咒 提交于 2019-11-26 20:07:48

问题


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.


回答1:


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




回答2:


This works fine on iOS 10,

Go to Targets --> (Application) --> Info --> URL Types --> +

In the URL Schemes write

prefs

Then Call,

- (void)openWifiSettings
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=WIFI"]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=WIFI"]];
    }
}



回答3:


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)
                }



回答4:


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.
    }



回答5:


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 {
  ...
}



回答6:


//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];



回答7:


Swift 4.2, iOS 12

This is the function that I'm currently using in my app for it:

extension UIApplication {

    ...

    @discardableResult
    static func openAppSetting() -> Bool {
        guard
            let settingsURL = URL(string: UIApplication.openSettingsURLString),
            UIApplication.shared.canOpenURL(settingsURL)
            else {
                return false
        }

        UIApplication.shared.open(settingsURL)
        return true
    }
}

Usage: UIApplication.openAppSetting()

I also used non-public URL scheme, such as: prefs:root=, but my app was rejected. So if you're trying to do more specific stuff with deeplinking, don't waste your time because at the moment you can't!




回答8:


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



来源:https://stackoverflow.com/questions/33437815/how-to-programmatically-open-the-wifi-settings-in-objective-c-in-ios9

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