在iOS开发过程中,有时需要连接网络。当访问请求,检测到网络不可用时,需要提示用户手动进行设置网络并告知用户操作路径设置可用的网络。
只需一行代码即可实现:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(50, 100, 100, 30);
[self.view addSubview:button];
[button addTarget:self action:@selector(pushSetting) forControlEvents:UIControlEventTouchUpInside];
}
- (void)pushSetting{
NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
BOOL isCanOpenURL = [[UIApplication sharedApplication] openURL:url];//一行代码实现跳转
}
另补充:在项目中的info.plist中添加 URL types(NSArray) 并在item0(NSDictionary)中选出键为URL Schemes(NSArray),然后设置item0对应的值为prefs。或者直接在info项中的URL Types点击+直接设置URL Schemes
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=Restrictions"]];
http://*.com/questions/8246070/ios-launching-settings-restrictions-url-scheme
List of currently known URLs in the Settings app:
- prefs:root=General&path=About
- prefs:root=General&path=ACCESSIBILITY
- prefs:root=AIRPLANE_MODE
- prefs:root=General&path=AUTOLOCK
- prefs:root=General&path=USAGE/CELLULAR_USAGE
- prefs:root=Brightness
- prefs:root=General&path=Bluetooth
- prefs:root=General&path=DATE_AND_TIME
- prefs:root=FACETIME
- prefs:root=General
- prefs:root=General&path=Keyboard
- prefs:root=CASTLE
- prefs:root=CASTLE&path=STORAGE_AND_BACKUP
- prefs:root=General&path=INTERNATIONAL
- prefs:root=LOCATION_SERVICES
- prefs:root=ACCOUNT_SETTINGS
- prefs:root=MUSIC
- prefs:root=MUSIC&path=EQ
- prefs:root=MUSIC&path=VolumeLimit
- prefs:root=General&path=Network
- prefs:root=NIKE_PLUS_IPOD
- prefs:root=NOTES
- prefs:root=NOTIFICATIONS_ID
- prefs:root=Phone
- prefs:root=Photos
- prefs:root=General&path=ManagedConfigurationList
- prefs:root=General&path=Reset
- prefs:root=Sounds&path=Ringtone
- prefs:root=Safari
- prefs:root=General&path=Assistant
- prefs:root=Sounds
- prefs:root=General&path=SOFTWARE_UPDATE_LINK
- prefs:root=STORE
- prefs:root=TWITTER
- prefs:root=General&path=USAGE
- prefs:root=VIDEO
- prefs:root=General&path=Network/VPN
- prefs:root=Wallpaper
- prefs:root=WIFI
- prefs:root=INTERNET_TETHERING