Open Settings application on Unity iOS
Asked Answered
T

2

4

I am in need of a way to make the user is taken to the Settings application to disable the multitasking gestures. I know that in iOS 8 you can launch the Settings application programmatically through the URL in Objective-C:

NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];

But I do not know how to get this URL in Unity to use with Application.OpenURL()

Thrifty answered 3/5, 2015 at 4:50 Comment(2)
Please, don't use unity tag for questions related to Unity game engine. It is generally a good idea to read the tag's description before using it.Aun
Sorry, I will pay more attention next timeThrifty
A
12

You need to write a tiny iOS plugin for that, here is more information about it: http://docs.unity3d.com/Manual/PluginsForIOS.html

And here is your solution, ask if something should be unclear.

Script/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
    }
}

Plugins/MyNativeBindings.cs

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

        [DllImport ("__Internal")]
        public static extern void OpenSettings();
    #endif
}

Plugins/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];
    }
}
Anarch answered 6/5, 2015 at 8:42 Comment(5)
I will try also print this URL to get it and use with Application.OpenURL() to not have to use a plugin ;)Thrifty
it's possible to print the URL and open it with Application.OpenURL(), but you still need a plugin to get the URL, because Apple might change the URL and you app will break with a hardcoded URL. (i will share that code tomorrow)Anarch
i added a GetSettingsURL() methodAnarch
I will accept your answer, thanks. Hope that Unity team provide a method to get this URL in C#Thrifty
I don't think the Unity Team will add this, they don't like platform specific code.Anarch
T
3

Using idea of JeanLuc, I create a empty XCode project and print the string constant UIApplicationOpenSettingsURLString and used in Unity with Application.OpenURL() to not have to use a plugin. Works very nice.

The value for constant UIApplicationOpenSettingsURLString is: "app-settings:" (without quotas).

Use: Application.OpenURL("app-settings:") to open directly from unity

WARNING: The use of hardcoded strings is dangerous and can break your code if Apple change the value of constant UIApplicationOpenSettingsURLString. Its just a workaround while Unity does not add a constant for reference in C# code.

Thrifty answered 6/5, 2015 at 19:26 Comment(1)
This is the correct answer. No need to make a plugin. It's just 'Application.OpenURL("app-settings:") 'Gerundive

© 2022 - 2024 — McMap. All rights reserved.