Search Unity

Android/iOS: Launch from within a Unity app another Unity app

Discussion in 'Android' started by LouiseW, Jan 15, 2014.

  1. LouiseW

    LouiseW

    Joined:
    Jul 16, 2013
    Posts:
    8
    Hi!

    (I posted this in Android Development but it applies to both Android and iPhone so if there's a better section for this post I apologize.)

    My question:

    Is it possible to launch another Unity application from within a Unity application? Basically I want to be able to press a "Start other app"-button inside Unity app #1 that launches this other Unity app (#2), or if it's not yet downloaded takes me to AppStore/PlayStore.

    Is something like Application.OpenURL() interesting here? Or what would be the best approach? I'm using Unity 4.3.2 btw.

    Thanks!
     
  2. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i've found Application.OpenURL() to work but you will be better putting it in an if statement with and else

    for example only

    Code (csharp):
    1.  
    2.  
    3. if(!app)
    4. {
    5.      if (Input.GetButton("Start"))
    6.       {
    7.       Application.OpenURL("Your URL");
    8.        }
    9.   else
    10.       {
    11.       Application.OpenURL("YourOtherURL");
    12.        }
    13. }
    14.  
    make sure you have a function that can detect you phone and find your that way it will work properly mine works by checking if the URL address is there first so make sure your URL is right you my need to check your phone folder structure first before approaching this of just have

    Code (csharp):
    1.  
    2.   if (Input.GetButton("Start"))
    3.       {
    4.       Application.OpenURL("Your URL");
    5.        }
    6.  
    and keep it simple but if its not there it will not open
     
  3. LouiseW

    LouiseW

    Joined:
    Jul 16, 2013
    Posts:
    8
    Thanks for the answer!

    I think I need to specify a bit more: Can I somehow check if an app is installed? And if that app is installed can I make the phone launch it? Or if it's not installed redirect the user/phone to AppStore/PlayStore?

    I would be very grateful if I could get example code on these specific lines, if it's even possible. Maybe I can do something with "com.company.appname"?
     
  4. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    You can use something like this on Android:

    Code (csharp):
    1.  
    2. public void launchApp(String packageName)
    3. {
    4.     Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
    5.     if (intent != null)
    6.     {
    7.         // Activity was found, launch new app
    8.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    9.         startActivity(intent);
    10.     }
    11.     else
    12.     {
    13.         // Activity not found. Send user to market
    14.         intent = new Intent(Intent.ACTION_VIEW);
    15.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    16.         intent.setData(Uri.parse("market://details?id="+packageName));
    17.         startActivity(intent);
    18.     }
    19. }
    20.  
    Not tested, but should work.
     
    Aryansh_Gupta and jerry2157 like this.
  5. LouiseW

    LouiseW

    Joined:
    Jul 16, 2013
    Posts:
    8
    Thanks for your reply! I'm not sure this is what I'm looking for though, since I need it to work in Unity (C# scripts) and preferably something that will work with both iOS and Android, i.e. a Unity method. But maybe I'm misunderstanding something?
     
  6. mtusan

    mtusan

    Joined:
    May 24, 2013
    Posts:
    4
    Hi LouiseW, have you found any answer to your original question?

    I as well need to make my app detect if another app is installed on iOS device. And then launch it from my unity app.
     
  7. LouiseW

    LouiseW

    Joined:
    Jul 16, 2013
    Posts:
    8
    Hi!

    Sorry, I haven't checked in here for over a week so I haven't seen your question.

    I have not found the answer I was looking for and since I have some deadline "issues", I decided to use "Application.OpenURL ("link-to-my-other-app-in-GooglePlay/AppStore");" instead. So when the user clicks the button that leads them to the store, they can choose to install the app (or open if already installed). This is a last solution I guess, but it works for me this time.

    If you somehow find the answer to my original question, I would be very grateful if you would write the answer here. :)
     
    aesparza likes this.
  8. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221
  9. mtusan

    mtusan

    Joined:
    May 24, 2013
    Posts:
    4
    The right question here is: Can we detect if apps are installed on device using custom URL schemes? We can't do this from Unity API therefore we need a plugin.

    Davesh, can your plugin do this or it can only launch those apps listed in description?
     
  10. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221

    Sorry, mtusan, My plugin will only launch another application.
     
  11. Hippiecode

    Hippiecode

    Joined:
    Feb 13, 2012
    Posts:
    110
    how to open installed application in unity.
     
  12. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221
    Ganesh, have you read the my plugin's description?

    This plugin is for Android and iOS so it will work on Android and iOS device only, it will not work in Unity Editor.
    After integrating this plugin you can launch dial pad, message, gmail, skype, facebook etc from your unity app.

    AppLauncher is very cheap and easy to integrate.
     
    Last edited: Jul 28, 2018
  13. kingandroid

    kingandroid

    Joined:
    Nov 13, 2014
    Posts:
    9
    Hi, it is a year late but you can do it by

    Code (CSharp):
    1.  
    2.             bool fail = false;
    3.             string bundleId = com.google.appname; // your target bundle id
    4.             AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    5.             AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
    6.             AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
    7.      
    8.             AndroidJavaObject launchIntent = null;
    9.             try
    10.             {
    11.                 launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage",bundleId);
    12.             }
    13.             catch (System.Exception e)
    14.             {
    15.                 fail = true;
    16.             }
    17.  
    18.             if (fail)
    19.             { //open app in store
    20.                 Application.OpenURL("https://google.com");
    21.             }
    22.             else //open the app
    23.                 ca.Call("startActivity",launchIntent);
    24.  
    25.             up.Dispose();
    26.             ca.Dispose();
    27.             packageManager.Dispose();
    28.             launchIntent.Dispose();
     
    Last edited: Mar 3, 2016
  14. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    kingandroid - Thanks for the share. Works like a charm.
     
  15. memetunity

    memetunity

    Joined:
    Dec 11, 2015
    Posts:
    1
    thanks so much its working perfect :)

     
  16. Mr-Buck

    Mr-Buck

    Joined:
    Nov 3, 2015
    Posts:
    21

    so I need to declare the "fail" variable? or is there something I miss?

    also, I just need to set my target app data at this string right?

    "com.unity3d.player.UnityPlayer"

    so that's say I have an app named "ABC"

    I just replace it like

    "com.unity3d.player.ABC"??
     
  17. kingandroid

    kingandroid

    Joined:
    Nov 13, 2014
    Posts:
    9
    Yes you need to declare
    Code (CSharp):
    1. bool fail = false;
    first.
    And just leave the unity name as it is, don't change it to your app name
     
  18. Mr-Buck

    Mr-Buck

    Joined:
    Nov 3, 2015
    Posts:
    21
    sorry I copy the code from below.
    So I miss a spot on the first line XD.
    I apologize for that.....

    Ok....I'm still kind of stuck in it.
    where should I set my target app name instead?
    and also do I need a .jar file to call my apps with your code?
     
  19. kingandroid

    kingandroid

    Joined:
    Nov 13, 2014
    Posts:
    9
    I edited my answer, so yeah there was not any bool before, no need to apologize for that xD

    Hmm if you see my answer now, I put comment showing where to put your target bundle id not the name ("com.companyname.applicationame" in build setting), it is the
    string bundleId = com.google.appname; // replace it to your target bundle ID

    you dont need any .jar, it is default built-in within unity
     
  20. Mr-Buck

    Mr-Buck

    Joined:
    Nov 3, 2015
    Posts:
    21
    Yes! It works!!!! My app has come to life!!!

    Thank you!!!! kingandroid (like a billion thanks)

    I don't know if it is my fantasy, or do you just happen to have an account called "king ios"?
     
  21. xndhai

    xndhai

    Joined:
    Dec 1, 2012
    Posts:
    4
    How about ios, need some script which can work with ios,

    please help
     
    iwunu likes this.
  22. rameshkumar

    rameshkumar

    Joined:
    Aug 5, 2013
    Posts:
    50
    @kingandroid , i just copy pasted your source code and i just want to open a truecaller which is already installed in my mobile so i just changed string bundleId = "com.google.Truecaller"; but when i click the ui i am getting google page not opening truecaller ,should i change anything in your source , please guide me.
     
  23. rameshkumar

    rameshkumar

    Joined:
    Aug 5, 2013
    Posts:
    50
    @kingandroid , i got the solution thanks for your post yaar.
     
  24. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    Nice - you can also send a message to launch app with something like:
    launchIntent.Call<AndroidJavaObject>("putExtra",app+".message",message);

    But I'm not sure of the code the other end to receive that message, in java it's like this:
    Intent intent = getIntent();
    String message = intent.getStringExtra(app+".message");

    Edit: Well solution is
    msg=intent.Call<string>("getStringExtra",app+".message");

    but you really need to handle 'onNewIntent' to get message if app already running which requires native code
     
    Last edited: May 25, 2016
    aesparza likes this.
  25. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Hey all, in addition to @kingandroid approach which was legittttt:cool: for android, I wanted to share an ios approach.

    After desperately:eek: looking for an ios example, and knowing squat about ObjC, I finally put an ios example together for anyone to use (unity talking to native ObjC).
    Also since ios9's intro, apple has now added a new requirement on checking app installations.

    Anyway, I have an objective-c plugin sample (.mm file), a c# script example (.cs file), and apple's new info.plist guide for anyone to use to accomplish app checks in ios:
    http://answers.unity3d.com/questions/1217507/get-objective-c-functionvalue-in-c-wrapper-for-uni.html

    If you plan on wanting to open a certain app via ObjC, look at the "- openURL:" section in the apple guidance and feel free to modify the scripts as you see fit.

    I know there are roundabout ways within unity to do ios app checks, they're just to clunky for me and I don't think they're necessarily the correct approach vs. a native one.

    Anyway, enjoy.

    Cheers,
    OJ
     
    Deleted User and alxcancado like this.
  26. fesul

    fesul

    Joined:
    Nov 20, 2012
    Posts:
    3
    it works, thanks man
     
  27. Martinez-Vargas

    Martinez-Vargas

    Joined:
    Jun 24, 2016
    Posts:
    39
    muchas gracias, funciona de maravilla.
     
  28. cronem

    cronem

    Joined:
    Feb 21, 2017
    Posts:
    1
    Yeah, it's working, thanks!
     
  29. WhiteLotus

    WhiteLotus

    Joined:
    Mar 4, 2015
    Posts:
    9
    Thanks @kingandroid its works , but when we open other app and then return to our application our application restarts. any idea to open our application from the last state?
     
  30. nupur7

    nupur7

    Joined:
    Feb 15, 2017
    Posts:
    9
    hello @kingandroid i tried to open skype app but it is not working. could you please share the complete code snippet. i might be missing something
     
  31. Martinez-Vargas

    Martinez-Vargas

    Joined:
    Jun 24, 2016
    Posts:
    39
    You have to insert the snippet of code into a function and normally call it Function ().
     
  32. shweta-unity

    shweta-unity

    Joined:
    Nov 23, 2016
    Posts:
    7
    This works fine thanks.
    Is there any similar option for iOS?
     
  33. LemonTreeApps

    LemonTreeApps

    Joined:
    Jul 30, 2017
    Posts:
    1
    Thanks. It works fine for the apps that have been already installed. However, when i try for the app that isn't in my phone, fail stays false and trys "ca.Call("startActivity",launchIntent)" action then it skips Dispose parts. It think understanding whether app is in phone or not requires extra effort? Could you please help for this situation?
     
  34. Gopisgs

    Gopisgs

    Joined:
    May 21, 2016
    Posts:
    2
    Hi all,
    That code works fine but i have a doubt on the same topic , Is it Possible to send and Receive the data from one app to another app, if its possible please let me know how to use .

    Thanks in Advance
     
    aesparza likes this.
  35. Eddyjones

    Eddyjones

    Joined:
    Dec 21, 2017
    Posts:
    1
    I get a exception
    Init'd AndroidJavaClass with null ptr!
     
  36. Tg_Studios

    Tg_Studios

    Joined:
    Dec 3, 2015
    Posts:
    2
    @Eddyjones You have to build it to an android tablet/phone for it to work instead of accessing it strait from the editor. Had the same issues when I got it.
     
    nomeepk likes this.
  37. nomeepk

    nomeepk

    Joined:
    Aug 8, 2017
    Posts:
    7
    It will give an error in Unity Editor.
    You can get a debug log from Unity editor and build on Android and run it like this:

    #if UNITY_EDITOR
    Debug.Log("Game Check");
    #elif UNITY_ANDROID

    // All Check Code
    bool fail=false;
    // after the code

    #endif
     
  38. ismaelnascimentoash

    ismaelnascimentoash

    Joined:
    Apr 2, 2017
    Posts:
    30
    Thank you ! It's work

     
    alxcancado and nomeepk like this.
  39. Captainhumza

    Captainhumza

    Joined:
    Apr 11, 2018
    Posts:
    1
    Hi @kingandroid
    can you please tell me that can we pass data from one app to another app through your code?
     
    aesparza likes this.
  40. Chintan-Kansagara

    Chintan-Kansagara

    Joined:
    Jul 16, 2016
    Posts:
    19
    unity 3d how to check app installed unity ios code
     
  41. michelattin

    michelattin

    Joined:
    May 28, 2018
    Posts:
    4
    hi for an application when user click on send button i want to open gmail in android with a To address(hardcoded one).
    Like a unity ui displaying a persons detail,when the user clicks send button.open gmail app with to address of person.the user wants to type the body and click send mail from gmail app.Is this possibe?
    this code will send the gmail.but without opening gmail app.the body of letter is also scripted.Does anyone know a workaround


    using UnityEngine;
    using System.Collections;
    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;

    public class message : MonoBehaviour
    {

    public void SendMail()
    {
    MailMessage mail = new MailMessage();

    mail.From = new MailAddress("123321@gmail.com");
    mail.To.Add("321123@gmail.com");
    mail.Subject = "Test Mail";
    mail.Body = "This is for testing SMTP mail from GMAIL";

    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
    smtpServer.Port = 587;
    smtpServer.Credentials = new System.Net.NetworkCredential("123321@gmail.com", "gmailpassword") as ICredentialsByHost;
    smtpServer.EnableSsl = true;
    ServicePointManager.ServerCertificateValidationCallback =
    delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    { return true; };
    smtpServer.Send(mail);
    Debug.Log("success");

    }
    }
     
    mrmeizongo likes this.
  42. italomarques

    italomarques

    Joined:
    Jan 20, 2017
    Posts:
    1
    kingandroid to fix a bug in the new versions of android: replace line 18 for this if (fail || launchIntent == null) // open app in store
     
  43. pablovydra

    pablovydra

    Joined:
    Dec 7, 2017
    Posts:
    1
    This still works? i was trying with "com.google.ar.core" but it allways fail.
     
  44. shahin2002

    shahin2002

    Joined:
    Dec 29, 2018
    Posts:
    1
    How I can open the"play store"(google) in unity by this code? I tested but I could not open play store app.help me plaese
    means open link my games in " google play store " Application
    help me please
    I could not use above code so write a code for example link :com.ds.aa
     
    Last edited: Feb 23, 2019
  45. aesparza

    aesparza

    Joined:
    Apr 4, 2016
    Posts:
    29
    You can do it with:

    Application.OpenURL("market://details?id=YOUR.APP.BUNDLE.HERE");
     
  46. taib

    taib

    Joined:
    Dec 27, 2012
    Posts:
    1
    hye sory im new and i cant get it.. did this one string bundleId = com.google.appname; need to chang to this string bundleId = "com.google.appname"; ??? and this one AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); to AndroidJavaClass up = new AndroidJavaClass("com.google.appname); sorry for my stupid question..
     
  47. MohammadAlizadeh

    MohammadAlizadeh

    Joined:
    Apr 16, 2015
    Posts:
    26
    Thanks it works, i'm trying to make app launcher
     
  48. Vandallord

    Vandallord

    Joined:
    Jan 3, 2017
    Posts:
    2
    I am doing this for android. Popular apps open, but one obscure one doesn't. What could be wrong? Perhaps some permission is needed?
     
  49. Bhavani21

    Bhavani21

    Joined:
    Mar 28, 2022
    Posts:
    1
    Im getting this error

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference
     
  50. vasanrouth

    vasanrouth

    Joined:
    May 21, 2022
    Posts:
    3
    Have you solved this problem? It worked for me 3 weeks back, I just added a few more modules to my game which have nothing to do with this code. Though I am getting this error now :(