Search Unity

How to call an Android / iOS Unity app from a website?

Discussion in 'Scripting' started by yannID3C, Jun 19, 2012.

  1. yannID3C

    yannID3C

    Joined:
    Aug 8, 2011
    Posts:
    6
    Hi,
    I have a website. I want it to be able to call an application on my mobile (on iOS and Android), whith parameters.

    For example, by clicking a link on Facebook you can open Spotify with the concerned music.

    I hope someone could help me on this please.
    Thanks
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,406
  3. yannID3C

    yannID3C

    Joined:
    Aug 8, 2011
    Posts:
    6
    No, I don't want to open a URL from Unity, but from the WebBrowser of my phone, I want to launch an application
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    I'm not 100% sure, but I don't think you can unless your phone/its browser has specific support for the application in question built in. I don' t think you can query what's installed on a device, let alone mess around with it by sending it commands.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Thats done through the custom url scheme supported by iOS and I guess Android too
    Check the corresponding documentation at Apple / Google to see how to make use of it
     
  6. havchr

    havchr

    Joined:
    Jun 18, 2009
    Posts:
    75
    I'm trying to do this.
    I have a custom UnityPlayerActivity and a manifest with :
    <intent-filter>
    <data
    android:host="somehost"
    android:scheme="test"/>

    <action android:name="android.intent.action.VIEW"/>

    <category android:name="android.intent.category.BROWSABLE"/>
    <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

    Currently not working, but I'm trying to figure out why :)

    edit: got it working by using something else than http.
    mind you have to open this link in a browser on your phone, so create a simple html file like this:

    <html>
    <a href="test://somehost/?test=333"> click the url</a>
    </html>
     
    Last edited: Nov 7, 2012
  7. oferei

    oferei

    Joined:
    Dec 1, 2012
    Posts:
    36
    Last edited: Jun 5, 2013
  8. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    This was quite helpful but the problem I am having is that every time I tap on my custom link to test, it says my app has crashed. Is this because I skipped adding a plugin to handle this activity? I really don't need to do anything but launch the app so I was hoping to avoid that part.
     
  9. Sasori

    Sasori

    Joined:
    Jan 16, 2014
    Posts:
    1
    Hi, I have the same problem, and I can not fix it, I modified the manifest to me with a url open the game, but I was showing completely black screen.

    I got this:
    <activity android:label="@string/app_name"
    android:name="com.unity3d.player.UnityPlayerProxyActivity"
    android:screenOrientation="portrait"
    android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|fontScale">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="my_app" android:host="my_action"/>
    </intent-filter>
    </activity>

    to call "my_app://my_action/?param=value"
    I only shows black screen
     
  10. kjQtt

    kjQtt

    Joined:
    Jan 15, 2014
    Posts:
    1

    In iOS you can just do a link wiith the app name as far as i know, eg snapchat:// instead of http:// or spotify://
     
    Last edited: Jan 16, 2014
  11. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    Update: Since I am using Prime31 plugins which change your main activity, I realized I just had to put my custom scheme on the Prime31 main activity then it worked fine without needing anything else. I don't need access to any passed variables though, just a way to launch the app, so this may not work for other people.
     
    monark likes this.
  12. gull

    gull

    Joined:
    Nov 18, 2014
    Posts:
    4
    please tell me how to open my app from URL? you have shared java code is it work in unity??? what configurations will be required in unity to work?
     
  13. SuperbStarling42

    SuperbStarling42

    Joined:
    Jan 15, 2014
    Posts:
    5
    Thank you, adding the intent-filter to the android manifest does now open the app from a url
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="my_app" android:host="my_action"/>
    </intent-filter>

    However, how do I now access the parameters being passed in by the url?
    my_app://my_action?var=val
     
  14. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    Create plugin with that code

    public static String getLauncherURL()
    {
    Intent intent = UnityPlayer.currentActivity.getIntent();
    Uri data = intent.getData();
    if(data == null)
    return "no input data";
    return data.toString();
    }

    And .cs code
    public static string getLauncherURI()
    {
    if (Application.platform == RuntimePlatform.Android)
    {
    using (AndroidJavaClass jc = new AndroidJavaClass("com.yourpackage.MainActivity"))
    {
    return jc.CallStatic<string>("getLauncherURL");
    }
    }
    return "";
    }
    }
     
  15. MnzSoftware

    MnzSoftware

    Joined:
    Jun 14, 2016
    Posts:
    6
    Any example? for creation plugins?

    Create plugin with that code

    public static String getLauncherURL()
    {
    Intent intent = UnityPlayer.currentActivity.getIntent();
    Uri data = intent.getData();
    if(data == null)
    return "no input data";
    return data.toString();
    }
     
  16. meta-meta

    meta-meta

    Joined:
    May 10, 2013
    Posts:
    6
    Here's a Java example. Anyone know how to do it for iOS?

    This should be in a Android Library project. Build it as a jar and place in /Assets/Plugins/Android

    Code (csharp):
    1. import android.content.Intent;
    2. import android.os.Bundle;
    3. import com.google.unity.GoogleUnityActivity;
    4. import com.unity3d.player.UnityPlayer;
    5.  
    6. /* When launching from a URL, the URL is passed to the Android intent launched. By the time we are in Unity
    7. * it is too late to get at this since Unity runs in a separate Activity which can't get at the originally launched
    8. * intent. We override GoogleUnityActivity here so we can get at the URL and pass it to unity as a message. */
    9. public class MyActivity extends GoogleUnityActivity {
    10.     @Override
    11.     protected void onCreate(Bundle savedInstanceState) {
    12.         super.onCreate(savedInstanceState);
    13.  
    14.         int flags = getIntent().getFlags();
    15.         if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
    16.             // The activity was launched from history
    17.             // remove extras here
    18.         } else {
    19.             // this calls the LaunchFromUrl method in the Start GameObject's script in our Unity project
    20.             String url = getIntent().getDataString();
    21.             if(null != url) {
    22.                 UnityPlayer.UnitySendMessage("Start", "LaunchFromUrl", url);
    23.             }
    24.         }
    25.     }
    26.  
    27.     @Override
    28.     protected void onNewIntent(Intent intent) {
    29.         super.onNewIntent(intent);
    30.  
    31.         // this calls the LaunchFromUrl method in the Start GameObject's script in our Unity project
    32.         UnityPlayer.UnitySendMessage("Start", "LaunchFromUrl", intent.getDataString());
    33.     }
    34. }
    The plugin's AndroidManifest:
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.           package="com.myDomain.MyAndroidPlugin"
    5.           android:versionCode="1"
    6.           android:versionName="1.0">
    7.     <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
    8.         <activity android:name=".MyActivity"
    9.                   android:label="@string/app_name">
    10.             <intent-filter>
    11.                 <action android:name="android.intent.action.MAIN"/>
    12.                 <category android:name="android.intent.category.LAUNCHER"/>
    13.             </intent-filter>
    14.         </activity>
    15.     </application>
    16. </manifest>
    17.  
    Your Unity App's AndroidManifest under Assets/Plugins/Android (note the reference to the plugin activity):
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <!--
    4.      Copyright 2014 Google Inc. All rights reserved.
    5.  
    6.      Licensed under the Apache License, Version 2.0 (the "License");
    7.      you may not use this file except in compliance with the License.
    8.      You may obtain a copy of the License at
    9.  
    10.           http://www.apache.org/licenses/LICENSE-2.0
    11.  
    12.      Unless required by applicable law or agreed to in writing, software
    13.      distributed under the License is distributed on an "AS IS" BASIS,
    14.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15.      See the License for the specific language governing permissions and
    16.      limitations under the License.
    17. -->
    18. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    19.     <application android:icon="@drawable/app_icon"
    20.                  android:label="@string/app_name">
    21.         <activity android:name="com.myDomain.MyAndroidPlugin.MyActivity"
    22.                   android:label="@string/app_name"
    23.                   android:screenOrientation="landscape"
    24.                   android:launchMode="singleTask"
    25.                   android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
    26.             <intent-filter>
    27.                 <action android:name="android.intent.action.MAIN" />
    28.                 <category android:name="android.intent.category.LAUNCHER" />
    29.                 <category android:name="com.google.intent.category.CARDBOARD" />
    30.             </intent-filter>
    31.             <intent-filter>
    32.                 <action android:name="android.intent.action.VIEW" />
    33.                 <category android:name="android.intent.category.DEFAULT" />
    34.                 <category android:name="android.intent.category.BROWSABLE" />
    35.                 <data android:scheme="https" android:host="myhost.com" android:pathPattern="/some/path/.*" />
    36.                 <data android:scheme="http" android:host="myhost.com" android:pathPattern="/some/path/.*" />
    37.             </intent-filter>
    38.           <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    39.         </activity>
    40.         <meta-data android:name="IMMERSIVE_MODE" android:value="true" />
    41.     </application>
    42.     <!-- Set target sdk version to Lollipop to prevent issues with Marshmallow's runtime permissions. -->
    43.    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
    44.    <uses-feature android:glEsVersion="0x00020000" />
    45.    <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true"/>
    46.    <uses-feature android:name="android.hardware.sensor.gyroscope" android:required="true"/>
    47.    <uses-permission android:name="android.permission.NFC"/>
    48.    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    49.    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    50.    <uses-permission android:name="android.permission.INTERNET" />
    51. </manifest>
    52.  
     
    Last edited: Nov 2, 2016
  17. BenoitFreslon

    BenoitFreslon

    Joined:
    Jan 16, 2013
    Posts:
    163
    on iOS you just have to add the scheme in the Info.plist

    CFBundleURLTypes
    Item 0
    CFBundleURLSchemes
    Item 0 > your_scheme