Ios in app purchase always fails - ios

I'm trying to make a non consumable in app purchase. I have all my methods setup and it worked fine a couple of days ago, but now it always fails.
override func viewDidLoad() {
super.viewDidLoad()
//In-App Prurchase Transaction Observer
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
self.getProductInfo() }
In my viewDidLoad I added the transaction observer, and then handled everything else.
//In-App Prurchase
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
let products = response.products
if products.count != 0{
product = products[0] as! SKProduct
}
}
//In-App Prurchase
func getProductInfo(){
if SKPaymentQueue.canMakePayments(){
let productID:NSSet = NSSet(object: "premiunAccountProductID")
let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
request.delegate = self
request.start()
}
}
//In-App Prurchase
#IBAction func goPremiunPressed(sender: AnyObject) {
if IJReachability.isConnectedToNetwork(){//check for internet
let payment:SKPayment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment)
} else{ // no internet connection
let alertview = JSSAlertView()
alertview.show(self, title: "Sorry", text: "You need to be connected to the internet in order to buy a premium account", color: UIColor(hex: "#e74c3c"), iconImage: UIImage(named: "Cancel"))
alertview.setTextTheme(.Light)
}
}
//In-App Prurchase
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
for transaction:AnyObject in transactions{
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState{
case .Purchased:
UserPreferences.userDefaults.setBool(true, forKey: "isPremium")
UserPreferences.userDefaults.synchronize()
println("Transanction Succeded. Is premium is now \(UserPreferences.isPremium)")
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break
case .Failed:
let alertview = JSSAlertView()
alertview.show(self, title: "Sorry", text: "There was an error completing your transaction, please try again later.", color: UIColor(hex: "#e74c3c"), iconImage: UIImage(named: "Cancel"))
alertview.setTextTheme(.Light)
println("Transaction Failed")
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break
case .Restored:
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
default:
break
}
}
}
}
So every time I press the "goPremium" button, it takes a couple of seconds and then just goes to the .Failed case. I have tried to make the transaction with multiple sandbox users and they all fail.
I used a tutorial to write all this code so I don't truly know about all the stuff that some of this code is doing and or if this is the most efficient way to make an in app purchase.
Can someone tell me if there is a mistake in my code or if there is a better way to do this.
Thanks!

I'm running Xcode Version 6.4 (6E35b) and this is a simulator error. Just run the application on a real device and it works perfectly.

Related

iOS how to solve "you have already purchased this item would you like to buy it again" issue with Firebase analytics

I implemented Google admob with Firebase integration for the analytics. I want to get the user to pay in order to remove the ads. The code for this works but some issues.
I get the message "You have already purchased this item would you like to buy it again" even if the user is purchasing it for the first time.
The ads were able to be removed eventually but the message above is confusing to the user.
1) I read online that the issue can be resolved if I place this code
SKPaymentQueue.default().add(self)
before the Firebase statement in the AppDelegate
FirebaseApp.configure()
But I cannot compile the code if SKPaymentQueue.default().add(self) this is the AppDelegate class.
2) I commented out FirebaseApp.configure() after reading that Firebase also add its own TransactionObserver. But this did not work either.
3) I placed SKPaymentQueue.default().add(self) in the GameScene class in the sceneDidLoad() function but this did not solve the problem.
4) I have queue.finishTransaction(transaction) in the func paymentQueue function for each of the SKPaymentTransactionState state.
Update with code used in the sandbox app:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
btnRemoveBannerAds.isEnabled = false
SKPaymentQueue.default().add(self)
getPurchaseInfo()
}
#IBAction func removeBannerAds(_ sender: UIButton) {
let skPayment = SKPayment(product: skProduct!)
SKPaymentQueue.default().add(skPayment)
}
extension PurchaseViewController : SKPaymentTransactionObserver, SKProductsRequestDelegate {
func getPurchaseInfo() {
if SKPaymentQueue.canMakePayments() {
let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID) as! Set<String>) // dont understand this?
request.delegate = self
request.start()
} else {
print("In App purchase not enabled")
}
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
var products = response.products
if products.count == 0 {
print("Product not found")
} else {
skProduct = products[0]
print("product is \(String(describing: skProduct))")
btnRemoveBannerAds.isEnabled = true
}
let invalids = response.invalidProductIdentifiers
for product in invalids {
print("Product not found \(product)")
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.purchased:
SKPaymentQueue.default().finishTransaction(transaction)
btnRemoveBannerAds.isEnabled = false
userPref.setValue(true, forKey: "AdRemovalPurchase")
userPref.synchronize()
case SKPaymentTransactionState.restored:
SKPaymentQueue.default().finishTransaction(transaction)
btnRemoveBannerAds.isEnabled = false
userPref.setValue(true, forKey: "AdRemovalPurchase")
userPref.synchronize()
case SKPaymentTransactionState.failed:
SKPaymentQueue.default().finishTransaction(transaction)
btnRemoveBannerAds.isEnabled = false
default:
break
}
}
}
}
Please advise how to implement this. Thank you.

