0

I am working on a app in Xamarin.Forms that has a button for books. I want it to open iBooks if the OS is iOS and Google play books if it is android

I have the iBooks Part down but How would I open google play books from the button click in the android version of the app?

Heres my xaml code:

<StackLayout Orientation="Horizontal" MinimumHeightRequest="30" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
              <Button  Image="books.png" HorizontalOptions="CenterAndExpand" BackgroundColor="Transparent" Clicked="OpenBooks" />
              <Button  Image="settings.png" HorizontalOptions="EndAndExpand" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="gotosettings" />
          </StackLayout>

Heres My C# code:

public void OpenBooks(object sender, EventArgs e)
{
    switch (Device.OS)
    {
        case TargetPlatform.iOS:
            Device.OpenUri(new Uri("itms-books"));
            break;
            case TargetPlatform.Android:
            //open google play books code here
            break;
     }
}

any help would be amazing!

Thank in advance!

Esteban Verbel
  • 608
  • 1
  • 19
  • 36
Phoneswapshop
  • 1,229
  • 8
  • 23
  • 42
  • Do you know the package name of Play Books? If so, you can do as read [here](http://stackoverflow.com/questions/3872063/launch-an-application-from-another-application-on-android). If not, I am not sure you can achieve what you want – Demitrian Jan 05 '17 at 22:21

1 Answers1

1

In the android platform you should know the package name of google play books and check if it is installed already.

In the PCL part you can not achieve it by Device.OpenUri("packagename");

You should use the dependency service to open the google play books app, and The google play books app package name is com.google.android.apps.books :

In the PCL side define the interface:

namespace OpenBooks_Demo
{
    public interface OpenBookInterface
    {
        void openBooks();
    }
}

In the andorid side implements the interface:

[assembly: Xamarin.Forms.Dependency(typeof(OpenBookImp))]
namespace OpenBooks_Demo.Droid
{
    public class OpenBookImp :  Java.Lang.Object, OpenBookInterface
    {
        public OpenBookImp() { }
        public void openBooks()
        {
            var ctx = Forms.Context;

            Intent launchIntent = new Intent();
            launchIntent = ctx.PackageManager.GetLaunchIntentForPackage("com.google.android.apps.books");
            if (launchIntent != null)
            {
                ctx.StartActivity(launchIntent);//null pointer check in case package name was not found
            }
            else
            {
                try
                {
                    ctx.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + "com.google.android.apps.books")));
                }
                catch (Exception e)
                {
                    ctx.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://play.google.com/store/apps/details?id=" + "com.google.android.apps.books")));
                }
            }
        }
    }
}

And then call the openBooks method in the PCL side:

public void OpenBooks(object sender, EventArgs e)
        {
            switch (Device.OS)
            {
                case TargetPlatform.iOS:
                    Device.OpenUri(new Uri("itms-books"));
                    break;
                case TargetPlatform.Android:
                    DependencyService.Get<OpenBookInterface>().openBooks();
                    break;
            }
        }

In my android device I have installed the google play books: enter image description here

Mike X X Ma
  • 1,937
  • 1
  • 10
  • 16