How to share an url to my app?

I want to share an url, the url of page where I am, to my Xamarin Forms App (Android and iOS).

And when I',m going to share this url...

I want that my app appears into the share options list, to send this url to my app to a specific page, beacause I want to use this url strings into the app.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We have to implement it on each platform.
    For Android:
    Firstly, Add this intent filter to your MainActivity

    [IntentFilter(new[] { "android.intent.action.SEND" }, Categories = new[] { Intent.CategoryDefault }, DataMimeTypes = new[] { "text/plain" })]
    

    Then this app could be opened by your browser. And we could retrieve the url through

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
    
        base.OnCreate(savedInstanceState);
    
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    
        if (this.Intent.Action == Intent.ActionSend)
        {
            var uri = this.Intent.Extras.GetString(Intent.ExtraText);
            // Use messaging center to pass this url to Forms project.
            // ...
        }
    }
    

    For iOS:
    We have to implement the share extension to add our applications in the share functionality in Safari:
    https://docs.microsoft.com/en-us/xamarin/ios/platform/extensions

Sign In or Register to comment.