SKProductRequestDelegate Crashing when leaving view

I have implemented an IAP Scene OptionsPage: SKScene where users can purchase "Remove Ads" and in game currency etc. But if I go from MenuScene: SKScene to the OptionsPage: SKScene and back to the menu quickly it crashes with Thread 1: EXC_BAD_ACCESS (code=1, address=0x216d0716) and the only thing in the log down the bottom is (lldb).
I am calling SKPaymentQueue.defaultQueue().removeTransactionObserver(self) in willMoveFromView().
In my didMoveToView I call the setIAP() function (shown below).
After constantly bashing my head against a table (first time dealing with IAP) , I think that it is happening because I've set request.delegate = self and so when I enter the scene request.start() runs but because I quickly leave the scene request.delegate = self is no longer valid.
I thought removing the transaction observer would deal with this in willMoveFromView. How can I get around this problem?
Here is how I am calling the purchase in touchesBegan()
if removeAds.containsPoint(location) {
for product in list {
let prodID = product.productIdentifier
if (prodID == "removeAds") {
p = product
buyProduct()
break
}
}
}
Here is how the IAP section looks in
// MARK: In App Purchases
func setIAP() {
// Set IAPS
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID:NSSet = NSSet(objects: "removeAds, 10000coins")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
}
var list = [SKProduct]()
var p = SKProduct()
func buyProduct() {
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("product request")
let myProduct = response.products
for product in myProduct {
print("Product added: \(product.productIdentifier), \(product.localizedTitle), \(product.localizedDescription), \(product.price)")
list.append(product)
}
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("transactions restored")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case "removeAds":
defaults.setBool(true, forKey: "removeAdsPurchased")
default:
print("IAP not setup")
}
}
let alert = UIAlertController(title: "Thank You", message: "Thankyou for your purchase.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add paymnet")
for transaction:AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState {
case .Purchased, .Restored:
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID {
case "removeAds":
defaults.setBool(true, forKey: "removeAdsPurchased")
case "10000Coins":
defaults.setInteger(bank + 10000, forKey: "bankValue")
bank = defaults.integerForKey("bankValue")
default:
print("IAP not setup")
}
queue.finishTransaction(trans)
break;
case .Failed:
print("buy error")
queue.finishTransaction(trans)
break;
default:
print("default")
break;
}
}
}
func finishTransaction(trans:SKPaymentTransaction) {
print("Transaction finished")
}
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Transaction removed")
}
The way I solved this, seeing as there was no feedback, was very tedious but as follows:
Rather than have two scenes I combined the Menu and options scene so that when the options button is tapped, it runs an action to slide the menu nodes off the screen and options onto the screen. This way the request.delegate = self doesn't change therefore avoiding the crash. Then the only issue was if I launch the app and press the play button quickly to move to the game scene before the request.start finishes it would get the same crash. So I created a loading splash screen to run for 2 seconds in the menu scenes didMoveToView this way it had plenty of time to get all the data before allowing interaction.
I would love to hear of a more simple solution but with the little experience I have this is what I came up with.

In-App Purchase & Restore Button : Single Product - Non-Consumable

