Open Settings app in Unity iOS

I need a way to get the user to go to the Settings app to disable multitasking gestures. I know that in iOS 8 you can launch the Settings app programmatically via the URL in Objective-C:

NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString]; 

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

+2
ios settings unity3d launch
source share
2 answers

To do this, you need to write a tiny iOS plugin, here is more detailed information about it: http://docs.unity3d.com/Manual/PluginsForIOS.html

And here is your decision, 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]; } } 
+4
source share

Using the idea of ​​JeanLuc, I create an empty Xcode project and print the string constant UIApplicationOpenSettingsURLString and use Unity with Application.OpenURL () to not use the plugin. It works very well.

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

Usage: Application.OpenURL("app-settings:") to open directly from a unit

A WARNING. Using hard-coded strings is dangerous and can break your code if Apple changes the value of the UIApplicationOpenSettingsURLString constant. Its just a workaround, while Unity does not add a constant for the link in C # code.

0
source share

All Articles