Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: In-app purchases to remove advertisements

[ Edited ]

for qsettings there's a few area's in applicationUI.cpp & .hpp you need to copy in

 

applicationUI.cpp

 

setup your context property like this where your qml doc is created

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    qml->setContextProperty("cpp", this);

 then add these 2 functions

QString ApplicationUI::getValueFor(const QString &objectName, const QString &defaultValue)
{
    QSettings settings;

    // If no value has been saved, return the default value.
    if (settings.value(objectName).isNull()) {
        return defaultValue;
    }

    // Otherwise, return the value stored in the settings object.
    return settings.value(objectName).toString();
}

void ApplicationUI::saveValueFor(const QString &objectName, const QString &inputValue)
{
    // A new value is saved to the application settings object.
    QSettings settings;
    settings.setValue(objectName, QVariant(inputValue));

}

 applicationUI.hpp

declare the 2 functions publicly in your header

 

public:
    ApplicationUI(bb::cascades::Application *app);
    virtual ~ApplicationUI() { }

    Q_INVOKABLE
    QString getValueFor(const QString &objectName, const QString &defaultValue);

    Q_INVOKABLE
    void saveValueFor(const QString &objectName, const QString &inputValue);

 

.QML

from then on out its as simple as saying 

 

cpp.saveValueFor("someName","someValue")

 

cpp.getValueFor("someName","")  (if it is a integer or float use .parseInt or .parseFloat(cpp.getValueFor()))

 

 

 

Developer
Tobster_
Posts: 268
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: In-app purchases to remove advertisements

Okay, thanks :smileyvery-happy: - I have added all of that in and now my ActionItem looks like this:

 

 ActionItem {
              title: "Remove Ads"
              imageSource: "asset:///IMG/ic_clear_list.png";
              
              onTriggered: {
                  paymentControl.id = "47326888"
                  paymentControl.sku = "ShoppingForeBayAds"
                  paymentControl.name = "Remove Ads"
                  paymentControl.metadata = "Purchasing this will remove the adverts!"
                  paymentControl.getPrice(paymentControl.id, paymentControl.sku)
                  paymentControl.purchase(paymentControl.id, paymentControl.sku, paymentControl.name, paymentControl.metadata)

              }
              id: root
              
              property int removeAdds
              
              onRemoveAddsChanged: {
                  if (removeAdds == 100){
                      //Hide advertisements
                  }
              }
              
              attachedObjects: [
                  
                  //! [3]
                  // The custom payment element for holding item data
                  // and used for method invocation and receiving events as a
                  // result of those invocations
                  PaymentServiceControl {
                      id: paymentControl
                      property string id
                      property string sku
                      property string name
                      property string metadata
                      onPriceResponseSuccess: {
                      
                      }
                      
                      onPurchaseResponseSuccess: {
                          cpp.saveValueFor("removeAdds","100");
                          root.removeAdds = 100;
                      }
                      onExistingPurchasesResponseSuccess: {
                      
                      }
                      onSubscriptionTermsResponseSuccess: {
                      
                      }
                      onCancelSubscriptionResponseSuccess: {
                      
                      }
                      onCheckStatusResponseSuccess: {
                      
                      }
                      onInfoResponseError: {
                      }
                  }
              //! [3]
              ]  
            },

 What else do I need to do for it to be fully functioning (make the ads be removed)?

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: In-app purchases to remove advertisements

where it says //Hide Advertisements is where you should hide your advertisements :smileywink:... 

 

Assuming your advertisements were in a container called myAdvertisements

 

onRemoveAddsChanged: {
   if (removeAdds == 100) {
      myAdvertisements.visible = false
   }
}

 

Also, you should put all the code from onPurchaseResponseSuccess in onExistingPurchasesResponseSuccess as well.

 

****If you ever use more than 1 add-on, you will need to do some further optimization.****

I haven't used more than 1 add-on per app yet so i'm not entirly sure what all would need to be done though.

 

 

Developer
Tobster_
Posts: 268
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: In-app purchases to remove advertisements

[ Edited ]

Okay thanks :smileyhappy: - what do you mean by add-on?

 

Here it is:

 