This issue has had me going for days.
I have a simple app, that displays banners and interstitial adverts.
I am using a single view application, have main view controller (ViewController.swift) and have set up another view controller (InAppViewController.swift) to handle a pop-up page that:
Allows the user to make an in-app purchase to remove all ads (AdBanners & InterstitialAds); or
Restore purchases.
My code is error-free when I run it.
In-app purchases are running ok, but occasionally I get a sign-in request to iTunes twice.
But my restore button and associated functionality seem to be the problem.
I have set-up numerous sandbox tester accounts to test, and a new user that has not bought the app is able to restore purchases successfully. Which should not be possible, so I definitely have done something wrong here.
Here is my code:
Main View Controller:
// ViewController.swift
import UIKit
import MessageUI
import Social
import iAd
import StoreKit
class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, ADBannerViewDelegate, ADInterstitialAdDelegate
{
let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;
override func viewDidLoad() {
product_id = "some.product.id";
super.viewDidLoad()
//Check if product is purchased
if (defaults.boolForKey("purchased")){
print("already purchased")
// Hide or show banner ads is purchased/not purchased.
// Advertising Banner:
self.canDisplayBannerAds = false
}
else if (!defaults.boolForKey("stonerPurchased")){
print("not yet purchased")
// Advertising Banner:
self.canDisplayBannerAds = true
}
This code seems to work perfectly. When the app loads, it is able to determine who has paid to remove ads and those who have not paid, and ad banners are shown appropriately.
It is in the second view controller (InAppPViewController.swift) That I am having problems.
here is my code:
Second View Controller - InAppViewController.swift:
// InAppPViewController.swift
import UIKit
import StoreKit
import iAd
class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;
#IBOutlet weak var unlockAction: UIButton!
#IBOutlet var adBannerView: ADBannerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func restorePurchases(sender: UIButton) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Transactions Restored")
let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
#IBAction func unlockAction(sender: AnyObject) {
product_id = "some.product.id";
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
//Check if product is purchased
if (defaults.boolForKey("purchased")){
}
else if (!defaults.boolForKey("stonerPurchased")){
print("false")
}
print("About to fetch the products");
// We check that we are allowed to make the purchase.
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("Fething Products");
}else{
print("can't make purchases");
}
}
func buyProduct(product: SKProduct){
print("Sending the Payment Request to Apple");
let payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
//Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse 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(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)
defaults.setBool(true , forKey: "purchased")
break;
case .Failed:
print("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .Restored:
print("Already Purchased");
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
default:
break;
}
}
}
}
}
Where am I going wrong?
Questions:
Is my above code correct?
What should I modify and why?
Apologies in advance, I am new to this wonderful world of coding ... but loving every minute of it!
SKPaymentQueue.defaultQueue().addTransactionObserver(self) should be in viewDidLoad() not in restore func
You can put SKPaymentQueue.defaultQueue().restoreCompletedTransactions() like this
override func viewWillDisappear(animated: Bool) {
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
}
login twice in sandbox is normal.
hope I helped with something.
I have amended my code for the InAppPViewController.swift file as follows:
// InAppPViewController.swift
import UIKit
import StoreKit
class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func restorePurchases(sender: UIButton) {
// Set up the observer
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
//Check if user can make payments and then proceed to restore purchase
if (SKPaymentQueue.canMakePayments()) {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
}
#IBAction func unlockAction(sender: AnyObject) {
product_id = "some.product.id";
// Adding the observer
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
//Check if product is purchased
if (defaults.boolForKey("purchased")){
print("User already purchased this")
// Hide a view or show content depends on your requirement
}
else if (!defaults.boolForKey("Purchased")){
print("User has not yet pur hased this")
}
print("About to fetch the products");
// Check if user can make payments and then proceed to make the purchase.
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("User can make purchases and will fetch products from Apple Store now");
}else{
print("User can't make purchases");
}
}
func buyProduct(product: SKProduct){
print("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) {
let 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");
}
// Allowing for all possible outcomes:
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")
let alert = UIAlertView(title: "Thank You", message: "Thank you for your purchase!", delegate: nil, cancelButtonTitle: "OK")
alert.show();
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
defaults.setBool(true , forKey: "purchased")
break;
case .Failed:
print("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .Restored:
print("Already Purchased");
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
break;
default:
break;
}
}
}
}
}
I left the ViewController.swift file as is.
Product purchases seem to work now.
But regarding Restore Purchases, I can run the code on my physical device, but cannot test the Restore Purchases function.
I am caught with previous Restore Purchases that are still unresolved and looping in the system. I am unable to clear my SKPaymentsQueue manually. Thus my code refuses to entertain anymore new Restore Purchase requests.

In-App Purchase Swift

