iOS In-app purchase - unable to unlock the content as the function is in another view controller












0















I have set up an In-App purchase using a 'IAPHelper' class. I can retrieve the correct information from the App Store and display it in one VC. Once a user selects the required purchase item in that VC, the details are displayed on the next View Controller called 'Review'. This is where the actual purchase takes place. I have a 'buyProduct' function that is working fine in this ReviewVC, and sends the product payment to the paymentQueue of the SKPaymentTransactionObserver of the IAPHelper class.
I need to unlock the content via a Pop Up in the 'Review VC' which should appear once the purchase status is shown as 'purchased' in the SKPaymentTransactionObserver, so I have tried many ways to include this function as part of the paymentQueue status for 'purchased' but the app always crashes after successful payment, once it reaches the function to show the PopUp.
Here's some code - I won't include all the IAPHelper code, just the relevant pieces:



open class IAPHelper: NSObject { ......
var review: ReviewVC! ...... }

extension IAPHelper: SKPaymentTransactionObserver {

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for trans in transactions {
print("func IAPHelper = (trans.transactionState.status(), trans.payment.productIdentifier)")
switch trans.transactionState {
case .purchasing: break
case .purchased: complete(transaction: trans)
break
case .failed : fail(transaction: trans)
case .restored : restore(transaction: trans)
case .deferred : break
// default: queue.finishTransaction(trans)
}
}
}

func complete(transaction: SKPaymentTransaction) {
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
review.showSuccessPopUp() //***ERROR IS HERE***Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10426ac70)*****

}


This is the code in the ReviewVC file:-



class ReviewVC: UIViewController {
@IBAction func buyTestButton(_ sender: Any) {
print("Buy button pressed - products = (products?.localizedTitle ?? "No Product")")
buyProduct()
}

func buyProduct() {
let payment = SKPayment(product: products!)
SKPaymentQueue.default().add(payment as SKPayment)

}


//SUCCESSFUL PURCHASE:-
func showSuccessPopUp() {
UIView.animate(withDuration: 0.4) {
self.hideView.isHidden = false
}
UIView.animate(withDuration: 0.8) {
self.successPopUp.isHidden = false
}
}


Been stuck on this for days so any help would be much appreciated!!










share|improve this question

























  • Are you saying it is crashing on showSuccessPopUp()? If that is the case, which line is crashing and what is the error? Also, I see self.hideView and self.successPopUp but I don't see the initialization for either. Without being provided the problem, it is difficult to provide a solution.

    – impression7vx
    Nov 15 '18 at 19:15











  • @impression7vx yes it crashes when the function is called within the 'complete' function. I have shown this in the code I included, where it says 'error is here'. The self.hideview and self.successPopUp are IB outlets in the Review VC. As you can see, I am trying to call this showSuccessPopup function in the IAPHelper class, once the purchase has been made successfully. Let me know if you need further info - and thanks for any help.

    – PlanB
    Nov 15 '18 at 20:06











  • Why are you calling it twice? Once in paymentQueue() and complete()? And on top of that what is there error? Is that all error prints out or does it have a lot of mumble jumble? Preferred if you print out the whole mumble jumble

    – impression7vx
    Nov 15 '18 at 20:11











  • Sorry - typo calling it twice. That’s all the error says

    – PlanB
    Nov 15 '18 at 20:21











  • How do you initialize review inside IAPHelper class?

    – impression7vx
    Nov 15 '18 at 20:25


















0















I have set up an In-App purchase using a 'IAPHelper' class. I can retrieve the correct information from the App Store and display it in one VC. Once a user selects the required purchase item in that VC, the details are displayed on the next View Controller called 'Review'. This is where the actual purchase takes place. I have a 'buyProduct' function that is working fine in this ReviewVC, and sends the product payment to the paymentQueue of the SKPaymentTransactionObserver of the IAPHelper class.
I need to unlock the content via a Pop Up in the 'Review VC' which should appear once the purchase status is shown as 'purchased' in the SKPaymentTransactionObserver, so I have tried many ways to include this function as part of the paymentQueue status for 'purchased' but the app always crashes after successful payment, once it reaches the function to show the PopUp.
Here's some code - I won't include all the IAPHelper code, just the relevant pieces:



open class IAPHelper: NSObject { ......
var review: ReviewVC! ...... }

extension IAPHelper: SKPaymentTransactionObserver {

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for trans in transactions {
print("func IAPHelper = (trans.transactionState.status(), trans.payment.productIdentifier)")
switch trans.transactionState {
case .purchasing: break
case .purchased: complete(transaction: trans)
break
case .failed : fail(transaction: trans)
case .restored : restore(transaction: trans)
case .deferred : break
// default: queue.finishTransaction(trans)
}
}
}

func complete(transaction: SKPaymentTransaction) {
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
review.showSuccessPopUp() //***ERROR IS HERE***Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10426ac70)*****

}


This is the code in the ReviewVC file:-



class ReviewVC: UIViewController {
@IBAction func buyTestButton(_ sender: Any) {
print("Buy button pressed - products = (products?.localizedTitle ?? "No Product")")
buyProduct()
}

func buyProduct() {
let payment = SKPayment(product: products!)
SKPaymentQueue.default().add(payment as SKPayment)

}


//SUCCESSFUL PURCHASE:-
func showSuccessPopUp() {
UIView.animate(withDuration: 0.4) {
self.hideView.isHidden = false
}
UIView.animate(withDuration: 0.8) {
self.successPopUp.isHidden = false
}
}


Been stuck on this for days so any help would be much appreciated!!










share|improve this question

























