Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
hyperPad

hyperPad Forum

  1. Home
  2. Help and Support
  3. In App Purchase Tutorial on Exported Projects

In App Purchase Tutorial on Exported Projects

Scheduled Pinned Locked Moved Help and Support
1 Posts 1 Posters 375 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HamedH Offline
    HamedH Offline
    Hamed
    Admin
    wrote on last edited by Hamed
    #1

    In App purchase swift code for exported projects:

    first add pod 'SwiftyStoreKit' to the list of pods in Podfile in a text editor.

    Then in terminal, run pod install. If you don't have cocoapods installed on your mac, follow the instructions here: https://guides.cocoapods.org/using/getting-started.html

    Replace the contents of HPSwift.swift with the following file

    //
    //  HPSwift.swift
    //  hyperPad-Project
    //
    //  Created by Hamed Saadat on 2019-08-07.
    //
    
    import Foundation
    import SwiftyStoreKit
    
    @objc class HPSwift: NSObject {
        fileprivate var _behaviours: HPBehaviours? = nil
        @objc var behaviours: HPBehaviours? {
            get {
                return _behaviours
            }
            
            set(behaviours) {
                _behaviours = behaviours
                
                SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
                    for purchase in purchases {
                        switch purchase.transaction.transactionState {
                        case .purchased, .restored:
                            if purchase.needsFinishTransaction {
                                // Deliver content from server, then:
                                SwiftyStoreKit.finishTransaction(purchase.transaction)
                            }
                            // Unlock content
                            behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
                            
                        case .failed, .purchasing, .deferred:
                            behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseError")
                            break // do nothing
                        @unknown default:
                            break
                        }
                    }
                }
                
                behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in
                    SwiftyStoreKit.restorePurchases(atomically: true) { results in
                        if results.restoreFailedPurchases.count > 0 {
                            behaviours?.broadcastValue("Restore Failed: \(results.restoreFailedPurchases)" as NSString, withKey: "purchaseError")
                        }
                        else if results.restoredPurchases.count > 0 {
                            results.restoredPurchases.forEach { (purchase) in
                                behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
                            }
                        }
                        else {
                            print("Nothing to Restore")
                        }
                    }
                })
                
                /*
                 Add Behaviour functionality here
                */
                
                behaviours?.addReceiveKey("purchase", onReceive: { (recieveValue) in
                     // handle in app purchase logic! //
                        SwiftyStoreKit.purchaseProduct("INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue", quantity: 1, atomically: true) { result in
                            switch result {
                            case .success(let purchase):
                                behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
                            case .error(let error):
                                var message = ""
                                switch error.code {
                                case .unknown: message = "Unknown error. Please contact support"
                                case .clientInvalid: message = "Not allowed to make the payment"
                                case .paymentCancelled: break
                                case .paymentInvalid: message = "The purchase identifier was invalid"
                                case .paymentNotAllowed: message = "The device is not allowed to make the payment"
                                case .storeProductNotAvailable: message = "The product is not available in the current storefront"
                                case .cloudServicePermissionDenied: message = "Access to cloud service information is not allowed"
                                case .cloudServiceNetworkConnectionFailed: message = "Could not connect to the network"
                                case .cloudServiceRevoked: message = "User has revoked permission to use this cloud service"
                                default: message = ((error as NSError).localizedDescription)
                                }
                                behaviours?.broadcastValue(message as NSString, withKey: "purchaseFailed")
                            }
                        }
                })
            }
        }
        
    }
    

    Some key points:

    behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in
                    // add your code here
                })
    

    Will get triggered when you trigger a broadcast behaviour with the restorePurchases key

    behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")

    Will trigger a receive message behaviour with the purchaseComplete key

    INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue can be replaced with either the identifier of your purchase from AppStoreConnect. OR the receiveValue parameter. If you use receiveValue make sure in your broadcast behaviour, you're sending the identifier from App Store Connect.

    1 Reply Last reply
    1

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search