1

I'm new in the channel; I recently started with xamarin.form for cross-plattform apps.

I've been stuck with a question for a few days. I would like to know if there is a method that allow me to open a third-party application by my app or it's necessary to implement this function for each platform in a different way.

Precisely, I would like to know if there is a method similar to that provided by IFrame in UIWebview for web pages to open third-party apps in my app without launching the second app sending my app in background?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Could you provide more specific example of what you want to archieve? – rudolf_franek Aug 28 '18 at 17:04
  • Sure, imagine a main page in which there are 3 button each one representing a different application (for example facebook, twitter and instagram), when the user clicks on one of these buttons my application should open the selected app in a new page of my application in which I would like to implement a method similar to that of the iframe for web pages (so a visual box in which just launch the third app and make it use to the user). when the user presses in back_button the application will return to the main page – Agata Loiudice Aug 28 '18 at 18:37

2 Answers2

1

you can try this;

 private async void Button_OnClicked(object sender, EventArgs e)
            {  //You need to change your app package address which you want to open with button. Example: "com.microsoft.office.officelens"
                Intent intent = new Intent();
                intent.SetComponent(new ComponentName("com.package.address", "com.package.address.MainActivity")); 
                Android.App.Application.Context.StartActivity(intent);
            }

Also, if you want to check user downloaded or didn't download this app;

public string facebookApp;



public async Task ShowAlert()
        {
            this.facebookApp = "https://play.google.com/store/apps/details?id=com.facebook.katana&hl=tr";
            var action = await DisplayAlert("Warning", "Please download facebook app first.", "Okay", "Cancel");
            if (action)
            {
                Device.OpenUri(new Uri(this.facebookApp));
            }
        }

private async void Button_OnClicked(object sender, EventArgs e)
           {  
                try
                {
                    Intent intent = new Intent();
                    intent.SetComponent(new ComponentName("com.package.address", "com.package.address.MainActivity")); 
                    Android.App.Application.Context.StartActivity(intent);
                }
                catch (Exception ex)
                {
                    ShowAlert();
                }
           }

So, in this case; if the user didn't download xApp(facebook, twitter, etc.) before, he/she will get a warning. If they click Okay on DisplayAlert, the program will send them to Google Play link.

Guts
  • 23
  • 3
0

This works very well for me.

In xamarin forms in the layout file put this button

<Button Text="Open Facebook"
            Margin="20"
            Clicked="Button_Clicked"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="FillAndExpand"
            BackgroundColor="#1b89c0"
            TextColor="White"/>

Next the .cs of this layout, I have the button and implementing the dependency service.

async void Button_Clicked(System.Object sender, System.EventArgs e)
    {
        DependencyService.Get<IBackgroundDependency>().ExecuteCommand();
    }

The next step is make a new interface

public interface IBackgroundDependency
{
    void ExecuteCommand();
}

and finish with the class inside the android directory.

[assembly: Dependency(typeof(BackgroundDependency_Android))]
namespace AbrirApp.Droid{
public class BackgroundDependency_Android : Java.Lang.Object, IBackgroundDependency
{

    public void ExecuteCommand()
    {

        Intent intent = new Intent();
        intent.SetFlags(ActivityFlags.NewTask);
        intent.SetComponent(new ComponentName("com.example.averesta", "com.example.averesta.MainActivity"));
        Android.App.Application.Context.StartActivity(intent);

    }

  }
}

new ComponentName("Package name", "Package name activity")

In my case the main application is in xamarin, and the invoked application is in android studio.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • How to do this in Xamarin.Forms **iOS app**? This line `UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(...);` opens it in new window. I need to open it within the app window itself. – Thamarai T Jul 08 '22 at 09:59