Code Monkey home page Code Monkey logo

cordova-plugin-idfa's Introduction

Cordova plugin for getting Advertising ID (IDFA or AAID)

NPM version NPM downloads NPM total downloads PayPal donate Twitter

Index

Supported Platforms

  • iOS
  • Android

Installation

$ cordova plugin add cordova-plugin-idfa

Use variable ANDROID_PLAY_ADID_VERSION to override dependency version on Android:

$ cordova plugin add cordova-plugin-idfa --variable ANDROID_PLAY_ADID_VERSION='16.+'

API

The API is available on the cordova.plugins.idfa global object.

getInfo()

Returns a Promise<object> with the following fields:

  • trackingLimited: boolean - Whether usage of advertising id is allowed by user.
  • idfa: string (iOS only) - Identifier for advertisers.
  • trackingPermission (iOS 14+ only): number Tracking permission status, available on iOS 14+ devices.
  • aaid: string (Android only) - Android advertising ID.

requestPermission()

(iOS only) A one-time request to authorize or deny access to app-related data that can be used for tracking the user or the device. See Apple's API docs for more info on the dialog presented to the user. Available only for iOS 14+ devices.

Returns a Promise<number>. On devices with iOS < 14 the method will return a rejected promise.

Note: You should make sure to set the NSUserTrackingUsageDescription key in your app's Information Property List file, otherwise your app will crash when you use this API. You can do it with the following code in your Cordova project's config.xml:

<platform name="ios">
    <edit-config target="NSUserTrackingUsageDescription" file="*-Info.plist" mode="merge">
        <string>My tracking usage description</string>
    </edit-config>
</platform>

Tracking Permission Values

The tracking permission values are numbers returned by getInfo() and requestPermission(). The possible values are stored in constants on the plugin object. See the example on how to use them.

For the meaning of the values see the tracking transparency API docs:

Constant Value Description
TRACKING_PERMISSION_NOT_DETERMINED 0 User has not yet received an authorization request to authorize access to IDFA
TRACKING_PERMISSION_RESTRICTED 1 User restricted the value returned if authorization to access IDFA
TRACKING_PERMISSION_DENIED 2 The value returned if the user denies authorization to access IDFA
TRACKING_PERMISSION_AUTHORIZED 3 The value returned if the user authorizes access to IDFA

Example

const idfaPlugin = cordova.plugins.idfa;

idfaPlugin.getInfo()
    .then(info => {
        if (!info.trackingLimited) {
            return info.idfa || info.aaid;
        } else if (info.trackingPermission === idfaPlugin.TRACKING_PERMISSION_NOT_DETERMINED) {
            return idfaPlugin.requestPermission().then(result => {
                if (result === idfaPlugin.TRACKING_PERMISSION_AUTHORIZED) {
                    return idfaPlugin.getInfo().then(info => {
                        return info.idfa || info.aaid;
                    });
                }
            });
        }
    })
    .then(idfaOrAaid => {
        if (idfaOrAaid) {
            console.log(idfaOrAaid);
        }
    });

cordova-plugin-idfa's People

Contributors

chemerisuk avatar pierogit avatar bendingbender avatar mayainle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.