0

This example code from SwiftyStoreKit works, but I would like to convert it to a method that returns priceString.

SwiftyStoreKit.retrieveProductsInfo([productID]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                print("Product: \(product.localizedDescription), price: \(priceString)")
            }
            
            else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            }
            else {
                print("Error: \(String(describing: result.error))")
            }
        }
    }

When I try this :

func getProductsTest() -> String {
        
        SwiftyStoreKit.retrieveProductsInfo([productID]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                print("Product: \(product.localizedDescription), price: \(priceString)")
            }
            
            else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            }
            else {
                print("Error: \(String(describing: result.error))")
            }
        }
    }

I get the error "Cannot convert return expression of type 'InAppRequest' to return type 'String' Any idea how I can return the value of priceString?

Thanks

jat
  • 183
  • 3
  • 14
  • did you find a solution for this? I am looking for exactly the same thing – Fabian H. Feb 26 '21 at 16:41
  • yes, I had to return the result via a delegate. I wrote about it here : https://levelup.gitconnected.com/beginner-ios-dev-in-app-purchase-iap-made-simple-with-swiftystorekit-3add60e9065d – jat Feb 27 '21 at 17:26
  • Great thanks! I found that one already, but it helps that you actually solved it. the only thing I am still working on now is that I am completely working in SwiftUI, so I am trying to return it in to a regular textfield, but it keeps returning empty. Do you have a workaround for that perhaps? Right now I am converting to a IBoutlet, and the next step would be taking the text from there and put it in a Text() in SwiftUI. Would love to hear your thoughts – Fabian H. Mar 01 '21 at 18:41
  • 1
    Great article by the way! – Fabian H. Mar 01 '21 at 18:49
  • hey thanks. Sorry not great at SwiftUI, just learning. – jat Mar 05 '21 at 17:22

0 Answers0