Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated iAd Attribution API and fix Xcode v16 builds #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# react-native-apple-ads-attribution

Fetches apple attribution data via iAd and AdServices APIs
Fetches apple attribution data via AdServices APIs

## Requirements
- iAd Framework
- AdServices Framework

## Installation
Expand All @@ -17,8 +16,6 @@ npm install @hexigames/react-native-apple-ads-attribution
```js
import AppleAdsAttribution from "@hexigames/react-native-apple-ads-attribution";

const attributionData = await AppleAdsAttribution.getAttributionData();
const iAdAttributionData = await AppleAdsAttribution.getiAdAttributionData();
const adServicesAttributionToken = await AppleAdsAttribution.getAdServicesAttributionToken();
const adServicesAttributionData = await AppleAdsAttribution.getAdServicesAttributionData();
```
Expand All @@ -31,10 +28,6 @@ Gets install attribution data first trying to use the AdServices API (iOS 14.3+)
If it fails to retrieve data it will fallback to iAd API.
Throws error if everything fails

#### getiAdAttributionData()
Gets install attribution data using iAd API https://developer.apple.com/documentation/iad/setting_up_apple_search_ads_attribution/
Throws error if data couldn't be retrieved (e.g. if user rejected permission for app tracking)

##### Example
```javascript
try {
Expand Down
38 changes: 0 additions & 38 deletions index.js

This file was deleted.

33 changes: 33 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NativeModules, Platform } from "react-native";

const { AppleAdsAttribution: RNAppleAdsAttribution } = NativeModules;

class AppleAdsAttribution {
getAdServicesAttributionToken(): null | Promise<string> {
if (Platform.OS !== "ios") {
return null;
}
return RNAppleAdsAttribution.getAdServicesAttributionToken();
}

getAdServicesAttributionData(): null | Promise<{
attribution: boolean;
orgId: number;
campaignId: number;
conversionType: string;
clickDate?: string; // "2020-04-08T17:17Z",
adGroupId: number;
countryOrRegion: string;
keywordId: number;
adId: number;
}> {
if (Platform.OS !== "ios") {
return null;
}
return RNAppleAdsAttribution.getAdServicesAttributionData();
}
}

const AppleAdsAttributionInstance = new AppleAdsAttribution();

export default AppleAdsAttributionInstance;
74 changes: 0 additions & 74 deletions ios/AppleAdsAttribution.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import "AppleAdsAttribution.h"
#import <React/RCTLog.h>
#import <AdServices/AdServices.h>
#import <iAd/iAd.h>

@implementation AppleAdsAttribution
static NSString *const RNAAAErrorDomain = @"RNAAAErrorDomain";
Expand Down Expand Up @@ -158,79 +157,6 @@ + (void) getAdServicesAttributionDataWithCompletionHandler: (void (^)(NSDictiona
}
}

/**
* Gets attribution data from the old iAd API.
* completionHandler will return nil with an error if attribution data couldn't be retrieved. Reasons for failing may be that the user disabled tracking or that the iOS version is < 10.
*/
+ (void) getiAdAttributionDataWithCompletionHandler: (void (^)(NSDictionary * _Nullable data, NSError * _Nullable error))completionHandler {

if ([[ADClient sharedClient] respondsToSelector:@selector(requestAttributionDetailsWithBlock:)]) {
[[ADClient sharedClient] requestAttributionDetailsWithBlock: ^(NSDictionary *attributionDetails, NSError *error) {
if (error == nil) {
completionHandler(attributionDetails, nil);
} else {
NSLog(@"getiAdAttributionDataWithCompletionHandler error getting data %@", error);
completionHandler(nil, error);
}
}];
} else {
// requestAttributionDetailsWithBlock is not available probably < iOS 10
NSMutableDictionary* details = [NSMutableDictionary dictionary];
[details setValue:@"iAd ADClient not available" forKey:NSLocalizedDescriptionKey];
NSError* error = [NSError errorWithDomain:RNAAAErrorDomain code:100 userInfo:details];
completionHandler(nil, error);
}
}

/**
* Tries to get attribution data first using the AdServices API. If it fails it fallbacks to the old iAd API.
* Rejected with error if both fails
*/
RCT_EXPORT_METHOD(getAttributionData:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {

[AppleAdsAttribution getAdServicesAttributionDataWithCompletionHandler:^(NSDictionary * _Nullable attributionData, NSError * _Nullable adServicesError) {
if (attributionData != nil) {
resolve(attributionData);
} else {
// Fallback to old iAd client API
[AppleAdsAttribution getiAdAttributionDataWithCompletionHandler:^(NSDictionary * _Nullable data, NSError * _Nullable iAdError) {
if (data != nil) {
resolve(data);
} else {
// Reject with both error messages
NSString *combinedErrorMessage = [NSString stringWithFormat:@"Ad services error: %@. \niAD error: %@", adServicesError != NULL ? adServicesError.localizedDescription : @"no error message", iAdError != NULL ? iAdError.localizedDescription : @"no error message"];

[AppleAdsAttribution rejectPromiseWithUserInfo:reject
userInfo:[@{
@"code" : @"unknown",
@"message" : combinedErrorMessage
} mutableCopy]];
}

}];
}
}];
}

/**
* Tries to get attribution data using the old iAd API.
* Rejected with error if it failed to get data
* */
RCT_EXPORT_METHOD(getiAdAttributionData: (RCTPromiseResolveBlock) resolve rejecter: (RCTPromiseRejectBlock) reject) {

[AppleAdsAttribution getiAdAttributionDataWithCompletionHandler:^(NSDictionary * _Nullable data, NSError * _Nullable error) {
if(data != nil) {
resolve(data);
} else {
[AppleAdsAttribution rejectPromiseWithNSError:reject error:error];
}

}];
}

/**
* Tries to generate an attribution token that then can be used for calls to Apples AdServices API.
* Rejected with error if token couldn't be generated.
Expand Down