Whitegorilla
Whitegorilla

Reputation: 15

Issues with in app-purchases. The name 'DependencyService' does not exist in the current

I try how to implement consumable in-app purchases in my project.

Tutorials:

Purchase consumable

Securing in-app purchases

I have read these tutorials but I don't know what DependencyService, Debug and Device is. I get the following error messages:

Error CS0103: The name 'DependencyService' does not exist in the current context (CS0103) Error CS0103: The name 'Device' does not exist in the current context (CS0103) Error CS0103: The name 'Debug' does not exist in the current context (CS0103)

In addition, I don't understand what this line of code means. Why are we checking Device.RuntimePlatform == Device.iOS ? What should I do if it is true and what should I do when it is false?

 if(Device.RuntimePlatform == Device.iOS)
            return;

This is how I implemented the code to my project:

 string payload = "apppayload";
    public async Task<bool> PurchaseItem(string productId, string payload)
    {
        if (!CrossInAppBilling.IsSupported)
            return false;

        var billing = CrossInAppBilling.Current;
        try
        {
            var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
            if (!connected)
            {
                //we are offline or can't connect, don't try to purchase
                return false;
            }

            var verify = DependencyService.Get<IInAppBillingVerifyPurchase>();

            //check purchases
            var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, payload, verify);

            //possibility that a null came through.
            if (purchase == null)
            {
                //did not purchase
            }
            else if (purchase.State == PurchaseState.Purchased)
            {
                //purchased, we can now consume the item or do it later

                //If we are on iOS we are done, else try to consume the purchase
                //Device.RuntimePlatform comes from Xamarin.Forms, you can also use a conditional flag or the DeviceInfo plugin
                if (Device.RuntimePlatform == Device.iOS)
                    return false;

                var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);

                if (consumedItem != null)
                {
                    //Consumed!!
                }
            }
        }
        catch (InAppBillingPurchaseException purchaseEx)
        {
            //Billing Exception handle this based on the type
            Debug.WriteLine("Error: " + purchaseEx);
        }
        catch (Exception ex)
        {
            //Something else has gone wrong, log it
            Debug.WriteLine("Issue connecting: " + ex);
        }
        finally
        {
            await billing.DisconnectAsync();
        }

        return false;
    }

Upvotes: 1

Views: 368

Answers (1)

Legion
Legion

Reputation: 791

Mh... "DependencyService" and "Device" are subClasses of Xamarin.Forms, so if you haven't already do that add:

using Xamarin.Forms;

on top of your code

If DependencyService/Debug still not exist i think you're in the wrong project, you need to use it in your "portable" project, and not in OS-Related projects. Try to study how Cross-Platform xamarin projects works :)

Legion-

Upvotes: 2

Related Questions