- Home /
Launched from a browser
I want to start the app, passing arguments (such as URL) from the browser.
But I do not know how.
Please tell me What is the way.
Answer by whydoidoit · Mar 04, 2014 at 07:31 AM
EDIT: Don't know why I thought you wanted this for IOS - must have been reading something else. Please ignore if you do not.
ON IOS
You have to modify the XCode project to achieve this and add a entry to the info.plist plus handle the code on app launching.
This defines a URL scheme for radicalWords which you use in a link as radicalWords://someparameters
The following is at the bottom of my info.plist just before the last tag
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>radicalWords</string>
</array>
</dict>
</array>
Then you need to register your own app controller to take the initial parameters - this is achieved by putting the definition in Plugins/IOS in your project - I'll list it at the bottom. My version calls a method on a game object called !URLSchemeHandler (which must be available in the first scene) - the method is OnOpenWithUrl and it takes a string parameter of the url passed in which I then parse in C#.
MyAppController.m
#import <UIKit/UIKit.h>
#import "UnityAppController.h"
@interface MyAppController : UnityAppController
{
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
- (void) openURLAfterDelay:(NSURL*) url;
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url;
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
@end
@implementation MyAppController
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[super application:application didFinishLaunchingWithOptions:launchOptions];
if ([launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]) {
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
[self performSelector:@selector(openURLAfterDelay:) withObject:url afterDelay:2];
}
return YES;
}
- (void) openURLAfterDelay:(NSURL*) url
{
UnitySendMessage("!URLSchemeHandler", "OnOpenWithUrl", [[url absoluteString] UTF8String]);
}
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
UnitySendMessage("!URLSchemeHandler", "OnOpenWithUrl", [[url absoluteString] UTF8String]);
return YES;
}
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
UnitySendMessage("!URLSchemeHandler", "OnOpenWithUrl", [[url absoluteString] UTF8String]);
return YES;
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(MyAppController)
I don't need this for now but sure to vote up if that's working, so there's no way in Android yet right?
I'm using this for Android: https://www.assetstore.unity3d.com/en/#!/content/30430
Thanks so much! The only detailed example I could find. Works perfectly!
Thanks for your answer, saved a number of people a lot of headaches. I've been using it for a while. Just recently I made a change which increased my frequency of cold starts compared to background resumes and I noticed that, for my code at least, openURLAfterDelay was failing to message Unity.
I think it's because, despite the delay, the Unity object doesn't exist or isn't being found. Anyway, this mod worked for me:
NSURL *launchURL = NULL;
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[super application:application didFinishLaunchingWithOptions:launchOptions];
if ([launchOptions objectFor$$anonymous$$ey:UIApplicationLaunchOptionsURL$$anonymous$$ey]) {
NSURL *url = [launchOptions objectFor$$anonymous$$ey:UIApplicationLaunchOptionsURL$$anonymous$$ey];
launchURL = url;
}
return YES;
}
extern "C" void _startURL()
{
if (launchURL!=NULL)
UnitySend$$anonymous$$essage("!URLSchemeHandler", "OnOpenWithUrl", [[launchURL absoluteString] UTF8String]);
}
And I call _startURL from the Start method in my URLSchemeHandler script.
Your answer
Follow this Question
Related Questions
How to create a complicated game over scenario ? HELP 0 Answers
A node in a childnode? 1 Answer
Help on Jumping 1 Answer
is the unity player browser specific? 1 Answer
Can't find MonoDevelop. Unity 4.5.2 1 Answer