The IOS app can directly open the corresponding application by setting the URL_Scheme in the Apple mobile phone and changing the request header to URL_Scheme:// in the browser (if the corresponding app is not installed, a pop-up box with invalid instructions will pop up. But you can jump to the specified application to download, the specific js logic refer to this note of mine https://blog.csdn.net/ssssssilver/article/details/109730514 )
The specific settings of URL_Scheme need to be set in the info of the xcode project after unity has packaged the xcode project, as shown in the figure
After setting the URL_Types, you can see the corresponding node configuration on the info.plist of the project root directory
URL_Identifier should refer to the unique identifier of the application, URL_Schemes should mean that an application can set multiple URL_Sheme, here I set it to test_scheme, then when I enter test_scheme:// in the browser, press OK to open the corresponding package Applications coming out.
The method of opening the application is implemented, and the value needs to be passed in. The goal is to enter the string of information test_scheme://param1=1¶m2=2, and the string of param1=1¶m2=2 can be detected in the application in Unity. Then perform custom operations such as page jumps based on logic.
We first need to define a method reference in unity, this method is provided to xcode to call.
public void IosCallFunction(string parmaters)
{
Debug.Log("url_scheme传过来的字符串为: " + parmaters);
}
At the same time, the class of this method should be mounted on MainCamera, so that the method in the class mounted on the specified object in unity can be called in xcode through SendMessage.
After completing the previous step, package the unity project into an xcode project, and find the UnityAppController.mm class. The specific location is as follows
Find the corresponding openURL class in this class. Xcode handles the logic of url_scheme opening through this class. Before return returns, the value of the url is passed back to unity through the UnitySendmessage method, and the class created in unity can be triggered. code show as below
// UIApplicationOpenURLOptionsKey was added only in ios10 sdk, while we still support ios9 sdk
- (BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary<NSString*, id>*)options
{
id sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey], annotation = options[UIApplicationOpenURLOptionsAnnotationKey];
NSMutableDictionary<NSString*, id>* notifData = [NSMutableDictionary dictionaryWithCapacity: 3];
if (url)
{
notifData[@"url"] = url;
UnitySetAbsoluteURL(url.absoluteString.UTF8String);
}
if (sourceApplication) notifData[@"sourceApplication"] = sourceApplication;
if (annotation) notifData[@"annotation"] = annotation;
AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);
URLString = [url absoluteString];
// 传值给unity
//Main Camera指unity场景中的Main Camera物体
//IosCallFunction指在Main Camera中挂载的类里面的IosCallFunction方法
//URLString指上面这方法需要传的值
UnitySendMessage( "Main Camera", [@"IosCallFunction" UTF8String], [URLString UTF8String]);
return YES;
}
In this case, when you open the ios application through the url, you can pass the url_scheme parameters back to the unity method for processing at the same time.