expo-ads-admob
Version:
Provides support for the Google AdMob SDK (https://www.google.com/admob/) for mobile advertising. This module is largely based of the react-native-admob (https://github.com/sbugert/react-native-admob) module, as the documentation and questions surrounding
55 lines (45 loc) • 2.01 kB
text/typescript
import { ConfigPlugin, InfoPlist, withInfoPlist } from '@expo/config-plugins';
import { ExpoConfig } from '@expo/config-types';
export const withAdMobIOS: ConfigPlugin = (config) => {
return withInfoPlist(config, (config) => {
config.modResults = setAdMobConfig(config, config.modResults);
return config;
});
};
// NOTE(brentvatne): if the developer has installed the google ads sdk and does
// not provide an app id their app will crash. Standalone apps get around this by
// providing some default value, we will instead here assume that the user can
// do the right thing if they have installed the package. This is a slight discrepancy
// that arises in ejecting because it's possible for the package to be installed and
// not crashing in the managed workflow, then you eject and the app crashes because
// you don't have an id to fall back to.
export function getGoogleMobileAdsAppId(config: Pick<ExpoConfig, 'ios'>) {
return config.ios?.config?.googleMobileAdsAppId ?? null;
}
export function setGoogleMobileAdsAppId(
config: Pick<ExpoConfig, 'ios'>,
{ GADApplicationIdentifier, ...infoPlist }: InfoPlist
): InfoPlist {
const appId = getGoogleMobileAdsAppId(config);
if (appId === null) {
return infoPlist;
}
return {
...infoPlist,
GADApplicationIdentifier: appId,
};
}
function setAdMobConfig(config: Pick<ExpoConfig, 'ios'>, infoPlist: InfoPlist): InfoPlist {
infoPlist = setGoogleMobileAdsAppId(config, infoPlist);
return infoPlist;
}
const USER_TRACKING = 'This identifier will be used to deliver personalized ads to you.';
export const withUserTrackingPermission: ConfigPlugin<{
userTrackingPermission?: string;
} | void> = (config, { userTrackingPermission } = {}) => {
if (!config.ios) config.ios = {};
if (!config.ios.infoPlist) config.ios.infoPlist = {};
config.ios.infoPlist.NSUserTrackingUsageDescription =
userTrackingPermission || config.ios.infoPlist.NSUserTrackingUsageDescription || USER_TRACKING;
return config;
};