locked
How to open other apps from xamarin forms RRS feed

  • Question

  • User145768 posted

    How to open other apps from xamarin forms ? or How to open other apps from xamarin forms apps

    Wednesday, August 12, 2015 2:38 PM

All replies

  • User28549 posted

    On Android there is the notion of Intents that allow you to do this, however iOS isn't as friendly.

    On iOS you use URI Schemes to launch applications, so you'll want to use that approach there. Android also understands the URI Scheme concept, so you might get away with using this approach for both.

    With this in mind, you would write a custom renderer for each platform. On Android, you can either wire up the intent, or launch the URI Scheme. On iOS, you launch the URI Scheme.... sorry, no idea about Win Phone.

    Here's a list of URI Schemes to get you started.


    added info.

    On Android, if you wish to register your own URI Scheme for other apps to launch, you do it in your manifest.

    <activity android:name=".MyUriActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" android:host="path" /> </intent-filter> </activity>

    And on iOS, to register a URL type for your app, include the CFBundleURLTypes key in your app’s Info.plist file. The CFBundleURLTypes key contains an array of dictionaries, each of which defines a URL scheme the app supports. read more...

    Wednesday, August 12, 2015 2:46 PM
  • User56293 posted

    Hi,

    You could try calling Device.OpenUri with a specific URL schema, as detailed in the following examples :-

    riccardo-moschetti.org/2014/10/03/opening-a-mobile-app-from-a-link-the-xamarin-way-url-schemas/ developer.xamarin.com/recipes/ios/shared_resources/phone/dial-phone-uri/

    Hope this helps.

    Paul Diston

    Wednesday, August 12, 2015 2:47 PM
  • User145768 posted

    @PaulDiston : I want to open other app from my xamarin forms app so how I will achieve the URL because I want just pass name of the app (Installed app in Decive) from my xamarin form app and it open that app(which name i give input field).

    Thursday, August 13, 2015 9:13 AM
  • User177799 posted

    I came across this post earlier today and used most of the information provided to create a solution. I figured I would share for anyone else that comes across this issue. What is shown also will lead the user to the app store to download the app if it has not been previously downloaded on the user's phone.

    Interface (Inside Interfaces folder in PCL):

    namespace YourApp.Interfaces
    {
        public interface IOpenApp
        {
            void OpenExternalApp();
        }
    }
    

    iOS:

    using OpenAppiOS;
    
    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppiOS))]
    namespace YourApp.iOS
    {
        public class OpenAppiOS : IOpenApp
        {
            public OpenAppiOS() { }
    
            public void OpenExternalApp()
            {
                NSUrl request = new NSUrl("yourapp://");
    
                try
                {
                    bool isOpened = UIApplication.SharedApplication.OpenUrl(request);
    
                    if (isOpened == false)
                        UIApplication.SharedApplication.OpenUrl(new NSUrl("yourappurl"));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView("Error", ex.Message, null, "OK", null);
    
                    alertView.Show();
                }
            }
        }
    }
    

    Android:

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppAndroid))]
    namespace YourApp.Droid
    {
        [Activity(Label = "OpenAppAndroid")]
        public class OpenAppAndroid : Activity, IOpenApp
        {
            public OpenAppAndroid() { }
    
            public void OpenExternalApp()
            {
                Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage("yoururl");
    
                // If not NULL run the app, if not, take the user to the app store
                if (intent != null)
                {
                    intent.AddFlags(ActivityFlags.NewTask);
                    Forms.Context.StartActivity(intent);           
                }
                else
                {
                    intent = new Intent(Intent.ActionView);
                    intent.AddFlags(ActivityFlags.NewTask);
                    intent.SetData(Android.Net.Uri.Parse("market://details?id=yoururl"));
                    Forms.Context.StartActivity(intent);
                }
            }
    

    In the MainActivity.cs in Droid add:

    Xamarin.Forms.DependencyService.Register<OpenAppAndroid>();
    

    In the AppDelegate.cs in iOS add:

    Xamarin.Forms.DependencyService.Register<OpenAppiOS>();
    

    To then access the external app in your app use this:

    DependencyService.Get<IOpenApp>().OpenExternalApp();
    

    This should do it for both Android and iOS.

    Wednesday, February 8, 2017 5:10 AM
  • User261478 posted

    @Jordanjhewitt If you would clarify something for me I would greatly appreciate it. We ran into a snag using app links to another app. What we want to do is check if the related app is present, if so open it, otherwise open the url. It appears the IOS code is doing just that, but I am unsure of the Android solution. It seems an intent is established to open the URL first, where we need the open (if present) to be opened first. Additionally, I would like to pass the URL if that is straight forward.

    Thank you in advance.

    Ken

    Friday, May 5, 2017 3:23 PM
  • User177799 posted

    @KenNickerson I'm not the best at Android, but here is what I believe is going on.

    When the intent is being instantiated with this:

    Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage("yoururl");
    

    The intent actually is taking in the package name, not exactly "yoururl". Most package names are formatted com.CompanyName.AppName.

    This should have the intent to launch then the mobile app, not the URL. If the intent to launch the mobile app is then null, the user does not have the mobile application downloaded. Thus, I personally lead the user to App store to download the app, whereas I feel like you want to lead the user to a specific URL.

    In your case, I would have the method public void OpenExternalApp() have a return type of bool instead of void. I would return true if the app is accessed directly and false if the app is not downloaded on the user's phone. Then you could check this bool and use Device.OpenUri(new Uri("https://www.myurihere.com")) to then navigate to the desired URL you would like.

    I hope this helps! If not let me know!

    -Jordan

    Friday, May 5, 2017 7:13 PM
  • User27668 posted

    @Jordanjhewitt in the OpenAppiOS both UIApplication.SharedApplication.OpenUrl(request); and UIApplication.SharedApplication.CanOpenUrl(appURL); always return false.. anything else that needs to be setup...? here's the code:

        public void OpenExternalApp(string app)
        {
            NSUrl appURL; //= new NSUrl("yourapp://");
            string appURI;
    
            if (app == "NOWCLINIC")
            {
                appURL = new NSUrl("NowClinic://");
                //appURL = new NSUrl("NowClinic://com.americanwell.OnlineCareMember.NowClinic"); //this desn't open it either
                appURI = "https://itunes.apple.com/us/app/nowclinic/id602339098?mt=8";
            }
            else
            {
                return;
            }
    
            try
            {
               // bool canOpen = UIApplication.SharedApplication.CanOpenUrl(appURL);
    
                bool isOpened = UIApplication.SharedApplication.OpenUrl(appURL);
    
                if (isOpened == false)
                {
                    UIApplication.SharedApplication.OpenUrl(new NSUrl(appURI));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Cannot open url: {0}, Error: {1}", appURL.AbsoluteString, ex.Message);
                var alertView = new UIAlertView("Error", ex.Message, null, "OK", null);
    
                alertView.Show();
            }
        }
    
    Friday, August 4, 2017 4:52 PM
  • User364073 posted

    Hi @Jordanjhewitt . I'm a new in Xamarin, thanks for your solution. Can I transfer data using the Intent class to my other android application?

    Friday, March 16, 2018 1:57 PM
  • User307954 posted

    How to sending data between one application to another application using xamarin intents. Can you please help for my scenario??

    Tuesday, March 27, 2018 11:16 AM
  • User244838 posted

    @Jordanjhewitt It was amazing ur solution but I struck in Ios CanOpenUrl(appURL); always return false.. anything else that needs to be setup...? I am not able open the app. Please help me

    Thursday, July 5, 2018 1:20 PM
  • User222926 posted

    Regarding the iOS CanOpenUrl and OpenUrl returning false - there was a note about iOS > 9 doing this due to security. Please reference the information _about and above _the Info.plist in the post below. I had added http and https in an attempt to open safari through this method and it is returning 'true' on CanOpenUrl now. https://mindofai.github.io/Launching-Apps-thru-URI-with-Xamarin.Forms/

    Update - nope, I was able to open calendar in iOS without having that in the info.plist so that wasn't it. Sorry for the confusion.

    Tuesday, February 11, 2020 1:48 PM