Teknasyon-Teknoloji / deepwall-ios-sdk

Managing apps that offer in-app purchases have never been easier with Deepwall
https://docs.deepwall.com/sdk-framework/sdk-installation/install-ios
MIT License
9 stars 3 forks source link

How can I use on SwiftUI #3

Open Borzalic opened 3 years ago

Borzalic commented 3 years ago

When I use : DeepWall.shared.requestPaywall(action: "UNLOCKFEATURES", in: self) Then I got error : Cannot convert value of type 'ContentView' to expected argument type 'UIViewController'

Can you explain how can I use on SwiftUI ?

kellvembarbosa commented 2 years ago

Hello, After a few hours lost I was able to implement it and it's working very well.

I'm apologizing for any error, it's only been 2 months since I've been working with swiftui, today I'm more reactjs... I was able to do the integration, I created one: UIViewControllerRepresentable

I also created a class to manage the subscription throughout the app:

import Foundation
import DeepWall

class PurchaseDataSource: ObservableObject {
    @Published var purchased = false

    init(){
        purchased = UserDefaults.standard.bool(forKey: AppConfig.premiumVersion)
    }

    func showPaywallDeepWall(action: String) {
        if(!self.purchased){
            if let rootController = UIApplication.shared.keyWindow?.rootViewController {
                if DeepWall.shared.readyStatus {
                        DeepWall.shared.requestPaywall(action: action, in: rootController)
                }
            }
        }
    }

    func sestUserProperties(){
        let userProperties = DeepWallUserProperties(uuid: self.getUUID(), country: self.getCountryCode(), language: self.getLangCode())
        DeepWall.shared.setUserProperties(userProperties)
    }

    func unlockPremium(){
        self.purchased = true
        UserDefaults.standard.set(true, forKey: AppConfig.premiumVersion)
    }

    func lockPremium(){
        self.purchased = false
        UserDefaults.standard.set(false, forKey: AppConfig.premiumVersion)
    }

    func getUUID() -> String {
        let userDefaults = UserDefaults.standard

        if (userDefaults.string(forKey: "uuid") ?? "").isEmpty == true {
            userDefaults.set(UUID().uuidString,forKey: "uuid")
            userDefaults.synchronize()
            return userDefaults.string(forKey: "uuid") ?? ""
        } else {
            return userDefaults.string(forKey: "uuid") ?? ""

        }
    }

    func getLangCode() -> String {
        if let langCode = Locale.current.languageCode {
            return langCode.count > 2 ? "Unknown" : langCode
        } else {
            return "Unknown"
        }
    }

    func getCountryCode() -> String {
        if let regionCode = Locale.current.regionCode {
            return regionCode.count > 2 ? "Unknown" : regionCode
        } else {
            return "Unknown"
        }
    }
}

In AppDelegate to listen to the events:

import DeepWall

/// App delegate for SwiftUI application
class AppDelegate: NSObject, UIApplicationDelegate, DeepWallNotifierDelegate  {

    var purchaseDataSource = PurchaseDataSource()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

        let environment: DeepWallEnvironment = .sandbox
        DeepWall.initialize(apiKey: AppConfig.DEEPWALL_KEY, environment: environment)

        print("AppDelagate: \(self.getUUID()) - \(self.getCountryCode()) - \(self.getLangCode())")

        let userProperties = DeepWallUserProperties(uuid: self.getUUID(), country: self.getCountryCode(), language: self.getLangCode())
        DeepWall.shared.setUserProperties(userProperties)
        DeepWall.shared.observeEvents(for: self)

        return true
    }

    // ...

    func deepWallPaywallPurchaseSuccess(_ event: DeepWallValidateReceiptResult) {
        print("DeepWall: deepWallPaywallPurchaseSuccess \(String(describing: event.result?.subscriptions))")

        if let subscriptions = event.result?.subscriptions {

            if(subscriptions.isEmpty){
                purchaseDataSource.lockPremium()
                return
            }

            for subscription in subscriptions {
                print("DeepWall: subscription: \(subscription.isActive) - refunded: \(subscription.isRefunded)")

                /// here I have a cruel doubt, will the user have more than 1 item in this subscription array?
                /// Only in the future will I be able to see if this logic will have a problem

                if(subscription.isRefunded) {
                    purchaseDataSource.lockPremium()
                    return
                }

                if(subscription.isActive) {
                    purchaseDataSource.unlockPremium()
                    DeepWall.shared.closePaywall()
                } else {
                    purchaseDataSource.lockPremium()
                }
            }
        }

    }

    func deepWallPaywallResponseFailure(_ event: DeepWallPaywallResponseFailedModel) {
        print("DeepWall: deepWallPaywallResponseFailure \(event.reason)")
        //UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true)
    }

    func deepWallPaywallClosed(_ event: DeepWallPaywallClosedInfoModel) {
        print("DeepWall: deepWallPaywallClosed")
    }

    func deepWallPaywallRestoreSuccess() {
        print("DeepWall Delegate: deepWallPaywallRestoreSuccess")
        DeepWall.shared.closePaywall()
    }

    func deepWallPaywallPurchasingProduct(_ event: DeepWallPaywallPurchasingProduct) {
        print("DeepWall Delegate: deepWallPaywallPurchasingProduct")
    }

on @main

@main
struct HelloWorldApp: App {
   init(){
      appDelegate.purchaseDataSource.sestUserProperties()
    }

on first subview here is custom to app for app and strategy for actions to show paywall:

   .onAppear{
      purchaseDataSource.showPaywallDeepWall(action: "AppLaunch")
   }

Now always call purchaseDataSource.showPaywallDeepWall(action: "Name_to_action")