diff --git a/README.md b/README.md index 996d0b9..945e7cc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # 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 @@ -7,7 +7,6 @@ This plugin depends on [com.google.play.services](http://plugins.cordova.io/#/pa 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 diff --git a/plugin.xml b/plugin.xml index 227c412..d13ab8e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -63,6 +63,18 @@ + + + + + + + + + + + + diff --git a/src/ios/AndroidIDFA.h b/src/ios/AndroidIDFA.h new file mode 100755 index 0000000..faa4939 --- /dev/null +++ b/src/ios/AndroidIDFA.h @@ -0,0 +1,14 @@ +#import +#import + +@interface AndroidIDFA : CDVPlugin + +- (void)getAdId:(CDVInvokedUrlCommand*)command; + +- (void)getIDFV:(CDVInvokedUrlCommand*)command; + +- (void)getLimitAdFlag:(CDVInvokedUrlCommand*)command; + +- (void)getAdInfo:(CDVInvokedUrlCommand*)command; + +@end \ No newline at end of file diff --git a/src/ios/AndroidIDFA.m b/src/ios/AndroidIDFA.m new file mode 100755 index 0000000..213b48b --- /dev/null +++ b/src/ios/AndroidIDFA.m @@ -0,0 +1,126 @@ +#import "AndroidIDFA.h" +#import + +@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"]) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:advertiserID]; + } + + 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)getLimitAdFlag:(CDVInvokedUrlCommand*)command +{ + [self.commandDelegate runInBackground:^{ + CDVPluginResult* pluginResult = nil; + + if (NSClassFromString(@"ASIdentifierManager")) { + + BOOL enabled = [[NSClassFromString(@"ASIdentifierManager") sharedManager] isAdvertisingTrackingEnabled]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:!enabled]; + + 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 \ No newline at end of file