ActionItem {
                title: "Remove Ads"
                imageSource: "asset:///IMG/ic_clear_list.png"

                onTriggered: {
                    paymentControl.id = "47326888"
                    paymentControl.sku = "ShoppingForeBayAds"
                    paymentControl.name = "Remove Ads"
                    paymentControl.metadata = "Purchasing this will remove the adverts!"
                    paymentControl.getPrice(paymentControl.id, paymentControl.sku)
                    paymentControl.purchase(paymentControl.id, paymentControl.sku, paymentControl.name, paymentControl.metadata)

                }
                id: root

                property int removeAdds

                onRemoveAddsChanged: {
                    if (removeAdds == 100) {
                        myAdvertisements.visible = false
                    }
                }

                attachedObjects: [

                    //! [3]
                    // The custom payment element for holding item data
                    // and used for method invocation and receiving events as a
                    // result of those invocations
                    PaymentServiceControl {
                        id: paymentControl
                        property string id
                        property string sku
                        property string name
                        property string metadata
                        onPriceResponseSuccess: {

                        }

                        onPurchaseResponseSuccess: {
                            cpp.saveValueFor("removeAdds", "100");
                            root.removeAdds = 100;
                        }
                        onExistingPurchasesResponseSuccess: {
                            cpp.saveValueFor("removeAdds", "100");
                            root.removeAdds = 100;
                        }
                        onSubscriptionTermsResponseSuccess: {

                        }
                        onCancelSubscriptionResponseSuccess: {

                        }
                        onCheckStatusResponseSuccess: {

                        }
                        onInfoResponseError: {
                        }
                    }
                //! [3]
                ]
            },

 

Container {
                    id: myAdvertisements
// BlackBerry Ad service info 
}

 Is everything set up now? :smileyhappy:

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: In-app purchases to remove advertisements

Everything appears to be in order =)  So that should work for you, but be sure to test it thoroughly in your sandbox before release, better safe than sorry :smileywink:

 

add-on = virtual good.  (I.E you want to have a vitual good for removing add-ons, this should work for just that but if you wanted to also have another vitual good for something else for this same app you'll need to do a little more leg-work to differentiate between the two so that when 1 is purchased both aren't unlocked.)

Developer
Tobster_
Posts: 268
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: In-app purchases to remove advertisements

[ Edited ]

Thanks for your help! 

 

Today, I swiped down from the top and clicked remove ads. It comes up with the payment screen for the in-app purchase I have added to BlackBerry World. 

 

I put it back into test mode and clicked remove ads and it removed the whole webview I was using; played around with the code a bit and it's sorted now :smileyvery-happy: 

 

Also, before you mentioned about a 'restore purchases' option - how do I include that and where would you recommend that I add it as I don't have a dedicated shop page, I just have the main page and an about page. 

 

Thanks again :smileyhappy:

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: In-app purchases to remove advertisements

if you installed the app from anywhere other than BlackBerry world you wont be charged because it cannot contact the payment system.  If you instealled the app from your sandbox, you won't be charged because your BBID is associated with the sandbox so you can test purchases for your apps

Developer
Tobster_
Posts: 268
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: In-app purchases to remove advertisements

I had put it into production mode - which I only just noticed. Played around with the code and put it back into test mode and it works perfectly.

Also, before you mentioned about a 'restore purchases' option - how do I include that and where would you recommend that I add it as I don't have a dedicated shop page, I just have the main page and an about page.

Thanks again :smileyvery-happy:
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: In-app purchases to remove advertisements

in test mode you have no way of knowing if it actually works.  Even if it was setup wrong it will still 'work' in test mode this is only to demonstrate the messages it returns.  If you can't turn add's off from production mode in your sandbox (after uploading to bb world and downloading through sandbox) then you won't be able to turn them off once it's released.

 

place it in the about page, anywhere is better than nowhere

also place the call in your main.qml oncreationcompleted to auto check at launch 

 

paymentControl.getExisting (true);
Developer
Tobster_
Posts: 268
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: In-app purchases to remove advertisements

[ Edited ]

I've just added this to my main.qml file,

onCreationCompleted: {
paymentControl.getExisting(true);
}

Do I just add that same piece of code but as a button in the about page?

 

Also, when I remove the ads in test mode - they get removed. But if I close the app and then re-open it the ads are back there again - will it be the same as this when it's in BlackBerry World?