【问题标题】:How to open Settings programmatically like in Facebook app?如何像在 Facebook 应用程序中一样以编程方式打开设置?
【发布时间】:2014-07-12 12:11:23
【问题描述】:

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

【问题讨论】:

  • 请注意,Facebook 应用程序是开源的,完全可以在 github 上获得。
  • Facebook 没有显示此警报,操作系统做到了。它发生在某些情况下,例如打开飞行模式或关闭 WiFi,并且操作系统会为您提供打开设置并进行更改。
  • @Sulthan 请注意 Facebook 应用程序不是开源的。只有 SDK 是。
  • 访问 Swift 3 的这个答案:http://stackoverflow.com/a/34024467/5391914

标签: ios objective-c


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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


    对于 Swift 4,

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

    【讨论】:

    • 八月。 2018 年 14 月 - 我们的应用也因使用它而被拒绝。在他们最终拒绝我们之前,它已经在代码库中发布了 3 个版本。
    • 你们找到解决办法了吗?
    • 没有。我现在使用UIApplication.openSettingsURLString,并要求我的老板针对这个问题修改UI规范。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      不要使用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

      【讨论】:

        【解决方案5】:

        Vito Ziv 在 Swift 中的回答。

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

        【讨论】:

        • 我们如何进入 wifi 设置以便用户在应用中重新连接 wifi?
        • @JMStudios.jrichardson 目前还没有办法。仅限应用设置
        • openURL 现在在 Swift3 中已被弃用,有人有更新的示例吗?
        【解决方案6】:

        SWIFT 3

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

        【讨论】:

        • openURL 在 swift 3 中已经过时了,你应该使用 open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: { _ in })
        【解决方案7】:
        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
            }
        

        在 App 中打开隐私设置

        【讨论】:

          【解决方案8】:

          如果您想要打开应用程序设置,您需要使用 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”。苹果将​​拒绝您的应用程序在您的应用程序中使用非公共网址。我的被​​接受了
          【解决方案9】:
           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)

          【讨论】:

            【解决方案10】:

            在 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=About
            • prefs:root=General&path=ACCESSIBILITY
            • 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=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=照片
            • 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=声音
            • 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=壁纸
            • prefs:root=WIFI
            • prefs:root=INTERNET_TETHERING

            【讨论】:

            • 您能否为此链接到任何官方的 Apple 资源,或者它是通过某种方式探索发现的?
            • 看起来其中的大多数不再适用于 iOS 10 stackoverflow.com/questions/38064557/…
            • 只需将首选项替换为 iOS 10 的 App-Prefs。以上代码适用于 iOS 8、9、10。
            • "App-Prefs:root=Privacy&path=Location" 特定于Location Services
            • 你的应用会因为使用 prefs:root 而被苹果拒绝
            【解决方案11】:

            要在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");
            }
            

            参考和详细说明请关注

            Opening the Settings app programmatically in iOS 8

            【讨论】:

              【解决方案12】:
               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) {
                          }];
                      }
              

              【讨论】:

                【解决方案13】:

                当您在 Info.plist 文件中将 UIRequiresPersistentWiFi 设置为 YES 并且用户启动您的应用程序时,此问题所指的 Facebook 应用程序中的警报是 iOS 显示自己的标准警报网络连接。

                在这里总结讨论:

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

                【讨论】:

                • >>> 设置应用程序中您的应用程序部分 settings.bundle,会出现什么行为?我发现当通过 Xcode 启动时,此 URL 会将我带到顶级设置页面。但是当通过 TestFlight 时,它会转到我的(空)应用程序设置子页面。但是我没有创建settings.bundle,我的.app 产品中也没有...
                • 就 facebook 做同样的事情而言,这应该是正确的答案。 UIRequiresPersistentWiFi 是我要显示警报的内容。但是,有谁知道您是否可以在运行时以编程方式触发它而不是使用 info.plist? @Richard Venable 你是否有一种方法可以调用,以便在单击特定按钮时实际使用它?
                【解决方案14】:

                你不能,没有 API 调用可以做到这一点。

                只有系统对话框,来自 Apple 框架的对话框,才能打开设置应用程序。 在 iOS 5 中,有一个应用程序 url 方案可以打开系统对话框,但苹果后来删除了它。


                随着 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.
                }
                

                【讨论】:

                • 好吧,但 Facebook 做到了
                • 那是因为他们与 Apple 的合作非常密切,并且 Apple 在 iOS 中集成了那里的服务。这并不意味着你可以做到!他们甚至可能被允许调用一些私有 API 来打开设置应用程序。
                • 我很确定这只是应用程序的一个正常部分,看起来像设置页面。你知道吗?
                • 大家好,在 iOS 8 上以编程方式打开设置是可能的,请参阅我的回答
                • @VitoZiv 没错,但是当被问到这个问题时,我的回答是正确的。
                【解决方案15】:

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

                代码如下:

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

                如果您的应用有自己的设置包,则会打开设置并显示您的应用的设置。如果您的应用没有设置包,则会显示主设置页面。

                【讨论】:

                • @BeemerFan 不仅是您自己应用的设置页面。
                • 有谁知道如何只打开设置应用而不是个人设置页面? Facebook 打开“设置”应用程序,而不指向个人设置页面。
                • 注意:如果您没有设置设置包,此代码仍会打开应用设置(带有“通知”和“使用蜂窝数据”),而不是通用设置。
                • 如何删除应用设置,以便打开常规设置?安装应用程序时,它已经存在于 ios8 中。
                • 您能告诉我如何在 xcode 上删除应用程序设置包吗?我总是去应用程序设置而不是常规设置
                猜你喜欢
                • 2012-06-07
                • 2015-09-21
                • 1970-01-01
                • 2015-12-08
                • 2014-01-17
                • 2013-01-13
                • 1970-01-01
                • 2018-10-10
                相关资源
                最近更新 更多