How to programmatically open the WIFI settings in Objective-C on iOS 10

旧街凉风 提交于 2019-11-29 06:54:05
Ahmad F

The same exact code should work, but for iOS 10 you need to do some additional work by adding "prefs" to the URL Types:

After selecting your target:

  • Navigate to "Info" tab.
  • After scrolling to bottom, you should see "URL Types" section.
  • Add a new one (by clicking on the plus button) and fill the "URL Schemes" with "prefs".

It should be similar to this:

Now, your code should works fine.

UPDATE:

If it -somehow- did not work as expected, you might want to follow this workaround.

Hope that helped.

In iOS 10, a new url is required. Try using this code which tests both urls :

NSArray* urlStrings = @[@"prefs:root=WIFI", @"App-Prefs:root=WIFI"];
for(NSString* urlString in urlStrings){
    NSURL* url = [NSURL URLWithString:urlString];
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url];
        break;
    }
}

This works fine on iOS 10,

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

In the URL Schemes write

prefs

See the image,

Then add the following code,

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

Try this one :

    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
        }];
    }

Thanks :)

krishna

For Swift:

let url = URL(string: "App-Prefs:root=WIFI")

    if UIApplication.shared.canOpenURL(url!){
        UIApplication.shared.openURL(url!)

    }

Swift 4.2, iOS 12

It's not longer possible to do that kind of deeplinking with the newer version of iOS. My app was recently rejected for using: "non-public URL scheme", such as: prefs:root=. So I'd say to not waste your time with something that we can't currently do, and simply open the settings.

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: UIApplicationOpenSettingsURLString),
            UIApplication.shared.canOpenURL(settingsURL)
            else {
                return false
        }

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

Usage: UIApplication.openAppSetting()

 let url=URL(string: "App-Prefs:root=WIFI")
// you can change root as your requirements 
        if UIApplication.shared.canOpenURL(url!)
        {
            UIApplication.shared.open(url!, options: [:], completionHandler: {success in

            })

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

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