如何像在Facebook应用中一样以编程方式打开设置?


75

我需要从我的应用中以编程方式打开“设置”。我搜索了SO,但是到处都有人说这是不可能的。但是今天我看到它已在Facebook应用程序中实现。上有一个按钮,UIAlertView单击时会打开“设置”。因此,确实可以打开“设置”,我亲眼目睹了这一点。但是该怎么做呢?有谁知道Facebook是怎么做到的?


请注意,Facebook应用程序是开源的,可在github上完全使用。
苏珊(Sulthan)

Facebook没有显示此警报,而操作系统已显示。在某些情况下会发生这种情况,例如打开飞行模式或关闭WiFi,并且操作系统会为您提供打开设置并进行更改的功能。
Guy Kogus 2014年

9
@Sulthanthe请注意,Facebook应用程序不是开源的。只有SDK。
MatterGoal


请访问Swift 3的答案:http
//stackoverflow.com/a/34024467/5391914

Answers:


57

您不能,没有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.
}

5
好吧,但是Facebook
确实做到了

3
那是因为他们与Apple紧密合作,并且Apple在iOS中集成了该服务。这并不意味着您可以做到!他们甚至可能被允许调用某些私有API来打开设置应用。
rckoenes,2014年

3
我很确定这只是应用程序的正常部分,就像“设置”页面一样,看起来很像。你懂?
Fattie 2014年

9
大家好,在iOS 8上可以以编程方式打开“设置”,请参阅我的答案
Vito Ziv 2014年

2
@VitoZiv是正确的,但是当问这个问题时,我的回答是正确的。
rckoenes 2014年

139

在iOS 8上,您可以通过编程方式打开“设置”!

这是代码:

- (void)openSettings
{
    BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
    if (canOpenSettings) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url];
    }
}

如果您的应用程序具有自己的设置包,则将打开显示应用程序设置的设置。如果您的应用程序没有设置包,则将显示主设置页面。


@BeemerFan不仅是您自己的应用程序的设置页面。
rckoenes 2014年

3
有谁知道如何仅打开“设置”应用而非单个设置页面?Facebook将打开“设置”应用程序,而不会直接转到各个设置页面。
2015年

7
注意:如果您尚未设置设置包,则此代码仍会打开应用程序设置(带有“通知”和“使用移动数据”),而不是常规设置。
MatterGoal 2015年

1
如何删除应用程序设置,以便打开常规设置?安装应用程序时,它已在ios8中存在。
vatti 2015年

2
您能告诉我如何在xcode上删除应用程序设置包吗?我总是转到应用程序的“设置”,而不是常规设置
Raju yourPepe

33

在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

2
您可以为此链接到任何官方的Apple来源,还是通过某种方式发现而发现的?
乔伊·卡森

1
似乎其中大多数不再在iOS 10中正常工作 stackoverflow.com/questions/38064557/…–
wyu

6
只需将首选项替换为iOS 10的App-Prefs即可。以上代码适用于iOS 8,9,10。
斯大林·帕斯帕拉杰(Starin Pusparaj),

1
“ App-Prefs:root = Privacy&path = Location”特定于Location Services
dichen

1
您的应用将因使用prefs:root而被苹果拒绝:
Helam

20

Vito Ziv在Swift中的回答。

func openSettings() {
    UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, completionHandler: nil)

}

我们如何进入wifi设置,以便用户在应用程序中重新连接wifi?
JMStudios.jrichardson '16

1
@ JMStudios.jrichardson还没有办法。仅应用程序设置
alexey.metelkin's

1
Swift3中现已弃用openURL,有人对此有更新的示例吗?
Geoff

13

此问题所指的Facebook应用程序中的警报标准警报,当您设置UIRequiresPersistentWiFi,iOS会自行显示在Info.plist文件为YES且用户在没有网络连接的情况下启动您的应用程序。

在这里总结讨论:

  • 没有可用于进入根目录级别的URL“设置”应用程序的。
  • 可以使用iOS 8中的UIApplicationOpenSettingsURLString将用户发送到“设置”应用的应用部分
  • 通过将UIRequiresPersistentWiFi设置为YES,您的应用程序可以显示与Facebook相同的警报,Facebook会打开“设置”应用程序的根级别。

>>>您的“设置”应用程序中的应用程序部分<<<如果不settings.bundle为您的应用程序提供,则行为是什么?我发现当通过Xcode启动时,此URL带我到顶层“设置”页面。但是,当通过TestFlight时,它会转到我的(空)应用程序设置子页面。但是我没有创建一个settings.bundle,并且我的.app产品中没有一个...
pkamb 2015年

至于做相同的事情,Facebook做这应该是正确的答案。UIRequiresPersistentWiFi是我想要显示警报的内容。但是,有谁知道您是否可以在运行时以编程方式触发它而不是使用info.plist?@Richard Venable,是否有可以调用的方法,以便在单击特定按钮时实际上可以使用此方法?
user3832583 '16

11

关于prefs:root=和的备忘录Prefs:root

是的,由于今天和URL计划,Apple拒绝了我们的应用程序。它们是私有API。prefs:root=App-Prefs:root(2018-06-29)

被苹果拒绝


对于Swift 4

只有UIApplication.openSettingsURLString是打开设置的公共API。


2018年8月14日-我们的应用也因使用该应用而被拒绝。在他们最终拒绝我们之前,它已经进入了3个发行版的代码库。
卡梅伦E

你们在这里找到任何解决方案了吗?
Sukeshj

否。我UIApplication.openSettingsURLString现在使用,并请老板修改此问题的UI规范。
AechoLiu

7
 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)


5

如果要打开应用程序设置,则需要使用UIApplicationOpenSettingsURLString的write方法。

fileprivate func openSettings() {
  UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)      
}

如果需要打开许多设置之一,则需要使用此功能

fileprivate func openSettings() {
  UIApplication.shared.open(URL(string:"App-Prefs:root=General")!)
}

不要在您的应用程序中使用“ App-Prefs:root = General”。苹果将​​拒绝您的应用程序在应用程序中使用非公开网址。幸亏有regected
拉维·库马尔·

3

要在中打开我们自己的应用程序的设置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");
}

对于参考和详细说明,请遵循

在iOS 8中以编程方式打开“设置”应用


2
 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) {
            }];
        }

1

这是带有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)
}

0
if #available(iOS 10.0, *) {
        if let url = URL(string: "App-Prefs:root=Privacy") {
            UIApplication.shared.open(url, completionHandler: .none)
        }
    } else {
        // Fallback on earlier versions
    }

在应用程序中打开隐私设置



0

在这里给出的许多答案中,我看到使用“ App-Prefs:root”和“ prefs:root”,我们不应该在我们的应用程序中使用这些来打开设置应用程序。根据苹果公司的说法,这是一个非公开的网址方案,如果我们使用它,我们的应用程序将被拒绝。


-1

SWIFT 3

UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)

openURL在swift 3中已过时,您应该使用open(URL(string:UIApplicationOpenSettingsURLString)!,选项:[:],completionHandler:{_ in})
Damian Dudycz
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.