objective-c 如何以编程方式打开iOS9中objective c中的WIFI设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33437815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:36:45  来源:igfitidea点击:

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

objective-ciphoneios9

提问by Helton Fernandes Sampaio

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

我正在尝试使用 Objective-C 通过我的应用程序访问 WIFI 设置。但是找不到任何办法。有人可以帮助我吗?

Already tested with:

已经测试过:

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

Does not work on iOS 9.

不适用于 iOS 9。

采纳答案by Fernando García Corrochano

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/3668465did

尝试向 URL 方案添加首选项,如https://stackoverflow.com/a/31253743/3668465所做的

回答by Karthick Ramesh

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.

根据 Apple 的 New Review 标准,我们不应该使用这种方式打开 Wi-Fi 设置。我已经在我的应用程序中使用了很长时间,最近苹果拒绝了以下评论。

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.

您的应用程序使用“prefs:root=”非公共 URL 方案,这是一个私有实体。App Store 不允许使用非公共 API,因为如果这些 API 发生变化,可能会导致糟糕的用户体验。

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

因此,您可以使用 UIApplicationOpenSettingsURLString 导航到应用程序的设置。

Swift Code:

SWIFT代码:

if let settingsUrl = URL.init(string: UIApplicationOpenSettingsURLString), UIApplication.shared.canOpenURL(settingsUrl) {
                    UIApplication.shared.openURL(settingsUrl)
                }

回答by Shuvo Joseph

This works fine on iOS 10,

这在 iOS 10 上运行良好,

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

转到目标-->(应用程序)--> 信息--> URL 类型--> +

In the URL Schemeswrite

URL Schemes

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"]];
    }
}

回答by Vinod Sutar

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

回答by Pierre

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

回答by Bart

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

您无法使用 openURL 直接进入 wifi 设置。您所能做的就是打开您自己的应用程序的设置。

if (&UIApplicationOpenSettingsURLString != nil) {
   NSURL *URL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
   [[UIApplication sharedApplication] openURL:URL];
} else {
  ...
}

回答by Alessandro Francucci

Swift 4.2, iOS 12

斯威夫特 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()

用法: 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!

我还使用了非公共 URL 方案,例如:prefs:root=,但我的应用程序被拒绝了。因此,如果您想通过深层链接做更具体的事情,请不要浪费时间,因为目前您做不到!

回答by pedro.olimpio

You can use this option:

您可以使用此选项:

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

iOS >= 4.1 可以获取设备当前连接的无线网络的 SSID。

For this you'd use function CNCopyCurrentNetworkInfo

为此,您将使用函数CNCopyCurrentNetworkInfo

Details on implemenation: iPhone get SSID without private library

实现细节:iPhone无需私有库即可获取SSID