簡體   English   中英

如何在iOS中以編程方式打開設置

[英]How to open settings programmatically in ios

我正在尋找如何以編程方式打開設置的Xamarin實現

Vito-ziv回答了關於目標C的問題-在Xamarin Studio中的iOS版C#中,這樣做的正確方法是什么?

對於當前設備,這僅在ios8中才可能實現(在撰寫本文時ios9尚不可用)(過去在ios5之前顯然可以實現-請參閱Xamarin的Adrian Stevens的 博客文章 -向他大喊此答案的靈感)

為了在ios8中做到這一點,我做到了:

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

通過UIAlertView click中的委托類從click事件中調用上述代碼的位置。

由於我也支持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);
    }
  }
}

希望這會幫助你。 無法在iPad上運行,這在iPhone中可以正常工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM