jamesmontemagno / InAppBillingPlugin

Cross-platform In App Billing Plugin for .NET
MIT License
661 stars 152 forks source link

System.NotImplementedException in Plugin.InAppBilling on iOS with .NET MAUI #644

Open tecomunico opened 4 days ago

tecomunico commented 4 days ago

Bug Information

Steps to reproduce the Behavior

  1. Installed Plugin.InAppBilling version 8.0.5 in a .NET MAUI project.
  2. Set up in-app purchase functionality according to the documentation, including initializing CrossInAppBilling.Current and calling PurchaseAsync.
  3. Ran the app on a physical iOS device (iPhone 14 Pro Max) using Debug mode.
  4. Triggered the purchase process by clicking the subscription button.

Expected Behavior

The app should connect to the App Store, initiate the purchase, and handle any errors or completion events as expected.

Actual Behavior

The app throws a System.NotImplementedException with the message: "This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation."

Code snippet

private async void OnSubscribeClicked(object sender, EventArgs e) { LogToFile("OnSubscribeClicked - User initiated subscription process.");

var billing = CrossInAppBilling.Current;
if (billing == null)
{
    LogToFile("InAppBilling service not initialized.");
    return;
}

try
{
    LogToFile("Attempting to connect to billing service...");
    bool connected = await billing.ConnectAsync();

    LogToFile($"Billing service connected: {connected}");
    if (!connected)
    {
        LogToFile("Connection to billing service failed.");
        return;
    }

    LogToFile("Attempting purchase...");
    var purchase = await billing.PurchaseAsync("com.jabezsoftware.jabeztime.annual", ItemType.Subscription, "payload");

    LogToFile($"Purchase state: {(purchase != null ? purchase.State.ToString() : "null")}");
    if (purchase != null && purchase.State == PurchaseState.Purchased)
    {
        LogToFile("Purchase successful. Activating subscription...");
        await Services.TrialManager.ActivateSubscription();

        SubscriptionMessage.IsVisible = true;
        SubscribeButton.IsVisible = false;
        LogToFile("Subscription successfully activated.");
    }
    else
    {
        LogToFile("Purchase was not completed or canceled.");
    }
}
catch (InAppBillingPurchaseException purchaseEx)
{
    LogToFile($"InAppBillingPurchaseException: {purchaseEx.Message}");
}
catch (Exception ex)
{
    LogToFile($"General Exception in OnSubscribeClicked: {ex.Message}");
}
finally
{
    LogToFile("Disconnecting from billing service...");
    await billing.DisconnectAsync();
    LogToFile("Disconnected from billing service.");
}

}

    public static void LogToFile(string message)
    {
        try
        {
            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", "Caches", "log.txt");
            File.AppendAllText(path, $"{DateTime.Now}: {message}\n");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Failed to write log: {ex.Message}");
        }
    }

Error Message

Here is the error message displayed in the output:

System.NotImplementedException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation. at Plugin.InAppBilling.CrossInAppBilling.get_Current() at JabezTime.Views.SubscriptionPage.OnSubscribeClicked(Object sender, EventArgs e) in /Users/edgargarcia/Projects/Jabez Time/Jabez Time/Views/SubscriptionPage.xaml.cs:21 at System.Threading.Tasks.Task.<>c.b__128_0(Object state) at Foundation.NSAsyncSynchronizationContextDispatcher.Apply() in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:176 at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:58 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:94 at JabezTime.Program.Main(String[] args) in /Users/edgargarcia/Projects/Jabez Time/Jabez Time/Platforms/iOS/Program.cs:13

Screenshots

Screenshot 2024-11-11 at 2 13 17 PM

Additional Notes

To help trace the issue, I implemented a logging mechanism that writes to a log.txt file to track the execution flow within the app. Despite this, I have been unable to pinpoint the exact source of the error. I’ve tested the app on physical devices in both Debug and Release modes, utilizing all available debugging tools in Visual Studio for Mac and Xcode. However, the error persists without providing a clear trace to diagnose the cause.

I would appreciate any further guidance on resolving this issue, as I’ve followed the configuration steps outlined in the documentation and have double-checked that the plugin is correctly installed and referenced.

AdrianMa1996 commented 4 hours ago

Hello, I think I can confirm the bug. In my opinion the error lies with the InAppBillingImplementation class. The class is not implemented in IOS. Here is code to check it:

`

if ANDROID

        var inAppBillingImplementation = new InAppBillingImplementation(); // works

endif

if ios

        var inAppBillingImplementation = new InAppBillingImplementation(); // works

endif

if IOS

        var inAppBillingImplementation = new InAppBillingImplementation(); // error: The type or namespace name 'InAppBillingImplementation' could not be found

endif

`

If you write ios in lower case, it finds the class. Maybe an incorrect configuration in the InAppBilling.csproj for the *.apple.cs files. It worked with version 7.1.3.