  • Are you saying it is crashing on showSuccessPopUp()? If that is the case, which line is crashing and what is the error? Also, I see self.hideView and self.successPopUp but I don't see the initialization for either. Without being provided the problem, it is difficult to provide a solution.

    – impression7vx
    Nov 15 '18 at 19:15











  • @impression7vx yes it crashes when the function is called within the 'complete' function. I have shown this in the code I included, where it says 'error is here'. The self.hideview and self.successPopUp are IB outlets in the Review VC. As you can see, I am trying to call this showSuccessPopup function in the IAPHelper class, once the purchase has been made successfully. Let me know if you need further info - and thanks for any help.

    – PlanB
    Nov 15 '18 at 20:06











  • Why are you calling it twice? Once in paymentQueue() and complete()? And on top of that what is there error? Is that all error prints out or does it have a lot of mumble jumble? Preferred if you print out the whole mumble jumble

    – impression7vx
    Nov 15 '18 at 20:11











  • Sorry - typo calling it twice. That’s all the error says

    – PlanB
    Nov 15 '18 at 20:21











  • How do you initialize review inside IAPHelper class?

    – impression7vx
    Nov 15 '18 at 20:25
















0












0








0








I have set up an In-App purchase using a 'IAPHelper' class. I can retrieve the correct information from the App Store and display it in one VC. Once a user selects the required purchase item in that VC, the details are displayed on the next View Controller called 'Review'. This is where the actual purchase takes place. I have a 'buyProduct' function that is working fine in this ReviewVC, and sends the product payment to the paymentQueue of the SKPaymentTransactionObserver of the IAPHelper class.
I need to unlock the content via a Pop Up in the 'Review VC' which should appear once the purchase status is shown as 'purchased' in the SKPaymentTransactionObserver, so I have tried many ways to include this function as part of the paymentQueue status for 'purchased' but the app always crashes after successful payment, once it reaches the function to show the PopUp.
Here's some code - I won't include all the IAPHelper code, just the relevant pieces:



open class IAPHelper: NSObject { ......
var review: ReviewVC! ...... }

extension IAPHelper: SKPaymentTransactionObserver {

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for trans in transactions {
print("func IAPHelper = (trans.transactionState.status(), trans.payment.productIdentifier)")
switch trans.transactionState {
case .purchasing: break
case .purchased: complete(transaction: trans)
break
case .failed : fail(transaction: trans)
case .restored : restore(transaction: trans)
case .deferred : break
// default: queue.finishTransaction(trans)
}
}
}

func complete(transaction: SKPaymentTransaction) {
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
review.showSuccessPopUp() //***ERROR IS HERE***Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10426ac70)*****

}


This is the code in the ReviewVC file:-



class ReviewVC: UIViewController {
@IBAction func buyTestButton(_ sender: Any) {
print("Buy button pressed - products = (products?.localizedTitle ?? "No Product")")
buyProduct()
}

func buyProduct() {
let payment = SKPayment(product: products!)
SKPaymentQueue.default().add(payment as SKPayment)

}


//SUCCESSFUL PURCHASE:-
func showSuccessPopUp() {
UIView.animate(withDuration: 0.4) {
self.hideView.isHidden = false
}
UIView.animate(withDuration: 0.8) {
self.successPopUp.isHidden = false
}
}


Been stuck on this for days so any help would be much appreciated!!










share|improve this question
















I have set up an In-App purchase using a 'IAPHelper' class. I can retrieve the correct information from the App Store and display it in one VC. Once a user selects the required purchase item in that VC, the details are displayed on the next View Controller called 'Review'. This is where the actual purchase takes place. I have a 'buyProduct' function that is working fine in this ReviewVC, and sends the product payment to the paymentQueue of the SKPaymentTransactionObserver of the IAPHelper class.
I need to unlock the content via a Pop Up in the 'Review VC' which should appear once the purchase status is shown as 'purchased' in the SKPaymentTransactionObserver, so I have tried many ways to include this function as part of the paymentQueue status for 'purchased' but the app always crashes after successful payment, once it reaches the function to show the PopUp.
Here's some code - I won't include all the IAPHelper code, just the relevant pieces:



open class IAPHelper: NSObject { ......
var review: ReviewVC! ...... }

extension IAPHelper: SKPaymentTransactionObserver {

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for trans in transactions {
print("func IAPHelper = (trans.transactionState.status(), trans.payment.productIdentifier)")
switch trans.transactionState {
case .purchasing: break
case .purchased: complete(transaction: trans)
break
case .failed : fail(transaction: trans)
case .restored : restore(transaction: trans)
case .deferred : break
// default: queue.finishTransaction(trans)
}
}
}

func complete(transaction: SKPaymentTransaction) {
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
review.showSuccessPopUp() //***ERROR IS HERE***Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10426ac70)*****

}


This is the code in the ReviewVC file:-



class ReviewVC: UIViewController {
@IBAction func buyTestButton(_ sender: Any) {
print("Buy button pressed - products = (products?.localizedTitle ?? "No Product")")
buyProduct()
}

func buyProduct() {
let payment = SKPayment(product: products!)
SKPaymentQueue.default().add(payment as SKPayment)

}


//SUCCESSFUL PURCHASE:-
func showSuccessPopUp() {
UIView.animate(withDuration: 0.4) {
self.hideView.isHidden = false
}
UIView.animate(withDuration: 0.8) {
self.successPopUp.isHidden = false
}
}


Been stuck on this for days so any help would be much appreciated!!







ios swift in-app-purchase in-app






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:24







PlanB

















asked Nov 15 '18 at 18:37









PlanBPlanB

226




226













