Forum Xamarin.Forms

Detect and Open another App on device

KenNickersonKenNickerson USMember ✭✭✭

Can someone tell me the best approach to accomplishing this for IOS and Android. Code samples or links to related discussions would be most appreciated.

thanks

Tagged:

Answers

  • AndrewMcCormackAndrewMcCormack USUniversity ✭✭✭

    Need to find the package name of the application on your device. Then open the app using the package name and any Intents you need to pass to it. If you device is not rooted find the packages that are installed or use a simulator to find the package names or use the code below to list all package names on the device.

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
    

    Open app

    var pm = Android.App.Application.Context.PackageManager;
    pm.GetPackageInfo(package, 0);
    uri = Android.Net.Uri.Parse(packageurl);
    var intent = new Intent (Intent.ActionView, uri);
    // any IntentFlags or Data
    this.StartActivity (intent);
    
    public static void StartActivity(this object o, Intent intent)
    {
        var context = o as Context;
        if (context != null)
        {
            context.StartActivity (intent);
        } 
        else
        {
            intent.SetFlags (ActivityFlags.NewTask);
            Application.Context.StartActivity (intent);
        }
    }
    
  • KenNickersonKenNickerson USMember ✭✭✭

    @AndrewMcCormack Thank you for the response. Overall, I am not following the code you pasted, other than it apears to be specific to android. I am using Xamarin forms and should have stated that in my posted. With that I need a solution at the PCL level and specific platforms, IOS and Android.

  • CodingNewUserCodingNewUser INMember ✭✭

    HI @KenNickerson did you find a solution to this.
    I am using xamarin forms as well and need to detect if an app is present on my device else need to go to respective stores and install the app.
    I am looking for something at PCL level too for android and IOS.

  • KenNickersonKenNickerson USMember ✭✭✭
    INT THE VIEW OR VIEW MODEL:   
    
                   var  appname = @"otherappId";
                   var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
    

    IN THE HANDLER:

            public interface IAppHandler
            {
                Task<bool> LaunchApp(string uri);
            }
    

    IN THE IOS PROJECT:

                public class OpenAppiOS : IAppHandler
                {
                    public Task<bool> LaunchApp(string uri)
                    {
                        try
                        {
                            var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));
    
                            if (!canOpen)
                                return Task.FromResult(false);
    
                            return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));
    
                        }
                        catch (Exception ex)
                        {
                            return Task.FromResult(false);
                        }
                    }
    

    IN THE ANDROID PROJECT:

                   [Activity(Label = "OpenAppAndroid")]
                    public class OpenAppAndroid : Activity, IAppHandler
                    {
                        public Task<bool> LaunchApp(string uri)
                        {
                            bool result = false;
    
                            try
                            {
                                var aUri = Android.Net.Uri.Parse(uri.ToString());
                                var intent = new Intent(Intent.ActionView, aUri);
                                Xamarin.Forms.Forms.Context.StartActivity(intent);
                                result = true;
                            }
                            catch (ActivityNotFoundException)
                            {
                                result = false;
                            }
    
                            return Task.FromResult(result);
                        }
                    }
    
  • CodingNewUserCodingNewUser INMember ✭✭
    edited January 2018
    Hi @KenNickerson
    For ios what is the uri..any way to get the uri installed on an ios device
  • KenNickersonKenNickerson USMember ✭✭✭

    @CodingNewUser Not sure what your question is. Are you asking how to open an app from the browser or from another app. I posted the code if from another app

    uri would be something like someapp:// or itunes:// for example

  • CodingNewUserCodingNewUser INMember ✭✭
    edited January 2018
    Yes I'm asking how to open an app from another app..From where can we get the uri for the applications.
    In android we have several apps available on playstore which give the information of the package name.Do we have something similar for IOS as well?
    @KenNickerson
  • KenNickersonKenNickerson USMember ✭✭✭

    How is described above. What app do you want to open. DO you know who authored it? Is it an Apple owned app?

  • KenNickersonKenNickerson USMember ✭✭✭

    In any case, n most cases it should be the name of the app followed by //:

  • CodingNewUserCodingNewUser INMember ✭✭
    edited January 2018

    For Example- there is an app yammer on app store,then will this work?
    var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(yammer://));

    For some apps i know who authored it but some of them would be general applications on Appstore.

  • KenNickersonKenNickerson USMember ✭✭✭

    Probably but not guaranteed. Give it a shot. GL

  • SudharsanCSudharsanC INMember ✭✭

    For iOS, Destination application not launched. I was followed the same that what your are mentioned above.

  • KenNickersonKenNickerson USMember ✭✭✭

    @SudharsanC Do you have the correct iOS app name, as the device would recognize it. This may not be the same as the viewable appname. What app are you trying to detect?

  • KenNickersonKenNickerson USMember ✭✭✭
    edited April 2018

    The app name in your PCL project needs to look like the following:

         var  appname = @"otherappId://";
    

    The appname may also include parameters where the app supports multiple organizations

    Also, this might be the issue.
    You may also need the following in info.plist:
    -- &lt key &gt LSApplicationQueriesSchemes
    -- &lt array &gt
    -- &lt string &gt appname &lt /string &gt
    -- &lt /array

  • ManojkumarMaliManojkumarMali USMember ✭✭✭
    edited November 2018

    @KenNickerson said:
    INT THE VIEW OR VIEW MODEL:

    var appname = @otherappId;
    var result = await DependencyService.Get().LaunchApp(appname);

    IN THE HANDLER:

          public interface IAppHandler
          {
              Task<bool> LaunchApp(string uri);
          }
    

    IN THE IOS PROJECT:

              public class OpenAppiOS : IAppHandler
              {
                  public Task<bool> LaunchApp(string uri)
                  {
                      try
                      {
                          var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));
          
                          if (!canOpen)
                              return Task.FromResult(false);
          
                          return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));
          
                      }
                      catch (Exception ex)
                      {
                          return Task.FromResult(false);
                      }
                  }
    

    IN THE ANDROID PROJECT:

                 [Activity(Label = "OpenAppAndroid")]
                  public class OpenAppAndroid : Activity, IAppHandler
                  {
                      public Task<bool> LaunchApp(string uri)
                      {
                          bool result = false;
              
                          try
                          {
                              var aUri = Android.Net.Uri.Parse(uri.ToString());
                              var intent = new Intent(Intent.ActionView, aUri);
                              Xamarin.Forms.Forms.Context.StartActivity(intent);
                              result = true;
                          }
                          catch (ActivityNotFoundException)
                          {
                              result = false;
                          }
              
                          return Task.FromResult(result);
                      }
                  }
    

    Thanks @KenNickerson for your post.

    This one is also helpful for iOS.

  • XnainaXnaina USMember ✭✭✭

    Hi all, anybody knows how to launch default Contacts app from our app in iOS.
    Thanks in advance.

  • KenNickersonKenNickerson USMember ✭✭✭

    @Xnaina Try contacts://

    Just a guess

Sign In or Register to comment.