복붙노트

[SWIFT] SKScene에서 앱 구매에

SWIFT

SKScene에서 앱 구매에

해결법


  1. 1.먼저, 게임 장면 라인에 넣고 당신이 프레임 워크 'StoreKit'수입이 있는지 확인

    먼저, 게임 장면 라인에 넣고 당신이 프레임 워크 'StoreKit'수입이 있는지 확인

    class GameScene: SKScene, SKPaymentTransactionObserver, SKProductsRequestDelegate {
    

    다음으로, 당신은 당신의 didmovetoview 이러한 라인을 데려 가고 싶다는 것. 당신이 아이튠즈에 연결하여 설정 식별자 당신이 넣어 문자열 앱 구매에해야 다음 "개체"다음에 있음을 유의하십시오.

    // Set IAPS
        if(SKPaymentQueue.canMakePayments()) {
            println("IAP is enabled, loading")
            var productID:NSSet = NSSet(objects: "Put IAP id here")
            var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
            request.delegate = self
            request.start()
        } else {
            println("please enable IAPS")
        }
    

    다른 기능의 외부는 여전히 게임 장면에서,이 함수와 변수를 삽입

    //In App Purchases
    var list = [SKProduct]()
    var p = SKProduct()
    
    func buyProduct() {
        println("buy " + p.productIdentifier)
        var pay = SKPayment(product: p)
        SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
    }
    
    func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
        println("product request")
        var myProduct = response.products
    
        for product in myProduct {
            println("product added")
            println(product.productIdentifier)
            println(product.localizedTitle)
            println(product.localizedDescription)
            println(product.price)
    
            list.append(product as! SKProduct)
        }
    }
    
    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
        println("transactions restored")
    
        var purchasedItemIDS = []
        for transaction in queue.transactions {
            var t: SKPaymentTransaction = transaction as! SKPaymentTransaction
    
            let prodID = t.payment.productIdentifier as String
    
            switch prodID {
            case "IAP id here":
    
                //Right here is where you should put the function that you want to execute when your in app purchase is complete
            default:
                println("IAP not setup")
            }
    
        }
    
        var alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored. You may have to restart the app before banner ads are removed.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
    }
    
    
    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
        println("add paymnet")
    
        for transaction:AnyObject in transactions {
            var trans = transaction as! SKPaymentTransaction
            println(trans.error)
    
            switch trans.transactionState {
    
            case .Purchased, .Restored:
                println("buy, ok unlock iap here")
                println(p.productIdentifier)
    
                let prodID = p.productIdentifier as String
                switch prodID {
                case "IAP id here":
    
                    //Here you should put the function you want to execute when the purchase is complete
                    var alert = UIAlertView(title: "Thank You", message: "You may have to restart the app before the banner ads are removed.", delegate: nil, cancelButtonTitle: "OK")
                    alert.show()
                default:
                    println("IAP not setup")
                }
    
                queue.finishTransaction(trans)
                break;
            case .Failed:
                println("buy error")
                queue.finishTransaction(trans)
                break;
            default:
                println("default")
                break;
    
            }
        }
    }
    
    func finishTransaction(trans:SKPaymentTransaction)
    {
        println("finish trans")
    }
    func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
    {
        println("remove trans");
    }
    

    당신이 노드의 이름을 지정해야합니다 다음으로는 IAP를 할 필요가

    whateverYourNodeIs.name = "inAppPurchaseNode"
    

    마지막으로, touchesBegan에서이 작업을 수행

      let touch =  touches.first as? UITouch
      let positionInScene = touch!.locationInNode(self)
      let touchedNode = self.nodeAtPoint(positionInScene)
    
    if let name = touchedNode.name {
            if name == "inAppPurchaseNode" {
    
                    for product in list {
                        var prodID = product.productIdentifier
                        if(prodID == "iAp id here") {
                            p = product
                            buyProduct()  //This is one of the functions we added earlier
                            break;
                        }
                    }
                }
    
        }
    

    당신은 또한 당신의 감각이 다른 노드를 사용하여 구매를 복원하기 시작 할 것입니다.

        if let name = touchedNode.name {
            if name == "restore" {
    
                 SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
                SKPaymentQueue.defaultQueue().addTransactionObserver(self)
                 }
            }
    
  2. from https://stackoverflow.com/questions/31907050/in-app-purchase-in-skscene by cc-by-sa and MIT license