在Unity iOS上打开设置应用程序

Gil*_*ian 2 settings launch unity-game-engine ios

我需要一种使用户进入“设置”应用程序以禁用多任务手势的方法。我知道在iOS 8中,您可以通过Objective-C中的URL以编程方式启动“设置”应用程序:

NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何在Unity中获取此URL以与Application.OpenURL()一起使用

Jea*_*Luc 6

您需要为此编写一个微型iOS插件,此处是有关它的更多信息:http : //docs.unity3d.com/Manual/PluginsForIOS.html

这是您的解决方案,请问是否不清楚。

脚本/Example.cs

using UnityEngine;

public class Example 
{
    public void OpenSettings()
    {
        #if UNITY_IPHONE
            string url = MyNativeBindings.GetSettingsURL();
            Debug.Log("the settings url is:" + url);
            Application.OpenURL(url);
        #endif
    }
}
Run Code Online (Sandbox Code Playgroud)

插件/MyNativeBindings.cs

public class MyNativeBindings 
{
    #if UNITY_IPHONE
        [DllImport ("__Internal")]
        public static extern string GetSettingsURL();

        [DllImport ("__Internal")]
        public static extern void OpenSettings();
    #endif
}
Run Code Online (Sandbox Code Playgroud)

插件/iOS/MyNativeBindings.mm

extern "C" {
    // Helper method to create C string copy
    char* MakeStringCopy (NSString* nsstring)
    {
        if (nsstring == NULL) {
            return NULL;
        }
        // convert from NSString to char with utf8 encoding
        const char* string = [nsstring cStringUsingEncoding:NSUTF8StringEncoding];
        if (string == NULL) {
            return NULL;
        }

        // create char copy with malloc and strcpy
        char* res = (char*)malloc(strlen(string) + 1);
        strcpy(res, string);
        return res;
    }

    const char* GetSettingsURL () {
         NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
         return MakeStringCopy(url.absoluteString);
    }

    void OpenSettings () {
        NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL: url];
    }
}
Run Code Online (Sandbox Code Playgroud)