所以我试图在我的应用中添加In App Purchase来删除广告,但我的广告在另一个视图中,我认为我正在以错误的方式删除它,因为它正在破坏removeallAds函数:
func removeallAds() {
ViewController().bannerAd.removeFromSuperview()
}
它给了我以下错误:
致命错误:在展开Optional值时意外发现nil (lldb)
这是我的代码,如果你想看看:
import UIKit
import StoreKit
class IapViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
@IBOutlet weak var removeAds: UIButton!
@IBOutlet weak var restorePurchase: UIButton!
@IBAction func removeAdsAct(sender: AnyObject) {
for product in list{
var prodId = product.productIdentifier
if (prodId == "com.hazeApps.removeAds"){
p = product
buyProduct()
break;
}
}
}
@IBAction func resPurchaseAct(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
override func viewDidLoad() {
super.viewDidLoad()
removeAds.enabled = false
//IAP Setup
if(SKPaymentQueue.canMakePayments()){
println("IAP is up and running")
var productId: NSSet = NSSet(object: "com.hazeApps.removeAds")
var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productId)
request.delegate = self
request.start()
} else {
println("enable IAPs")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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 removeallAds() {
ViewController().bannerAd.removeFromSuperview()
}
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
println("Product Request")
var myProducts = response.products
for product in myProducts{
println("product added")
println(product.productIdentifier)
println(product.localizedTitle)
println(product.localizedDescription)
println(product.price)
list.append(product as SKProduct)
}
removeAds.enabled = true
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
println("Transaction restored")
var purchasedItemIDS = []
for transaction in queue.transactions{
var t: SKPaymentTransaction = transaction as SKPaymentTransaction
let prodId = t.payment.productIdentifier as String
switch prodId{
case "com.hazeApps.removeAds":
println("Remove Adds")
removeallAds()
default:
println("IAP not setup")
}
}
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("add payment")
for transaction: AnyObject in transactions{
var trans = transaction as SKPaymentTransaction
println(trans.error)
switch trans.transactionState{
case .Purchased:
println("Unlock IAP here")
println(p.productIdentifier)
let productId = p.productIdentifier as String
switch productId{
case "com.hazeApps.removeAds":
println("Remove Adds")
removeallAds()
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("Removed Trans")
}
}
另外,我有一些代码行可以处理无线连接以使bannerAd隐藏,如果从superview中删除广告,它可能会在这一行中出现一些错误?