  • Are you saying it is crashing on showSuccessPopUp()? If that is the case, which line is crashing and what is the error? Also, I see self.hideView and self.successPopUp but I don't see the initialization for either. Without being provided the problem, it is difficult to provide a solution.

    – impression7vx
    Nov 15 '18 at 19:15











  • @impression7vx yes it crashes when the function is called within the 'complete' function. I have shown this in the code I included, where it says 'error is here'. The self.hideview and self.successPopUp are IB outlets in the Review VC. As you can see, I am trying to call this showSuccessPopup function in the IAPHelper class, once the purchase has been made successfully. Let me know if you need further info - and thanks for any help.

    – PlanB
    Nov 15 '18 at 20:06











  • Why are you calling it twice? Once in paymentQueue() and complete()? And on top of that what is there error? Is that all error prints out or does it have a lot of mumble jumble? Preferred if you print out the whole mumble jumble

    – impression7vx
    Nov 15 '18 at 20:11











  • Sorry - typo calling it twice. That’s all the error says

    – PlanB
    Nov 15 '18 at 20:21











  • How do you initialize review inside IAPHelper class?

    – impression7vx
    Nov 15 '18 at 20:25





















  • Are you saying it is crashing on showSuccessPopUp()? If that is the case, which line is crashing and what is the error? Also, I see self.hideView and self.successPopUp but I don't see the initialization for either. Without being provided the problem, it is difficult to provide a solution.

    – impression7vx
    Nov 15 '18 at 19:15











  • @impression7vx yes it crashes when the function is called within the 'complete' function. I have shown this in the code I included, where it says 'error is here'. The self.hideview and self.successPopUp are IB outlets in the Review VC. As you can see, I am trying to call this showSuccessPopup function in the IAPHelper class, once the purchase has been made successfully. Let me know if you need further info - and thanks for any help.

    – PlanB
    Nov 15 '18 at 20:06











  • Why are you calling it twice? Once in paymentQueue() and complete()? And on top of that what is there error? Is that all error prints out or does it have a lot of mumble jumble? Preferred if you print out the whole mumble jumble

    – impression7vx
    Nov 15 '18 at 20:11











  • Sorry - typo calling it twice. That’s all the error says

    – PlanB
    Nov 15 '18 at 20:21











  • How do you initialize review inside IAPHelper class?

