我在应用程序的新版本中添加了iTunes Connect中的应用程序内购买,在应用程序中我添加了一个带有2个按钮的视图,一个用于购买,另一个用于恢复以前的购买,但我不知道代码,因为现在我的vIEwcontroller的inapp部分是
import StoreKitvar productIDs: Array<String!> = []var productsArray: Array<SKProduct!> = []class VIEwController: UIVIEwController,SKProductsRequestDelegate {
这样可以吗?如果我只有一个应用内购买,我是否需要阵列?
我还在功能部分添加了应用程序内购买,但我在标签“添加应用程序购买权利到您的应用程序ID”旁边有红色感叹号,但我看到了游戏中心部分和排行榜和成就的相同内容工作所以我认为我不必担心它
关于删除添加,这是最好的方法吗?
我使用admob,我想在Appdelegate中添加一个if语句,并在其中添加create和load interstital部分
如果用户进行了购买,或者每次用户打开应用程序时都必须检查,那么在代码中有一些变量设置为true?
提前感谢您的回答
解决方法 以下适用于Swift 2.首先,在Itunes Connect中进行IAP
导入Storekit
import StoreKit
添加StoreKit代理
class VIEwController: UIVIEwController,SKProductsRequestDelegate,SKPaymentTransactionObserver {
声明您的产品ID
var product_ID: Nsstring?overrIDe func vIEwDIDLoad() { product_ID = "YOUR_PRODUCT_ID" super.vIEwDIDLoad() SKPaymentQueue.defaultQueue().addTransactionObserver(self) //Check if product is purchased if (NSUserDefaults.standardUserDefaults().boolForKey("purchased")){ // HIDe ads adVIEw.hIDden = true } else { print("Should show ads...") }}
解锁内容:这是按钮 *** 作,将初始化购买
@IBAction func unlockAction(sender: AnyObject) { print("About to fetch the product...")// Can make paymentsif (SKPaymentQueue.canMakePayments()) { let productID:NSSet = NSSet(object: self.product_ID!); let productsRequest:SKProductsRequest = SKProductsRequest(productIDentifIErs: productID); productsRequest.delegate = self; productsRequest.start(); println("Fetching Products"); }else{ print("Can't make purchases"); }}
助手方法
func buyProduct(product: SKProduct){ println("Sending the Payment Request to Apple"); let payment = SKPayment(product: product) SKPaymentQueue.defaultQueue().addPayment(payment); }
IAP的代表方法
func productsRequest (request: SKProductsRequest,dIDReceiveResponse response: SKProductsResponse) { let count : Int = response.products.count if (count>0) { var valIDProduct: SKProduct = response.products[0] as SKProduct if (valIDProduct.productIDentifIEr == self.product_ID) { print(valIDProduct.localizedTitle) print(valIDProduct.localizedDescription) print(valIDProduct.price) buyProduct(valIDProduct); } else { print(valIDProduct.productIDentifIEr) } } else { print("nothing") }}func request(request: SKRequest!,dIDFailWithError error: NSError!) { print("Error Fetching product information");} func paymentQueue(_ queue: SKPaymentQueue,updatedTransactions transactions: [SKPaymentTransaction]){ print("Received Payment Transaction Response from Apple"); for transaction:AnyObject in transactions { if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{ switch trans.transactionState { case .Purchased: print("Product Purchased"); SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction) // Handle the purchase NSUserDefaults.standardUserDefaults().setBool(true,forKey: "purchased") adVIEw.hIDden = true break; case .Failed: print("Purchased Failed"); SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction) break; case .Restored: print("Already Purchased"); SKPaymentQueue.defaultQueue().restoreCompletedTransactions() // Handle the purchase NSUserDefaults.standardUserDefaults().setBool(true,forKey: "purchased") adVIEw.hIDden = true break; default: break; } } }}
恢复购买
将此添加到函数:
if (SKPaymentQueue.canMakePayments()) { SKPaymentQueue.defaultQueue().restoreCompletedTransactions()}总结
以上是内存溢出为你收集整理的Swift IOS 9 – 以最简单的方式实现应用内购买全部内容,希望文章能够帮你解决Swift IOS 9 – 以最简单的方式实现应用内购买所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)