Skip to content

Commit

Permalink
Merge pull request praves77#5 from Switch168/ios
Browse files Browse the repository at this point in the history
Add iOS support
  • Loading branch information
praves77 authored Nov 9, 2016
2 parents 27e2457 + 46a2219 commit a3da8a4
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# cordova-plugin-android-idfa
Phonegap, Cordova, Intel XDK plugin for Android to get Advertising Id Info
Phonegap, Cordova, Intel XDK plugin to get Advertising Id Info

Advertising ID (IDFA) Plugin for Android using Google Play Services

This plugin depends on [com.google.play.services](http://plugins.cordova.io/#/package/com.google.play.services).
Cordova (PhoneGap) 3.0+ Plugin to get Adertising ID (IDFA) on Adnroid device using Google Play Sercies API.
Obtains [advertising id (IDFA) info](https://support.google.com/googleplay/android-developer/answer/6048248?hl=en).

**iOS CAUTION:** This plugin is for Android Platform only.

Prerequisites:
* A Cordova 3.0+ project for Android
Expand Down
12 changes: 12 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@

<!-- ios-specific elements -->
<platform name="ios">

<config-file target="config.xml" parent="/*">
<feature name="AndroidIDFA">
<param name="ios-package" value="AndroidIDFA"/>
</feature>
</config-file>

<framework src="AdSupport.framework" weak="true" />

<header-file src="src/ios/AndroidIDFA.h" />
<source-file src="src/ios/AndroidIDFA.m" />

</platform>

</plugin>
14 changes: 14 additions & 0 deletions src/ios/AndroidIDFA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <AdSupport/AdSupport.h>
#import <Cordova/CDV.h>

@interface AndroidIDFA : CDVPlugin

- (void)getAdId:(CDVInvokedUrlCommand*)command;

- (void)getIDFV:(CDVInvokedUrlCommand*)command;

- (void)getLimitAd:(CDVInvokedUrlCommand*)command;

- (void)getAdInfo:(CDVInvokedUrlCommand*)command;

@end
125 changes: 125 additions & 0 deletions src/ios/AndroidIDFA.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#import "AndroidIDFA.h"
#import <Cordova/CDV.h>

@implementation AndroidIDFA


// Returns advertisingIdentifier as string
// Will return error on iOS < 6.0 since AdSupport framwork doesn't exist
- (void)getAdId:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult = nil;

// throw error if on iOS < 6.0
if (NSClassFromString(@"ASIdentifierManager")) {

NSString *advertiserID = [[(id)[NSClassFromString(@"ASIdentifierManager") sharedManager] advertisingIdentifier] UUIDString];

// have to handle iOS bug where 00000000-0000-0000-0000-000000000000 may be returned on iOS 6.0
if (advertiserID != nil && [advertiserID length] > 0 && ![advertiserID isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
NSDictionary *data = @{@"idfa" : advertiserID};
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
}

else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
}

else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}];
}

// Returns identifierForAdvertising as string
// Will return error on iOS < 6.0 since AdSupport framwork doesn't exist
- (void)getIDFV:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult = nil;

// throw error if on iOS < 6.0
if (NSClassFromString(@"ASIdentifierManager")) {

NSString *vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:vendorID];
}

else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}];
}

// Returns advertisingTrackingEnabled as boolean
// Will return error on iOS < 6.0 since AdSupport framwork doesn't exist
- (void)getLimitAd:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult = nil;

if (NSClassFromString(@"ASIdentifierManager")) {

BOOL enabled = [[NSClassFromString(@"ASIdentifierManager") sharedManager] isAdvertisingTrackingEnabled];
NSDictionary *data = @{@"limitAd" : [NSNumber numberWithBool:!enabled]};

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];

} else {

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];

}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}];
}

// Returns dictionary containing idfa and tracking enabled bool
// Will return error on iOS < 6.0 since AdSupport framwork doesn't exist
- (void)getAdInfo:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult = nil;

// throw error if on iOS < 6.0
if (NSClassFromString(@"ASIdentifierManager")) {

NSString *advertiserID = [[(id)[NSClassFromString(@"ASIdentifierManager") sharedManager] advertisingIdentifier] UUIDString];

NSString *vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

BOOL enabled = [[NSClassFromString(@"ASIdentifierManager") sharedManager] isAdvertisingTrackingEnabled];

// have to handle iOS bug where 00000000-0000-0000-0000-000000000000 may be returned on iOS 6.0
// set advertiserID to an empty string
if ([advertiserID isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
advertiserID = @"";
}

NSDictionary *adData = @{
@"idfa" : advertiserID,
@"idfv" : vendorID,
@"limitAd" : [NSNumber numberWithBool:!enabled]
};

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:adData];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}];
}
@end

0 comments on commit a3da8a4

Please sign in to comment.