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

时间:2015-05-25 13:44:29

标签: c# ios xamarin

我正在搜索How to open settings programmatically

的Xamarin实现

Vito-ziv回答了目标C - 在Xamarin Studio中使用C#for iOS执行此操作的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

对于目前的设备,这只能在ios8中使用(ios9在编写时不可用)(过去可能在ios5之前显然 - 参见this blog postAdrian Stevens at Xamarin - 向他大喊大叫这个答案的灵感)

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

}

为了完整性,这里是上面引用的事件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);
    }
  }
}

答案 1 :(得分:1)

希望这会对你有所帮助。这在iPhone上工作并不确定在iPad上工作。

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