我需要从我的应用中以编程方式打开“设置”。我搜索了SO,但是到处都有人说这是不可能的。但是今天我看到它已在Facebook应用程序中实现。上有一个按钮,UIAlertView
单击时会打开“设置”。因此,确实可以打开“设置”,我亲眼目睹了这一点。但是该怎么做呢?有谁知道Facebook是怎么做到的?
我需要从我的应用中以编程方式打开“设置”。我搜索了SO,但是到处都有人说这是不可能的。但是今天我看到它已在Facebook应用程序中实现。上有一个按钮,UIAlertView
单击时会打开“设置”。因此,确实可以打开“设置”,我亲眼目睹了这一点。但是该怎么做呢?有谁知道Facebook是怎么做到的?
Answers:
您不能,没有API调用可以做到这一点。
只有系统对话框(Apple框架中的对话框)才能打开设置应用程序。在iOS 5中,有一个应用程序网址方案可打开系统对话框,但Apple后来将其删除。
随着iOS 8的到来,您可以在应用程序页面上打开设置对话框。
if (&UIApplicationOpenSettingsURLString != NULL) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
else {
// Present some dialog telling the user to open the settings app.
}
在iOS 8上,您可以通过编程方式打开“设置”!
这是代码:
- (void)openSettings
{
BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
if (canOpenSettings) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}
如果您的应用程序具有自己的设置包,则将打开显示应用程序设置的设置。如果您的应用程序没有设置包,则将显示主设置页面。
在iOS 8上,您可以通过编程方式打开“设置”!
以下是“设置”应用中当前已知的URL的列表:
- (void) openSettings
{
if (&UIApplicationOpenSettingsURLString != nil)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
else
NSLog(@"UIApplicationOpenSettingsURLString is not available in current iOS version");
}
- prefs:root = General&path =关于
- prefs:root = General&path =可访问性
- prefs:root = AIRPLANE_MODE
- prefs:root = General&path = AUTOLOCK
- prefs:root = General&path = USAGE / CELLULAR_USAGE
- prefs:root =亮度
- prefs:root = General&path =蓝牙
- prefs:root = General&path = DATE_AND_TIME
- prefs:root = FACETIME
- prefs:root =常规
- prefs:root = General&path =键盘
- prefs:root =城堡
- prefs:root = CASTLE&path = STORAGE_AND_BACKUP
- prefs:root = General&path = INTERNATIONAL
- prefs:root = LOCATION_SERVICES
- prefs:root = ACCOUNT_SETTINGS
- prefs:root =音乐
- prefs:root = MUSIC&path = EQ
- prefs:root = MUSIC&path = VolumeLimit
- prefs:root = General&path =网络
- prefs:root = NIKE_PLUS_IPOD
- prefs:root =注意
- prefs:root = NOTIFICATIONS_ID
- prefs:root =电话
- prefs:root =照片
- prefs:root = General&path = ManagedConfigurationList
- prefs:root = General&path =重置
- prefs:root =声音和路径=铃声
- prefs:root = Safari
- prefs:root = General&path = Assistant
- prefs:root =声音
- prefs:root = General&path = SOFTWARE_UPDATE_LINK
- prefs:root =存储
- prefs:root = TWITTER
- prefs:root = General&path =用法
- prefs:root =视频
- prefs:root = General&path =网络/ VPN
- prefs:root =壁纸
- prefs:root = WIFI
- prefs:root = INTERNET_TETHERING
Location Services
。
Vito Ziv在Swift中的回答。
func openSettings() {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, completionHandler: nil)
}
此问题所指的Facebook应用程序中的警报是标准警报,当您设置UIRequiresPersistentWiFi时,iOS会自行显示在Info.plist文件为YES且用户在没有网络连接的情况下启动您的应用程序。
在这里总结讨论:
settings.bundle
为您的应用程序提供,则行为是什么?我发现当通过Xcode启动时,此URL带我到顶层“设置”页面。但是,当通过TestFlight时,它会转到我的(空)应用程序设置子页面。但是我没有创建一个settings.bundle
,并且我的.app
产品中没有一个...
UIRequiresPersistentWiFi
是我想要显示警报的内容。但是,有谁知道您是否可以在运行时以编程方式触发它而不是使用info.plist?@Richard Venable,是否有可以调用的方法,以便在单击特定按钮时实际上可以使用此方法?
关于prefs:root=
和的备忘录Prefs:root
。
是的,由于今天和URL计划,Apple拒绝了我们的应用程序。它们是私有API。prefs:root=
App-Prefs:root
(2018-06-29)
对于Swift 4
只有UIApplication.openSettingsURLString是打开设置的公共API。
UIApplication.openSettingsURLString
现在使用,并请老板修改此问题的UI规范。
if (&UIApplicationOpenSettingsURLString != NULL)
在iOS 8+中始终为TRUE。所以你可以这样编辑
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
(带有适用于iOS 10的新openURL)
如果要打开应用程序设置,则需要使用UIApplicationOpenSettingsURLString的write方法。
fileprivate func openSettings() {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
}
如果需要打开许多设置之一,则需要使用此功能
fileprivate func openSettings() {
UIApplication.shared.open(URL(string:"App-Prefs:root=General")!)
}
要在中打开我们自己的应用程序的设置iOS 8 and later
,请使用以下代码。
- (void) openSettings
{
if(&UIApplicationOpenSettingsURLString != nil)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
else
NSLog(@"UIApplicationOpenSettingsURLString is not available in current iOS version");
}
对于参考和详细说明,请遵循
if (&UIApplicationOpenSettingsURLString != NULL) {
UIAlertView_Blocks *alertView = [[UIAlertView_Blocks alloc] initWithTitle:NSLocalizedString(@"Camera Access Denied", nil)
message:NSLocalizedString(@"You must allow camera access in Settings", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
otherButtonTitles:NSLocalizedString(@"Open Settings", nil), nil];
[alertView showWithDissmissBlock:^(NSInteger buttonIndex) {
if (alertView.cancelButtonIndex != buttonIndex) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}];
}
else {
UIAlertView_Blocks *alertView = [[UIAlertView_Blocks alloc] initWithTitle:NSLocalizedString(@"Camera Access Denied", nil)
message:NSLocalizedString(@"You must allow camera access in Settings > Privacy > Camera", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil, nil];
[alertView showWithDissmissBlock:^(NSInteger buttonIndex) {
}];
}
这是带有UIAlertController和UIAlertAction的Swift 3示例。
func showSettingsPopup() {
let alertVC = UIAlertController(title: "Notifications disabled", message: "Please, turn on notifications if you want to receive a reminder messages.", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .destructive, handler: { (action) in
print("close action handler")
})
let openSettings = UIAlertAction(title: "Open settings", style: .default, handler: { (action) in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings are opened: \(success)")
})
}
})
alertVC.addAction(openSettings)
alertVC.addAction(close)
present(alertVC, animated: true, completion: nil)
}
而不是使用prefs:
,而是使用App-Prefs:
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString: @"App-Prefs:root=WIFI"]]) {
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"App-Prefs:root=WIFI"]];
}
。
一切都在这里:https: //medium.com/@thanhvtrn/how-to-open-settings-wifi-in-swift-on-ios-10-1d94cf2c2e60
在这里给出的许多答案中,我看到使用“ App-Prefs:root”和“ prefs:root”,我们不应该在我们的应用程序中使用这些来打开设置应用程序。根据苹果公司的说法,这是一个非公开的网址方案,如果我们使用它,我们的应用程序将被拒绝。
SWIFT 3
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)