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

C# 如何在ios中以编程方式打开设置,c#,ios,xamarin,C#,Ios,Xamarin,我正在搜索Xamarin的实现 为目标C回答-在C中,为Xamarin Studio中的iOS做这件事的正确方法是什么?对于当前的设备,这只能在ios8中实现(在撰写本文时,ios9不可用)(显然,在ios5之前,这是可能的-请参见来源-向他大声呼喊,以获得这个答案的灵感) 在ios8中,我是这样做的: var settingsString = UIKit.UIApplication.OpenSettingsUrlString; var url = new NSUrl (

我正在搜索Xamarin的实现


为目标C回答-在C中,为Xamarin Studio中的iOS做这件事的正确方法是什么?

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

在ios8中,我是这样做的:

var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
            var url = new NSUrl (settingsString);
            UIApplication.SharedApplication.OpenUrl (url);
其中,通过UIAlertView click中的委托类从click事件调用上述代码

由于我也支持ios7,为了处理ios7设备,我这样做了,在决定是否显示视图控制器时调用HandleLocationAuthorization方法-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;
    }

}
为完整起见,以下是上述事件delgate的代码:

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