【问题标题】:Open Settings app from another app programmatically in iPhone在 iPhone 中以编程方式从另一个应用程序打开设置应用程序
【发布时间】:2014-01-17 08:54:22
【问题描述】:

如果 iPhone 中未启用 gps,我必须从我的应用程序中打开设置应用程序。我使用了以下代码。它在 iOS 模拟器中运行良好,但在 iPhone 中无法运行。我可以知道这段代码有什么问题吗?

if (![CLLocationManager locationServicesEnabled]) {
        int (*openApp)(CFStringRef, Boolean);
        void *hndl = dlopen("/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices");
        openApp = (int(*)(CFStringRef, Boolean)) dlsym(hndl, "SBSLaunchApplicationWithIdentifier");
        openApp(CFSTR("com.apple.Preferences"), FALSE);
        dlclose(hndl);
    }

【问题讨论】:

  • 这是非法的,请在越狱设备中尝试。 Apple 从 iOS 5 + 中删除了此功能
  • 我认为路径可能与 sim 和 device 不同。这个应用会被提交到 appStore 吗?

标签: ios iphone gps settings


【解决方案1】:

好消息:

您可以像这样以编程方式打开设置应用程序(仅适用于 iOS8 及更高版本)。

如果您使用的是 Swift 3.0:

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

如果你使用的是 Objective-C:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

对于其他较低版本(低于 iOS8),无法以编程方式打开设置应用程序。

【讨论】:

  • 这个回退是否适合旧版本?
  • 可以打开特定的设置界面吗?
  • @nume 目前不可能。
  • 要让您的代码在 iOS 7 上运行,首先检查 UIApplicationOpenSettingsURLString 是否存在,如下所示:stackoverflow.com/a/25884389/72176
  • @nurne - 不,上面的代码将打开您应用的设置。
【解决方案2】:

正如其他人回答的那样,您无法从您的应用中打开“设置”。

但是你可以像我一样解决这个问题:

输出必须启用位置服务的消息,解释原因,并在该消息中显示路径:

“设置->隐私->定位服务”

【讨论】:

  • 不幸的是,这仍然是正确的 =(...不知道为什么这在 iOS 5.1 上被弃用了
【解决方案3】:

只能从 iOS 8 以编程方式打开设置应用程序。因此,请使用以下代码...

if([CLLocationManager locationServicesEnabled]&&
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
  //...Location service is enabled
}
else
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
    {
       UIAlertView* curr1=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
      [curr1 show];
    }
    else
    {
       UIAlertView* curr2=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Settings", nil];
       curr2.tag=121;
       [curr2 show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (alertView.tag == 121 && buttonIndex == 1)
 {
  //code for opening settings app in iOS 8
   [[UIApplication sharedApplication] openURL:[NSURL  URLWithString:UIApplicationOpenSettingsURLString]];
 }
}

【讨论】:

    【解决方案4】:

    在 iOS 5.0 之前,可以通过 URL schema 打开 settings,即

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"My Settings URL"]];
    

    这已从 iOS 5.1 开始弃用。

    【讨论】:

      【解决方案5】:

      这是一个适用于我的 Swift2 版本,其中包括一个提醒用户在设置打开时该做什么的提示。

      func initLocationManager() {
          locationManager = CLLocationManager()
          locationManager.delegate = self
          locationManager.desiredAccuracy = kCLLocationAccuracyBest
          locationManager.requestAlwaysAuthorization()
      
      
      // If there isn't a Lat/Lon then we need to see if we have access to location services
      // We are going to ask for permission to use location if the user hasn't allowed it yet.
      let status = CLLocationManager.authorizationStatus()
      if(status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied)  {
      
          //println(locationManager)
      
          //  check that locationManager is even avaiable.  If so, then ask permission to use it
          if locationManager != nil {
              locationManager.requestAlwaysAuthorization()
      
              //open the settings to allow the user to select if they want to allow for location settings.
              let alert = UIAlertController(title: "I Can't find you.", message: "To let my App figure out where you are on the map click 'Find Me' and change your location to 'Always' and come back to MyMobi.", preferredStyle: UIAlertControllerStyle.Alert)
              alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler:nil))
              alert.addAction(UIAlertAction(title: "Find Me", style: UIAlertActionStyle.Default, handler: {
                  (alert: UIAlertAction!) in
                  UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
              }))
              self.presentViewController(alert, animated: true, completion: nil)
      
      
          }
      }
      }
      

      【讨论】:

        【解决方案6】:

        openURL 在 iOS10.0 中已弃用:请改用 openURL:options:completionHandler

        let url = URL(string: UIApplicationOpenSettingsURLString)!
        UIApplication.shared.open(url, options: [:]) { success in }
        

        【讨论】:

        • options: 和结尾的completionHandler: 是不必要的
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-18
        • 2013-05-16
        • 2011-08-05
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多