📜 ⬆️ ⬇️

Combining Qt and AdMob

image
At one point, it took me to integrate advertising into a mobile application on Qt and I was very surprised to find that, in general, there were no solutions. No, there is of course, the V-Play AdMob plugin , but you excuse me, 160 bucks for what you can do over the weekend is too much. Subsequent searches led me to this article on Habré, which served as material for the Android implementation, and eventually turned out to be a small framework for working with AdMob advertising on IOS and Android.

The framework itself can be found on GitHub . Here, in QtAdMob is the library, and QtAdMobApp is a test application that demonstrates the capabilities of the library. Currently supported banners and Interstitial advertising (full-screen advertising). The banner interface is represented by IQtAdMobBanner , containing a set of methods for managing the banner (setting position, size, showing / hiding, etc.). Specific implementations are made in QtAdMobBanner Ios / Android / Dummy classes, respectively for IOS, Android and the latter is a stub class for unsupported platforms. Creating a platform-specific banner lay on the shoulders of the CreateQtAdMobBanner () function.
This structure also applies to interstitial advertising; it has its basic IQtAdMobInterstitial interface, a platform-specific implementation in QtAdMobInterstitial Ios / Android / Dummy classes, as well as the creation function CreateQtAdMobInterstitial () .

usage example:
#include "MainWindow.h" #include "ui_MainWindow.h" #include "QtAdMob/QtAdMobBanner.h" #include "QtAdMob/QtAdMobInterstitial.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_Switch(false) { ui->setupUi(this); m_Banner = CreateQtAdMobBanner(); m_Banner->Initialize(); m_Banner->SetUnitId("ca-app-pub-7485900711629006/8288667458"); m_Banner->SetSize(IQtAdMobBanner::Banner); m_Banner->AddTestDevice("514ED2E95AD8EECE454CC5565326160A"); m_Banner->Show(); m_Interstitial = CreateQtAdMobInterstitial(); m_Interstitial->LoadWithUnitId("ca-app-pub-7485900711629006/9462519453"); m_Interstitial->AddTestDevice("514ED2E95AD8EECE454CC5565326160A"); m_Interstitial->Show(); connect(ui->okButton, SIGNAL(clicked()), this, SLOT(OnButtonOkClicked())); } MainWindow::~MainWindow() { m_Banner->Shutdown(); delete m_Banner; delete m_Interstitial; delete ui; } void MainWindow::resizeEvent(QResizeEvent *event) { UNUSED(event); QPoint position((width() - m_Banner->GetSizeInPixels().width()) * 0.5f, 50.0f); m_Banner->SetPosition(position); } void MainWindow::OnButtonOkClicked() { bool isShowed = m_Banner->IsShow(); if (!isShowed) { m_Banner->Show(); ui->okButton->setText("Hide Banner"); } else { m_Banner->Hide(); ui->okButton->setText("Show Banner"); } } 

Initialize - the method performs the basic initialization of the banner, specifically in IOS this method makes the search for the main view and "attaches" the banner view to it. Actually, in android, this method does almost the same thing.
SetUnitId - sets the advertisement identifier that can be obtained in the company settings of your application on the AdMob page.
SetSize - as the name says, sets the size of the banner. Possible sizes are specified in the listing in the banner interface.
AddTestDevice - adds test device IDs, ads on such devices will be represented by a test banner, indicating the size of the banner, and so on. Debazhnyh things. The device ID can be pulled from the console when the application is launched on the device.
Show - well, and the method that performs the loading and display of advertising. To hide ads there is a method to hide
In the example above, in the resizeEvent method, the banner is positioned in the center.

For full-screen advertising is still easier, because there is no possibility to set the position and size, the interface is reduced to a couple of methods. As a rule, this type of advertisement is displayed when switching between levels in games, and therefore it should already be loaded at the time of the show. Therefore, the methods of loading and displaying are separated here. LoadWithUnitId - the method loads an advertisement with a specific identifier and must be called as early as possible. After the advertisement is loaded, it will not be displayed until the Show method is called

')
IOS integration:

Integration for IOS turned out to be quite simple and boils down to copying the framework into the desired project directory and adding the necessary libraries to the project. To begin, we will include the framework in the .pro project file
 include(QtAdMob/QtAdMob.pri) 

And also add all the necessary compilation flags and libraries.
 ios:QMAKE_CXXFLAGS += -fobjc-arc ios:QMAKE_LFLAGS += -ObjC ios:QT += gui_private ios:LIBS += -F $$PWD/QtAdMob/platform/ios/GoogleMobileAds -framework GoogleMobileAds \ -framework AVFoundation \ -framework AudioToolbox \ -framework CoreTelephony \ -framework MessageUI \ -framework SystemConfiguration \ -framework CoreGraphics \ -framework AdSupport \ -framework StoreKit \ -framework EventKit \ -framework EventKitUI \ -framework CoreMedia 


Android integration:

Since I have practically no experience with java and android, the implementation turned out to be somewhat "monstrous" for me. The main problem is that the flow in which the QT UI and the java UI flow work are two different flows, and all calls from C ++ to Java had to be transferred to the UI flow of Java, because the QtAdMobActivity implementation was rewarded with the same type of runOnUiThread calls.

To connect the framework, you will need the following:
- Copy the directory with the framework into the project directory and lock it into the .pro project file, as is the case with the IOS version
 include(QtAdMob/QtAdMob.pri) 

- Copy the src / and google-play-services_lib / directories from the QtAdMob / platform / android directory to the directory where the AndroidManifest.xml manifest is located

- Make all changes to your manifest as in the screenshot below.


- Link the following Qt libraries in the project file:
 android:QT += androidextras gui-private 

- If it does not exist, create a project.properties file in the directory with the manifest file and add the path to the Google Play Services library
 android.library.reference.1=./google-play-services_lib/ 

- and add a path to activations
 android:DISTFILES += <Path_to_manifest_location>/src/org/dreamdev/QtAdMob/QtAdMobActivity.java 


An example of the test application


That's all, I hope someone will benefit from this implementation. Well, if you notice inaccuracies, typographical errors or have any suggestions - write to me in the meeting room or lichku

Source: https://habr.com/ru/post/261425/


All Articles