Kat*_*ins 5 nsuserdefaults in-app-purchase ios swift
我有一个简单的应用程序,有一个非消费类应用内购买选项.
从我的初始视图控制器,我有一个"输入"按钮.
此按钮将向一个TabBarController"A"以及一系列视图和"付费"用户发送一个"免费"用户(尚未购买非消费品)给另一个TabBarController"B"一组不同的视图.这些视图永远不会相交.
我想检查一下,我的代码能够有效地区分用户是否进行了应用内购买或其他方式.
这是我的代码:
import UIKit
import StoreKit
class MainMainViewController: UIViewController, UIScrollViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;
...
override func viewDidLoad() {
product_id = "some.iap.id";
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
super.viewDidLoad()
}
@IBAction func Enter(sender: AnyObject) {
//Check if product is purchased
if (defaults.boolForKey("purchased")){
print("User has purchased da goods!")
// Grant or otherwise full access based on whether user has purchased/not purchased.
// Goto TabBarController A - FULL Access:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerPaid") as! TabBarControllerPaid
self.presentViewController(vc, animated: true, completion: nil)
}
else if (!defaults.boolForKey("purchased")){
print("user has NOT purchased yet")
// Goto TabBarController B - PARTIAL Access:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerFree") as! TabBarControllerFree
self.presentViewController(vc, animated: true, completion: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何答案,评论或想法:-)
| 归档时间: |
|
| 查看次数: |
5587 次 |
| 最近记录: |