如何在ios中以编程方式打开设置

本文关键字:编程 方式打 设置 ios | 更新日期: 2023-09-27 17:50:36

我正在寻找如何以编程方式打开设置的Xamarin实现

Vito-ziv为objective C回答了这个问题-在Xamarin Studio的iOS c#中正确的方法是什么?

如何在ios中以编程方式打开设置

对于目前的设备,这只能在ios8中实现(ios9在撰写本文时不可用)(这在ios5之前显然是可能的-请参阅Xamarin的Adrian Stevens的博客文章-向他大声呼喊以获得这个答案的灵感)

在ios8中,我这样做了:

var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
            var url = new NSUrl (settingsString);
            UIApplication.SharedApplication.OpenUrl (url);

上面的代码是通过UIAlertView点击中的委托类从点击事件中调用的。

因为我也支持ios7,为了处理ios7设备,我这样做了,在决定是否呈现视图控制器时调用HandleLocationAuthorisation方法- ios8及以上的用户可以选择直接进入设置,而ios7上的用户必须手动去那里。

下面的例子是检查位置服务,但可以很容易地更改为检查其他类型的设置。

public bool HandleLocationAuthorisation () 
{
    if (CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways) {
        return true;
    } else {
        UIAlertView uiAlert;  

        //iOS 8 and above can redirect to settings from within the app
        if (UIDevice.CurrentDevice.CheckSystemVersion(8,0)) {
            uiAlert = new UIAlertView 
                ("Location Services Required", 
                    "",
                    null,
                    "Return To App","Open Settings");
            uiAlert.Delegate = new OpenSettingsFromUiAlertViewDelegate();
            uiAlert.Message = "Authorisation to use your location is required to use this feature of the app.";
        //ios7 and below has to go there manually
        } else {
            uiAlert = new UIAlertView 
                ("Location Services Required", 
                    "Authorisation to use your location is required to use this feature of the app. To use this feature please go to the settings app and enable location services",
                    null,
                    "Ok");
        }
        uiAlert.Show ();
        return false;
    }
}

为了完整起见,下面是上面引用的事件委托的代码:

public class OpenSettingsFromUiAlertViewDelegate : UIAlertViewDelegate {
public override void Clicked (UIAlertView alertview, nint buttonIndex)
{
    if (buttonIndex == 1) {
        var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
        var url = new NSUrl (settingsString);
        UIApplication.SharedApplication.OpenUrl (url);
    }
  }
}

希望对您有所帮助。这是在iPhone上工作,不确定在iPad上工作

var url = new NSUrl("prefs:root=Settings");
UIApplication.SharedApplication.OpenUrl(url);