I’m writing in app purchases app which can’t get responded and not sure why iTunesConnect are all set too and I’m sure my product id are same
import Foundation
import StoreKit
class InAppPurchases: NSObject, SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print(response.products)
}
private override init() { }
static let shared = InAppPurchases()
func getProducts() {
let products: Set = ["com.google.sites.xxx.app.product"]
let request = SKProductsRequest(productIdentifiers: products)
request.delegate = self
request.start()
}
}
Calling get product in viewdidload and get empty set
Any help would be appreciated
Related
I'm trying to do fetch ProductIDs. But response always be null.
I want to show what I do.
I Create App IDs in developer Account.BundleID equal to AppID
I've installed the project App Connect. Selected my Project BundleID
Created ProductID's
Switch on the In App Purchase in Xcode
My Code:
Created ProductID's:
`enum IAPProduct: String {
case Consumble = "com.xxxx.xxx.Consumable"
case nonConsumble = "com.xxxx.xxx.TekSeferAl"
}`
My fetch code:
class IAPService: NSObject {
static let shared = IAPService()
private override init() {}
//MARK:- Properties
//MARK:- Private
func getProducts() {
SKPaymentQueue.canMakePayments()
let products: Set = [IAPProduct.Consumble.rawValue,
IAPProduct.nonConsumble.rawValue]
let request = SKProductsRequest(productIdentifiers: products)
request.cancel()
request.delegate = self
request.start()
}
}
Delegate:
extension IAPService: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
response.invalidProductIdentifiers.forEach { product in
print("Invalid: \(product)")
}
response.products.forEach { product in
print("Valid: \(product)")
}
func request(_ request: SKRequest, didFailWithError error: Error) {
print("Error for request: \(error.localizedDescription)")
}
}
}
Output
Where am I making a mistake?
The problem is that it seems your products are in the state of "Missing Metadata" in App Store Connect - they need to be in "Ready to Submit" for testing.
You're probably either:
Missing a photo (you can use a blank image for testing)
Haven't signed the the Paid Applications Agreement with Apple
Here's a good blog post that covers setting up products in more detail: Configuring In-app Products is Hard
I have followed a walkthrough to include a non-consumable in-app purchase. Unfortunately, nothing is happening at all after the user clicks the button for the purchase. I am not getting any prompt to log in to iTunes or to accept the payment. Is there something I am missing here?
I have tried multiple walkthroughs and they all seem to have the similar code, I have followed the steps through the apple website, however I am unsure as to whether I have to do a full app submission before I can test the in-app purchases through a sandbox.
IAPService.swift
import Foundation
import StoreKit
import UIKit
class IAPService: NSObject {
private override init() {}
static let shared = IAPService()
var products = [SKProduct]()
let paymentQueue = SKPaymentQueue.default()
func getProducts() {
let products: Set = [IAPProduct.nonConsumable.rawValue]
let request = SKProductsRequest(productIdentifiers: products)
request.delegate = self
request.start()
paymentQueue.add(self)
}
func purchase(product: IAPProduct) {
guard let productToPurchase = products.filter({
$0.productIdentifier == product.rawValue }).first else { return }
let payment = SKPayment(product: productToPurchase)
paymentQueue.add(payment)
}
func restorePurchases() {
print("restore purchases")
paymentQueue.restoreCompletedTransactions()
}
}
extension IAPService: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive
response: SKProductsResponse) {
products = response.products
for product in response.products {
print(product.localizedTitle)
}
}
}
extension IAPService: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions
transactions: [SKPaymentTransaction]) {
for transaction in transactions {
print(transaction.transactionState.status(),
transaction.payment.productIdentifier)
switch transaction.transactionState {
case .purchasing: break
default: queue.finishTransaction(transaction)
}
}
}
}
extension SKPaymentTransactionState {
func status() -> String {
switch self {
case .deferred: return "deferred"
case .failed: return "failed"
case .purchased: return "purchased"
case .purchasing: return "purchasing"
case .restored: return "restored"
}
}
}
In my products file
IAP.Products.swift
import Foundation
enum IAPProduct: String {
case nonConsumable = "Quizly"
}
In my mainVC
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
IAPService.shared.getProducts()
print("IAP == \(IAPService.shared.products)") // Why is this an empty array?
}
#objc func pressToGetPremium(_ sender : UIButton) {
IAPService.shared.purchase(product: .nonConsumable)
print("IAP ===== \(IAPService.shared.products)")
}
When the user clicks the button I was hoping that a pop up would come up first making the user have to sign in to their apple account and then another pop up would ask if they wanted to accept the non-consumable product ($1.99) etc. But I am not getting a pop up at all.
I am receiving this back from didReceive products......
response SKProductsResponse 0x00000002811cba10
baseNSObject#0 NSObject
isa Class 0x2811cba10 0x00000002811cba10
_internal SKProductsResponseInternal * 0x28139c0e0 0x000000028139c0e0
NSObject NSObject
_invalidIdentifiers __NSSingleObjectArrayI * 1 element 0x00000002811cb930
[0] __NSCFString * "Quizly" 0x000000028139c620
NSMutableString NSMutableString
_products __NSArray0 * 0 elements 0x00000002811c0050
NSArray NSArray
NSObject NSObject
isa Class __NSArray0 0x000001a25a605811
As per our comment discussion - The error produced from the didReceive products method indicates your bundleIdentifiers are not matching the ones on the appStore.
Why is paymentQueue(:shouldAddStorePayment::) not being called?
I am sure I've done everything I need to do.
I declared my own class that supports the SKPaymentTransactionObserver protocol:
import UIKit
import StoreKit
import AudioToolbox.AudioServices
class UTIPaymentTransactionObserver: NSObject, SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
print("!!! shouldAddStorePayment")
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
return false
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("!!! updatedTransactions")
for transaction in transactions {
print("!!! transaction=", transaction)
switch transaction.transactionState {
// Call the appropriate custom method for the transaction state.
case SKPaymentTransactionState.purchasing:
showTransactionAsInProgress(transaction, deferred: false)
case SKPaymentTransactionState.deferred:
showTransactionAsInProgress(transaction, deferred: true)
case SKPaymentTransactionState.failed:
failedTransaction(transaction)
case SKPaymentTransactionState.purchased:
completeTransaction(transaction)
case SKPaymentTransactionState.restored:
restoreTransaction(transaction)
}
}
}
func showTransactionAsInProgress(_ transaction: SKPaymentTransaction, deferred: Bool) {
print("!!! showTransactionAsInProgress")
}
func failedTransaction(_ transaction: SKPaymentTransaction) {
print("!!! failedTransaction")
SKPaymentQueue.default().finishTransaction(transaction)
}
func completeTransaction(_ transaction: SKPaymentTransaction) {
print("!!! completeTransaction")
SKPaymentQueue.default().finishTransaction(transaction)
}
func restoreTransaction(_ transaction: SKPaymentTransaction) {
print("!!! restoreTransaction")
}
}
I added code to vibrate the device when paymentQueue(:shouldAddStorePayment::) is called to indicate that the method is actually called.
I declared an instance of the observer class globally:
internal let paymentTransactionObserver = UTIPaymentTransactionObserver()
I made sure I added the observer in AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
SKPaymentQueue.default().add(paymentTransactionObserver)
return true
}
The print statement in the paymentQueue(:shouldAddStorePayment::) method never prints and the device never vibrates. It doesn't look like that method is called.
The paymentQueue(_:updatedTransactions:) method is called. The print statement in that method executed.
In this code I returned false for the paymentQueue(:shouldAddStorePayment::) method, but it doesn't make a difference. The process goes through just as if I returned a true. The product had already been purchased before, so it goes through and lets the user/tester purchase it again.
Any help would be appreciated.
Here is code extension for the view controller that retrieves the product from App Store and presents my user interface that allows the user to purchase the product:
I call validateProductIdentifiers() to start the process of selling the product to the user.
// MARK: - SKProductsRequestDelegate
extension CloudViewController: SKProductsRequestDelegate {
func validateProductIdentifiers() {
let url = Bundle.main.url(forResource: "Purchase", withExtension: "plist")!
let nsArrayProductIdentifiers: NSArray = NSArray(contentsOf: url)!
let productIdentifiers = nsArrayProductIdentifiers as! [String]
print(productIdentifiers)
let setProductIdentifers: Set = Set(productIdentifiers)
let productsRequest = SKProductsRequest(productIdentifiers: setProductIdentifers)
self.productsRequest = productsRequest
productsRequest.delegate = self
productsRequest.start()
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("!!! didReceive")
self.products = response.products
let alertMessage = "Would you like to purchase?"
let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .actionSheet)
let actionYes = UIAlertAction(title: "Yes", style: .default) {
action in
// Purchase
let product: SKProduct = response.products.first!
let payment: SKMutablePayment = SKMutablePayment(product: product)
SKPaymentQueue.default().add(payment)
}
let actionNo = UIAlertAction(title: "No", style: .cancel, handler: nil)
alert.addAction(actionYes)
alert.addAction(actionNo)
alert.popoverPresentationController?.barButtonItem = barButtonItemEnableDropbox
present(alert, animated: true, completion: nil)
}
}
As per the documentation for this method
This delegate method is called when the user starts an in-app purchase in the App Store, and the transaction continues in your app. Specifically, if your app is already installed, the method is called.
This occurs when a user redeems a promo code for an in-app purchase in the App Store app or purchases a promoted in-App purchase in the App Store app.
It is not called when the purchase is initiated in your app, since you already have control over whether purchasing should be permitted when the user is in your app.
I assigned an object to SKProductRequest.delegate and got a run time error: EXC_BAD_ACCESS.
public class MyDelegate : NSObject, SKProductsRequestDelegate {
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
// ...
}
}
func sendProdRequest() {
let request = SKProductsRequest(productIdentifiers: ProductType.all)
request.delegate = ProductsRequestDelegate(completion)
request.start()
}
If I set a breakpoint at request.start() and print the request.delegate in console, the code works without the error. But, if I just break without printing the request.delegate, the error occurs again.
Anyone knows why this happens?
SKProductsRequests delegate will not retain your ProductsRequestDelegate so you have to do so yourself. That is why if you add a break-point at request.start() you can still see the delegate being set, but when method sendProdRequest() is finished, nothing is holding on to the ProductsRequestDelegate and it gets deallocated.
Try adding a var productRequestDelegate to the object where sendProdRequest() is defined and then do:
let request = SKProductsRequest(productIdentifiers: ProductType.all)
productRequestDelegate = ProductsRequestDelegate(completion)
request.delegate = productRequestDelegate
request.start()
I have impelmented in app purchase in my app.I have followed this tutorial
Now in my app when i try load products then it does not load.It shows error as can't connect to iTunes store.I have craeted one product on iTunes.
Below is the code
/
*
* Copyright (c) 2016 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import StoreKit
public typealias ProductIdentifier = String
public typealias ProductsRequestCompletionHandler = (_ success: Bool, _ products: [SKProduct]?) -> ()
open class IAPHelper : NSObject {
static let IAPHelperPurchaseNotification = "IAPHelperPurchaseNotification"
fileprivate let productIdentifiers: Set<ProductIdentifier>
fileprivate var purchasedProductIdentifiers = Set<ProductIdentifier>()
fileprivate var productsRequest: SKProductsRequest?
fileprivate var productsRequestCompletionHandler: ProductsRequestCompletionHandler?
public init(productIds: Set<ProductIdentifier>) {
productIdentifiers = productIds
for productIdentifier in productIds {
let purchased = UserDefaults.standard.bool(forKey: productIdentifier)
if purchased {
purchasedProductIdentifiers.insert(productIdentifier)
print("Previously purchased: \(productIdentifier)")
} else {
print("Not purchased: \(productIdentifier)")
}
}
super.init()
SKPaymentQueue.default().add(self)
}
}
// MARK: - StoreKit API
extension IAPHelper {
public func requestProducts(completionHandler: #escaping ProductsRequestCompletionHandler) {
productsRequest?.cancel()
productsRequestCompletionHandler = completionHandler
productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest!.delegate = self
productsRequest!.start()
}
public func buyProduct(_ product: SKProduct) {
print("Buying \(product.productIdentifier)...")
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment)
}
public func isProductPurchased(_ productIdentifier: ProductIdentifier) -> Bool {
return purchasedProductIdentifiers.contains(productIdentifier)
}
public class func canMakePayments() -> Bool {
return SKPaymentQueue.canMakePayments()
}
public func restorePurchases() {
SKPaymentQueue.default().restoreCompletedTransactions()
}
}
// MARK: - SKProductsRequestDelegate
extension IAPHelper: SKProductsRequestDelegate {
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
let products = response.products
print("Loaded list of products...")
productsRequestCompletionHandler?(true, products)
clearRequestAndHandler()
for p in products {
print("Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)")
}
}
public func request(_ request: SKRequest, didFailWithError error: Error) {
print("Failed to load list of products.")
print("Error: \(error.localizedDescription)")
productsRequestCompletionHandler?(false, nil)
clearRequestAndHandler()
}
private func clearRequestAndHandler() {
productsRequest = nil
productsRequestCompletionHandler = nil
}
}
// MARK: - SKPaymentTransactionObserver
extension IAPHelper: SKPaymentTransactionObserver {
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case .purchased:
complete(transaction: transaction)
break
case .failed:
fail(transaction: transaction)
break
case .restored:
restore(transaction: transaction)
break
case .deferred:
break
case .purchasing:
break
}
}
}
private func complete(transaction: SKPaymentTransaction) {
print("complete...")
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func restore(transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restore... \(productIdentifier)")
deliverPurchaseNotificationFor(identifier: productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func fail(transaction: SKPaymentTransaction) {
print("fail...")
if let transactionError = transaction.error as? NSError {
if transactionError.code != SKError.paymentCancelled.rawValue {
print("Transaction Error: \(transaction.error?.localizedDescription)")
}
}
SKPaymentQueue.default().finishTransaction(transaction)
}
private func deliverPurchaseNotificationFor(identifier: String?) {
guard let identifier = identifier else { return }
purchasedProductIdentifiers.insert(identifier)
UserDefaults.standard.set(true, forKey: identifier)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object: identifier)
}
}
Please guide what is the issue ?
EDIT:
Did fail with error is getting called."Failed to load list of products."
Make sure you have signed out of any production iTunes accounts on the device.
I was getting this error on my test phone which was logged in with my actual iTunes account. You cannot test apps using your production iTunes account, hence the error. I just wish Apple provided a better error so as to avoid this guesswork...
The solution is to create a test user account if you don't have one. Test in-app purchase using that account.
Note: If in case, you are not getting products, please make sure that you are using product identifier while sending request exactly that matches with the product identifier on itunes.
Another silly reason, we encounter this error when we run on simulator. Check whether
Your app is running in the Simulator, which does not support In-App
Purchase
After logging out of the iTunes & iCloud account i reset my iOS device & In app purchase worked.Although i don't know why.
You don't have to be logged in to iOS device for getting products from in app purchase.For buying the products then we must be logged into the device.