如果我的应用程序记得我购买了应用程序内购买,我该怎么办?

时间:2015-08-08 16:23:31

标签: ios swift in-app-purchase

我已经制作了一款应用,并为优质版本进行了应用内购买。因为我无法在模拟器中测试应用内购买。

所以这是我的代码:

class settingsViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver, ADBannerViewDelegate {

@IBOutlet var adBennerView: ADBannerView!
@IBOutlet var outletRemoveAds: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    if(SKPaymentQueue.canMakePayments()) {
        println("IAP is enabled, loading")
        var productID:NSSet = NSSet(objects: "xxx")
        var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        request.delegate = self
        request.start()
    } else {
        println("please enable IAPS")
    }
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func removeAds(sender: AnyObject) {
    for product in list {
        var prodID = product.productIdentifier
        if(prodID == "xxx") {
            p = product
            buyProduct()
            break;
        }
    }

}
@IBAction func restorePurchases(sender: AnyObject) {
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

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)
    }
    outletRemoveAds.enabled = true
}
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:
            println("buy, ok unlock iap here")
            println(p.productIdentifier)

            let prodID = p.productIdentifier as String
            switch prodID {
            case "xxx":
                println("remove ads")
                removeAds()
                isPremium = true
            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")
    SKPaymentQueue.defaultQueue().finishTransaction(trans)
}

func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
{
    println("remove trans");
}

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 "xxx":
            println("remove ads")
            removeAds()
            isPremium = true

        default:
            println("IAP not setup")
        }

    }
}

func removeAds() {
    adBennerView!.removeFromSuperview()
}

如果用户购买了高级版本 - isPremium变为true,那么我怎么知道每次用户登录isPremium时仍然如此?

1 个答案:

答案 0 :(得分:0)

这是因为您只将isPremium存储在RAM而不是其他任何地方。尝试使用NSUserDefaults来实现这一目的,只是为了看看它是如何完成的(实际上建议不要将NSUserDefaults用于那种类型的信息,因为它很容易入侵,但你必须明白存储有关您的应用状态的信息的基础知识。)