ios 如何像在 Facebook 应用程序中一样以编程方式打开设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23824054/
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
How to open Settings programmatically like in Facebook app?
提问by Andrey Chernukha
I need to open Settings programmatically from within my app. I searched across SO but everywhere people say that it's impossible. But today I saw that it's implemented in Facebook app. There's a button on an UIAlertView
and when you click it you open the Settings. So indeed this is possible to open Settings, I have witnessed this myself. But how to do that? Does anyone know how Facebook does that?
我需要从我的应用程序中以编程方式打开设置。我搜索了 SO,但到处都有人说这是不可能的。但是今天我看到它已在 Facebook 应用程序中实现。上有一个按钮UIAlertView
,当您单击它时,您会打开“设置”。所以确实可以打开设置,我亲眼目睹了这一点。但是怎么做呢?有谁知道 Facebook 是如何做到的?
采纳答案by rckoenes
You can't, there is no API call to do this.
你不能,没有 API 调用可以做到这一点。
Only system dialogs, dialogs from Apple Frameworks, can open the settings app. In iOS 5 there was a app url scheme to open the system dialog but Apple removed it later.
只有系统对话框,来自 Apple Frameworks 的对话框,才能打开设置应用程序。在 iOS 5 中,有一个应用程序 url 方案可以打开系统对话框,但 Apple 稍后将其删除。
With the coming of iOS 8 you can open the settings dialog on your apps page.
随着 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.
}
回答by Vito Ziv
On iOS 8 you can open Settings programmatically!
在 iOS 8 上,您可以通过编程方式打开设置!
Here is the code:
这是代码:
- (void)openSettings
{
BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
if (canOpenSettings) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}
If your app has it's own settings bundle, the settings will be opened showing your app's settings. If your app does not have a setting bundle, the main settings page will be shown.
如果您的应用程序有自己的设置包,则会打开设置并显示您的应用程序设置。如果您的应用程序没有设置包,则会显示主设置页面。
回答by Stalin Pusparaj
On iOS 8 you can open Settings programmatically!
在 iOS 8 上,您可以通过编程方式打开设置!
Here is the list of currently known URLs in the Settings app:
以下是“设置”应用中当前已知的 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=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
- prefs:root=General&path=关于
- prefs:root=General&path=ACCESSIBILITY
- 首选项:root=AIRPLANE_MODE
- prefs:root=General&path=AUTOLOCK
- prefs:root=General&path=USAGE/CELLULAR_USAGE
- 首选项:根=亮度
- prefs:root=General&path=蓝牙
- 首选项:root=General&path=DATE_AND_TIME
- 首选项:root=FACETIME
- 首选项:root=一般
- prefs:root=General&path=Keyboard
- 首选项:root = CASTLE
- 首选项:root=CASTLE&path=STORAGE_AND_BACKUP
- prefs:root=General&path=INTERNATIONAL
- 首选项:root=LOCATION_SERVICES
- 首选项:root = ACCOUNT_SETTINGS
- 首选项:root=音乐
- 首选项:root=音乐&路径=EQ
- prefs:root=MUSIC&path=VolumeLimit
- prefs:root=General&path=Network
- 首选项:root=NIKE_PLUS_IPOD
- 首选项:root=注意
- 首选项:root=NOTIFICATIONS_ID
- 首选项:root=电话
- 首选项:root=照片
- prefs:root=General&path=ManagedConfigurationList
- prefs:root=General&path=Reset
- prefs:root=声音&路径=铃声
- 首选项:root = Safari
- prefs:root=General&path=Assistant
- prefs:root=声音
- prefs:root=General&path=SOFTWARE_UPDATE_LINK
- 首选项:root = STORE
- 首选项:root=推特
- prefs:root=General&path=USAGE
- 首选项:根=视频
- prefs:root=General&path=网络/VPN
- 首选项:root=壁纸
- 首选项:root=WIFI
- 首选项:root=INTERNET_TETHERING
回答by Iraklis Moutidis
Vito Ziv's answer in Swift.
Vito Ziv 在 Swift 中的回答。
func openSettings() {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, completionHandler: nil)
}
回答by Richard Venable
The alert in the Facebook app that this question refers to is a standard alert which iOS displays itselfwhen you set UIRequiresPersistentWiFito YES in your Info.plist files and the user launches your app without a network connection.
此问题所指的 Facebook 应用程序中的警报是一个标准警报,当您在 Info.plist 文件中将UIRequiresPersistentWiFi设置为 YES 并且用户在没有网络连接的情况下启动您的应用程序时,iOS 会自行显示该警报。
To summarize the discussion here:
总结一下这里的讨论:
- There is no URLyou can use to get to the root levelof the Settings app.
- You cansend the user to your app's sectionof the Settings app using UIApplicationOpenSettingsURLStringin iOS 8.
- Your app can display the same alert as Facebook, which does open the root level of the Settings app, by setting UIRequiresPersistentWiFi to YES.
- 没有可用于访问“设置”应用程序根级别的URL。
- 您可以在 iOS 8 中使用UIApplicationOpenSettingsURLString将用户发送到设置应用程序的应用程序部分。
- 通过将 UIRequiresPersistentWiFi 设置为 YES,您的应用程序可以显示与 Facebook 相同的警报,它会打开设置应用程序的根级别。
回答by AechoLiu
A memo about prefs:root=
and Prefs:root
.
Yes, our App was REJECTED by Appledue to prefs:root=
and App-Prefs:root
URL scheme today (2018-06-29)
. They are private API.
关于prefs:root=
和的备忘录Prefs:root
。
是的,我们的应用程序是由苹果拒绝因prefs:root=
和App-Prefs:root
URL方案今天(2018-06-29)
。它们是私有 API。
For Swift 4,
对于 Swift 4,
Only UIApplication.openSettingsURLStringis public API to open Settings.
只有UIApplication.openSettingsURLString是用于打开设置的公共 API。
回答by Joannes
if (&UIApplicationOpenSettingsURLString != NULL)
in iOS 8+ is always TRUE. So you can edit like this
在 iOS 8+ 中总是正确的。所以你可以像这样编辑
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
(with new openURL for iOS 10)
(带有适用于 iOS 10 的新 openURL)
回答by user1064264
If you want open application settings you need write method with UIApplicationOpenSettingsURLString.
如果您想打开应用程序设置,您需要使用 UIApplicationOpenSettingsURLString 编写方法。
fileprivate func openSettings() {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
}
If you need will open one of many settings, you need use this function
如果您需要将打开许多设置之一,则需要使用此功能
fileprivate func openSettings() {
UIApplication.shared.open(URL(string:"App-Prefs:root=General")!)
}
回答by Aamir
To open settings of our own application in iOS 8 and later
, use following code.
要在 中打开我们自己的应用程序的设置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");
}
For reference and detailed description please follow
如需参考和详细说明,请遵循
回答by IvanovDeveloper
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) {
}];
}