I am using IAPHelper to implement in-app purchase, an error is occurring when I go to another view controller after pressing the purchase button. For instance, when I press the purchase button then go to another view controller the error is presented after finishing the purchase work.
class selectQuestion_ViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource{
let helper = IAPHelper(productIdentifiers: NSSet(object: "sppid") as Set<NSObject>)
func purchase(sender: AnyObject)
{
startLoading("Purchasing ..")
self.helper.requestProductsWithCompletionHandler({ (success, products) -> Void in
if success {
self.endLoading()
println("wohooooo")
var sdad = self.helper.productsDict["sppid"]
self.helper.buyProduct(sdad!)
} else {
self.endLoading()
let alert = UIAlertController(title: "Error", message: "Cannot retrieve products list right now.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
}
I recommend you do not use such modules. It would be much better for you and your understanding of IAPs that you check this tutorial and implement the code and methods described there: https://www.youtube.com/watch?v=h1gQklbrgjc
Once you have implemented that code, if something doesn't work (you may have forgotten a line somewhere), come back to SO and we will gladly help you out. Already, as quick help, here is an answer that could help you with most errors once you implement your IAP code with something else than IAP helper: My IAP isn't working. Bugs at func Paymentqueue
In fact, you can watch the video and copy paste the code of my question. My IAP code works ;) Will save you time. But watch the video to understand how to implement IAPs !
I hope this answer helped you ;)
Here is the full IAP code :
import UIKit
import StoreKit
class GameViewController: UIViewController, ADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, GKGameCenterControllerDelegate,GADBannerViewDelegate{
#IBOutlet var outRemoveAds: UIButton!
#IBOutlet var outRestorePurchases: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if NSUserDefaults.standardUserDefaults().objectForKey("val") != nil {
print("Has a value.")
banner.removeFromSuperview()
bannerGoogle.removeFromSuperview()
outRemoveAds.removeFromSuperview()
outRestorePurchases.removeFromSuperview()
removeInterFrom = 1
}
else {
print("No Value.")
}
if(SKPaymentQueue.canMakePayments()){
print("IAP is enabled, loading...")
let productID:NSSet = NSSet(objects:"IAP id")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
}
else{
print("Please enable IAPS")
}
}
//IAP Ads
#IBAction func removeAds(sender: UIButton) {
for product in list{
let prodID = product.productIdentifier
if (prodID == "IAP id"){
p = product
buyProduct()
break
}
}
}
#IBAction func restorePurchases(sender: UIButton) {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
//IAP Functions
var list = [SKProduct]()
var p = SKProduct()
func removeAds(){
banner.removeFromSuperview()
bannerGoogle.removeFromSuperview()
outRemoveAds.removeFromSuperview()
outRestorePurchases.removeFromSuperview()
let theValue = 10
NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val")
NSUserDefaults.standardUserDefaults().synchronize()
}
func buyProduct(){
print("Buy: "+p.productIdentifier)
let pay = SKPayment (product: p)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("Product Request")
let myProduct = response.products
for product in myProduct{
print("Product Added")
print(product.productIdentifier)
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)
list.append(product as SKProduct)
}
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Add Payment")
for transaction:AnyObject in transactions{
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState{
case .Purchased:
print("IAP unlocked")
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID{
case "IAP id":
print("Remove Ads")
removeAds()
default:
print("IAP not setup")
}
queue.finishTransaction(trans)
break
case .Failed:
print ("Buy error")
queue.finishTransaction(trans)
break
default:
print("default: Error")
break
}
}
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Purchases Restored")
_ = []
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction as SKPaymentTransaction
let prodID = t.payment.productIdentifier as String
switch prodID{
case "IAP id":
print("Remove Ads")
removeAds()
default:
print("IAP not setup")
}
}
}
func finishTransaction(trans:SKPaymentTransaction){
print("Finshed Transaction")
}
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Remove Transaction")
}
}
Put:
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
in viewDidLoad or viewDidAppear

In App Purchase works on all devices expect iPad Air 2 running 8.1.2

really frustrating ,
Apple reject my app with the following msg :
Thank you for your response. We found that the In App Purchase is not accessible when reviewed on iPad Air 2 running iOS 8.1.2, on Wi-Fi and cellular networks.
viewdidload :
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
if SKPaymentQueue.canMakePayments(){
let productID:NSSet = NSSet(object:"removeads")
let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID)
request.delegate = self
request.start()
}
IAP works perfectly on other devices , no idea why it dosen't work on iPad air running 8.1.2
i made some debugging from img they sent me , and the error appear to be that on this particular case , the IAP data that should return the product identifier for making the purchase , dosent retrieve . So when the press the Remove Ads button it dosent do anything , My code if it helps :
The remove ads button :
if isConnectedToNetwork()
{
btn_removeads.enabled = false
let payment:SKPayment = SKPayment(product: product)
if payment.productIdentifier != nil {
SKPaymentQueue.defaultQueue().addPayment(payment)
}
else {
btn_removeads.enabled = true
presentViewController(alert2, animated: true, completion: nil)
// This is the error that poping , the data havent received yet.
}
}
else {
presentViewController(alert, animated: true, completion: nil)
}
REST OF IAP CODE :
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!){
let products = response.products
if products.count != 0
{
product = products[0] as SKProduct
}
}
func getProductInfo(){
if SKPaymentQueue.canMakePayments(){
let productID:NSSet = NSSet(object:"removeads")
let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID)
request.delegate = self
request.start()
}
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!){
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
btn_removeads.enabled = true
NSUserDefaults.standardUserDefaults().setInteger(1, forKey: "ifads")
self.view.userInteractionEnabled = false
self.lbl_afterbuy.alpha = 1
self.lbl_afterbuy.hidden = false
NSUserDefaults.standardUserDefaults().synchronize()
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break
case .Failed:
btn_removeads.enabled = true
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break
case .Restored:
btn_removeads.enabled = true
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break
default:
break
}
}
}
}

Resources