    – impression7vx
    Nov 15 '18 at 20:25



















Are you saying it is crashing on showSuccessPopUp()? If that is the case, which line is crashing and what is the error? Also, I see self.hideView and self.successPopUp but I don't see the initialization for either. Without being provided the problem, it is difficult to provide a solution.

– impression7vx
Nov 15 '18 at 19:15





Are you saying it is crashing on showSuccessPopUp()? If that is the case, which line is crashing and what is the error? Also, I see self.hideView and self.successPopUp but I don't see the initialization for either. Without being provided the problem, it is difficult to provide a solution.

– impression7vx
Nov 15 '18 at 19:15













@impression7vx yes it crashes when the function is called within the 'complete' function. I have shown this in the code I included, where it says 'error is here'. The self.hideview and self.successPopUp are IB outlets in the Review VC. As you can see, I am trying to call this showSuccessPopup function in the IAPHelper class, once the purchase has been made successfully. Let me know if you need further info - and thanks for any help.

– PlanB
Nov 15 '18 at 20:06





@impression7vx yes it crashes when the function is called within the 'complete' function. I have shown this in the code I included, where it says 'error is here'. The self.hideview and self.successPopUp are IB outlets in the Review VC. As you can see, I am trying to call this showSuccessPopup function in the IAPHelper class, once the purchase has been made successfully. Let me know if you need further info - and thanks for any help.

– PlanB
Nov 15 '18 at 20:06













Why are you calling it twice? Once in paymentQueue() and complete()? And on top of that what is there error? Is that all error prints out or does it have a lot of mumble jumble? Preferred if you print out the whole mumble jumble

– impression7vx
Nov 15 '18 at 20:11





Why are you calling it twice? Once in paymentQueue() and complete()? And on top of that what is there error? Is that all error prints out or does it have a lot of mumble jumble? Preferred if you print out the whole mumble jumble

– impression7vx
Nov 15 '18 at 20:11













Sorry - typo calling it twice. That’s all the error says

– PlanB
Nov 15 '18 at 20:21





Sorry - typo calling it twice. That’s all the error says

– PlanB
Nov 15 '18 at 20:21













How do you initialize review inside IAPHelper class?

– impression7vx
Nov 15 '18 at 20:25







How do you initialize review inside IAPHelper class?

– impression7vx
Nov 15 '18 at 20:25














1 Answer
1






active

oldest

votes


















1














The problem is that review is nil.



You are declaring it as a variable var review: ReviewVC! however, you are never initializing it.



Initialization is something like review = Something.



Not sure where your code is calling IAP() but wherever it is, I would do something like:



var iap = IAP()
iap.review = self //if you are in the review class.


Hard to tell as I am not sure where you declare your IAP object.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325923%2fios-in-app-purchase-unable-to-unlock-the-content-as-the-function-is-in-another%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The problem is that review is nil.



    You are declaring it as a variable var review: ReviewVC! however, you are never initializing it.



    Initialization is something like review = Something.



    Not sure where your code is calling IAP() but wherever it is, I would do something like:



    var iap = IAP()
    iap.review = self //if you are in the review class.


    Hard to tell as I am not sure where you declare your IAP object.






    share|improve this answer




























      1














      The problem is that review is nil.



      You are declaring it as a variable var review: ReviewVC! however, you are never initializing it.



      Initialization is something like review = Something.



      Not sure where your code is calling IAP() but wherever it is, I would do something like:



      var iap = IAP()
      iap.review = self //if you are in the review class.


      Hard to tell as I am not sure where you declare your IAP object.






      share|improve this answer


























        1












        1








        1







        The problem is that review is nil.



        You are declaring it as a variable var review: ReviewVC! however, you are never initializing it.



        Initialization is something like review = Something.



        Not sure where your code is calling IAP() but wherever it is, I would do something like:



        var iap = IAP()
        iap.review = self //if you are in the review class.


        Hard to tell as I am not sure where you declare your IAP object.






        share|improve this answer













        The problem is that review is nil.



        You are declaring it as a variable var review: ReviewVC! however, you are never initializing it.



        Initialization is something like review = Something.



        Not sure where your code is calling IAP() but wherever it is, I would do something like:



        var iap = IAP()
        iap.review = self //if you are in the review class.


        Hard to tell as I am not sure where you declare your IAP object.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 21:32









        impression7vximpression7vx

        53911038




        53911038
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325923%2fios-in-app-purchase-unable-to-unlock-the-content-as-the-function-is-in-another%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine