Adding Advertising to Your Mobile Apps

From Appmethod Topics
Jump to: navigation, search

Go Up to Creating an Android App

Go Up to Creating an iOS App


Note: This topic describes how to add support for advertising to a multi-device application on mobile platforms, regardless of the target advertising service. For service-specific documentation that covers prerequisites, testing, deployment and other aspects of advertising support on specific mobile platforms, read the following pages:

Advertising services enable you to display ads in your applications to gain some income.

FireMonkey provides a component, TBannerAd, that provides support for both the AdMob (Android) and the iAd (iOS) advertising services, and abstracts your applications from the particularities of each of these advertising services.

Here is the general procedure for adding ads to your multi-device applications on mobile platforms:

  1. Add an instance of the TBannerAd component to your application.
  2. Configure TBannerAd to load an ad from your advertising service.
  3. Handle how your user interacts with this ad.

TBannerAd also provides some service-specific features that you can use as well.

Loading an Ad

To display an ad from your advertising service in your application, you must configure TBannerAd to connect to your advertising service and load an ad from your service.

Configuring the Connection Data for Your Advertising Service

To connect to the AdMob service on Android:

1. Create your ad unit ID.
2. Copy your ad unit ID into the AdUnitID property of TBannerAd on an event such as the OnCreate event of your form:
Object Pascal: BannerAd1.AdUnitID := 'YourAdUnitID';
C++: BannerAd1->AdUnitID = "YourAdUnitID";
3. Go to Project > Options > Entitlement List, and select AdMob Service. Set its value to True.

To connect to the iAd service on iOS, TBannerAd does not need any special configuration and just works, provided that your application is configured for iAd in iTunes Connect.

Loading an Ad from Your Advertising Service

To load an ad from your advertising service, call LoadAd. The LoadAd procedure is asynchronous, and when it finishes, it triggers one of the following events:

  • OnDidFail is triggered if there is an error. OnDidFail provides a parameter, <Error>, that provides detailed information about the error that prevented the banner from loading an ad.
  • OnDidLoad is triggered if the request is successful.

You usually want to call LoadAd from a form event such as OnShow, to load an ad as soon as the form is displayed.

Handling User Interaction with Ads

When your user taps an ad, this triggers an action. This action typically consists of a full-screen version of the ad, or of opening a URL on a Web browser.

Between your user tapping the ad and the triggered action, TBannerAd triggers its OnActionCanBegin event. Handle this event to decide whether or not you want to let the action happen.

You might also want to use your handler for the OnActionCanBegin event to pause some running features of your applications, to avoid wasting processing during the ad action. Then you can define an event handler for the OnActionDidFinish event where you resume these features of your application.

Adding Support for Service-Specific Features

The following table shows features of a specific advertising service (iAd) that TBannerAd provides:

Service Features

iAd

Troubleshooting Loading an Ad from Your Advertising Service

The following table lists some problems that prevent your application from showing an ad, and potential solutions:

Cause Description Recommendation

Network error

No network connectivity to the ad server

Verify your Wi-Fi or data connection

Internal server error

An invalid response was received from the ad server

--

Invalid ad request

(Android) Wrong AdUnitID or no AdUnitID used

Use a valid unit ID (AdUnitID)

Lack of ad inventory

No ads are currently available to download from the ad server

--

Wrong ad properties Visible property of TBannerAd is set to False Set TBannerAd visible property to True
(Android) Fixed-size of the banner is too big for the screen resolution Set ad size property to a proper value

Wrong application configuration

Ad service configuration parameters of your application are wrong

See Also