你们有没有人试过在app中实现IAP商店视图控制器,可以从2个不同的视图控制器访问?如果有人能分享这样的经历,我真的很感激。
我正在尝试从2个不同的视图控制器访问StoreViewController(您可以在其中购买应用内购买)。这张图片显示了我的意思:
因此,黑色和蓝色VC都实现了SKProductsRequestDelegate
方法。
这是1的代码,但它在2中是相同的:
let productID1 = "id1"
let productID2 = "id2"
let productID3 = "id3"
let productID4 = "id4"
var product1 : SKProduct?
var product2 : SKProduct?
var product3 : SKProduct?
var product4 : SKProduct?
/////// THIS BUTTON TAKES USER TO STORE
@IBAction goToStore(){
getProductInfo()
}
func getProductInfo(){
if SKPaymentQueue.canMakePayments() {
let request = SKProductsRequest(productIdentifiers: Set([productID1,productID2,productID3,productID4]))
request.delegate = self
request.start()
print("looking for IAP")
}else{
print("make IAP available in Settings")
}
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
let products = response.products
if products.count != 0{
for product in products{
switch product.productIdentifier{
case "id1":
product1 = product
break
case "id2":
product2 = product
break
case "id3":
product3 = product
break
case "id4":
product4 = product
break
default:
break
}
}
print("I do have products")
performAnimation()
// }
}else{
print("product not found")
}
let invalidProducts = response.invalidProductIdentifiers
for productk in invalidProducts{
print("Pr not found \(productk)")
}
}
func performAnimation(){
let story = UIStoryboard(name: "Main", bundle: nil)
let storeViewController = story.instantiateViewControllerWithIdentifier("StoreViewController") as! StoreViewController
storeViewController.product1 = self.product1
storeViewController.product2 = self.product2
storeViewController.product3 = self.product3
storeViewController.product4 = self.product4
self.presentViewController(storeViewController, animated: false, completion: nil)})
})
}
//////////////////// storeViewController中的代码
let productID1 = "id1"
let productID2 = "id2"
let productID3 = "id3"
let productID4 = "id4"
var product1 : SKProduct?
var product2 : SKProduct?
var product3 : SKProduct?
var product4 : SKProduct?
viewDidLoad(){
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
//// PRESSED WHEN USER WANTS TO BUY ITEMS
@IBAction func buyButtonPressed(sender: UIButton) {
let items = itemsToBuy.count
if items > 0{
switch items{
case 1:
let payment = SKPayment(product: product1!)
print("description \(payment.description), identifier: \(payment.productIdentifier), data: \(payment.requestData)")
SKPaymentQueue.defaultQueue().addPayment(payment) ///////// ERROR HERE
case 2:
let payment = SKPayment(product: product2!)
SKPaymentQueue.defaultQueue().addPayment(payment)
case 3:
let payment = SKPayment(product: product3!)
SKPaymentQueue.defaultQueue().addPayment(payment)
case 4:
let payment = SKPayment(product: product4!)
SKPaymentQueue.defaultQueue().addPayment(payment)
default:
break
}
}
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions{
switch transaction.transactionState{
case SKPaymentTransactionState.Purchased:
self.buyItems()
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
print("bought")
case SKPaymentTransactionState.Failed:
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
print("error not bought")
default:
break
}
}
}
我从VC1去商店,成功购买和购买。比我从VC2存储(反之亦然)并且我在switch语句中的行SKPaymentQueue.defaultQueue().addPayment(payment)
上出现错误:EXC_BAD_ACCESS。
当只能从一个VC访问存储而不能从两个VC访问存储时,才会发生此错误。此外,如果我仅从一个VC进行多次付款,它可以正常工作。可能是什么原因?是否只有一个viewController能够实现SKProductsRequestDelegate
方法?
编辑://在SKPaymentQueue.defaultQueue().addPayment(payment)
行之前添加了一个打印语句。购买(1.成功,2。不成功)的结果是
description <SKPayment: 0x174204720>, identifier: id1, data: nil
和
description <SKPayment: 0x17001e1b0>, identifier: id1, data: nil