我正在尝试在我的应用中实施单个应用内购买以移除广告,从iTunes Connect端非常容易,但对于代码部分,我只找到带有tableview和/或旧版swift或objC的教程< / p>
我在应用程序的新版本中添加了iTunes Connect中的应用程序内购买,在应用程序中我添加了一个带有2个按钮的视图,一个用于购买,另一个用于恢复以前的购买,但我不知道代码,现在我的viewcontroller的inapp部分是
import StoreKit
var productIDs: Array<String!> = []
var productsArray: Array<SKProduct!> = []
class ViewController: UIViewController, SKProductsRequestDelegate {
这可以吗?如果我只有一个应用程序内购买,我是否需要数组?
我还在功能部分添加了应用程序内购买,但我在标签“添加应用程序购买权利到您的应用程序ID”旁边有红色感叹号,但我看到游戏中心部分和排行榜和成就是有效的,所以我认为我不必担心它
关于删除add,这是最好的方法吗?
我使用了admob,我想在Appdelegate中添加一个if语句,并在其中添加create和load interstital部分 在代码中,如果用户进行了购买,则有一些变量设置为true,或者每次用户打开应用程序时都必须检查它?
提前感谢您的回答
答案 0 :(得分:8)
WMios在 Swift 3 中回答。
首先,在Itunes Connect中进行IAP
import StoreKit
class ViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
var product_id: String?
override func viewDidLoad() {
super.viewDidLoad()
product_id = "YOUR_PRODUCT_ID"
SKPaymentQueue.default().add(self)
//Check if product is purchased
if (UserDefaults.standard.bool(forKey: "purchased")){
// Hide ads
//adView.hidden = true
} else {
print("Should show ads...")
}
}
@IBAction func unlockAction(sender: AnyObject) {
print("About to fetch the product...")
// Can make payments
if (SKPaymentQueue.canMakePayments())
{
let productID:NSSet = NSSet(object: self.product_id!);
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
productsRequest.delegate = self;
productsRequest.start();
print("Fetching Products");
}else{
print("Can't make purchases");
}
}
func buyProduct(product: SKProduct){
print("Sending the Payment Request to Apple");
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment);
}
func productsRequest (_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
let count : Int = response.products.count
if (count>0) {
let validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
print(validProduct.localizedTitle)
print(validProduct.localizedDescription)
print(validProduct.price)
buyProduct(product: validProduct);
} else {
print(validProduct.productIdentifier)
}
} else {
print("nothing")
}
}
func request(_ request: SKRequest, didFailWithError error: Error) {
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.default().finishTransaction(transaction as! SKPaymentTransaction)
// Handle the purchase
UserDefaults.standard.set(true , forKey: "purchased")
//adView.hidden = true
break;
case .failed:
print("Purchased Failed");
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .restored:
print("Already Purchased");
SKPaymentQueue.default().restoreCompletedTransactions()
// Handle the purchase
UserDefaults.standard.set(true , forKey: "purchased")
//adView.hidden = true
break;
default:
break;
}
}
}
}
将此添加到函数:
if (SKPaymentQueue.canMakePayments()) {
SKPaymentQueue.default().restoreCompletedTransactions()
}
答案 1 :(得分:6)
以下内容适用于Swift 2。
首先,在Itunes Connect中进行IAP
import StoreKit
class ViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
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 payments
if (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);
}
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()
}