如何更新 rnfirebase admob 以支持 iOS 14 AppTrackingTransparency

How to Update rnfirebase admob to support iOS 14 AppTrackingTransparency

Google AdMob 已宣布 preparations for iOS 14+ recently. There will be a requirement for AppTrackingTransparency permission. However, there is no roadmap for react-native developers that use AdMob through rnfirebase。我的 react-native 项目目前依赖于 react-native-firebase 版本 v6.7.1:

"dependencies": {
    "@react-native-firebase/admob": "^6.7.1",
    "@react-native-firebase/app": "^6.7.1",
    ...

我只是在我的应用程序中使用 InterstitialAdBannerAd

import { InterstitialAd, TestIds, AdEventType, 
    BannerAd, BannerAdSize} from '@react-native-firebase/admob';

升级我的项目以支持 iOS 14+ 的路线图应该是什么?

要进行此更新,应遵循多个步骤:

更新 react-native-firebase 依赖项

首先,您应该将 package.json 中的 react-native-firebase 依赖项更新到最新版本,因为有一个预请求“Google 移动广告SDK 7.64.0 或更高版本":

"@react-native-firebase/admob": "^11.3.2",
"@react-native-firebase/app": "^11.3.2",

然后是 运行 npm installyarn install 命令。

添加 react-native-permissions 依赖

其次,您应该向 package.json 添加一个 react-native-permissions 依赖项,以便稍后您将使用此依赖项来检查和请求 AppTrackingTransparency 权限:

"react-native-permissions": "^3.0.2",

然后是 运行 npm installyarn install 命令。

更新Info.plist

然后,您应该将在 Admob's Prepare for iOS 14+ document 上解释的新要求的 NSUserTrackingUsageDescription 键值和 SKAdNetworkItems 信息添加到您的 Info.plist 文件中:

<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>
<key>SKAdNetworkItems</key>
<array>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>cstr6suwn9.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>4fzdc2evr5.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>2fnua5tdw4.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>ydx93a7ass.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>5a6flpkh64.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>p78axxw29g.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>v72qych5uu.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>c6k4g5qg8m.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>s39g8k73mm.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>3qy4746246.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>3sh42y64q3.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>f38h382jlk.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>hs6bdukanm.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>prcb7njmu6.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>wzmmz9fp6w.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>yclnxrl5pm.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>4468km3ulz.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>t38b2kh725.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>7ug5zh24hu.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>9rd848q2bz.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>n6fk4nfna4.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>kbd757ywx3.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>9t245vhmpl.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>2u9pt9hc89.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>8s468mfl3y.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>av6w8kgt66.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>klf5c3l5u5.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>ppxm28t8ap.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>424m5254lk.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>uw77j35x4d.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>e5fvkxwrpn.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>zq492l623r.skadnetwork</string>
    </dict>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>3qcr597p9d.skadnetwork</string>
    </dict>
</array>

更新 Podfile

然后,您应该通过添加以下行来更新您的 PodFile

permissions_path = '../node_modules/react-native-permissions/ios'

pod 'Permission-AppTrackingTransparency', :path => "#{permissions_path}/AppTrackingTransparency"

然后 运行 pod installpod install --repo-update 在项目的 ios 目录上执行命令(您可以删除旧的 Pods 目录和 Podfile.lock 在此操作之前的文件)。

在你的 react-native 代码中添加 AppTrackingTransparency 权限

最后,您应该在 AdMob 广告请求之前的某处添加检查和请求 APP_TRACKING_TRANSPARENCY 权限到您的本机代码:

import {check, request, PERMISSIONS, RESULTS} from 'react-native-permissions';

//......

check(PERMISSIONS.IOS.APP_TRACKING_TRANSPARENCY)
  .then((result) => {
    switch (result) {
      case RESULTS.UNAVAILABLE:
        console.log('This feature is not available (on this device / in this context)');
        break;
      case RESULTS.DENIED:
        console.log('The permission has not been requested / is denied but requestable');
        request(PERMISSIONS.IOS.APP_TRACKING_TRANSPARENCY).then((reqResult) => {
            switch (reqResult) {
                case RESULTS.GRANTED:
                    console.log('The permission request is granted');
                    // now you can make ad requests
                    break;
                case RESULTS.BLOCKED:
                    console.log('The permission request is denied and not requestable anymore');
                    break;
            }
        });
        break;
      case RESULTS.LIMITED:
        console.log('The permission is limited: some actions are possible');
        break;
      case RESULTS.GRANTED:
        console.log('The permission is granted');
        // now you can make ad requests
        break;
      case RESULTS.BLOCKED:
        console.log('The permission is denied and not requestable anymore');
        break;
    }
  })
  .catch((error) => {
    // …
  });

如果从 checkrequest 方法调用中获得 RESULTS.GRANTED 结果,则可以发出广告请求。

最后说明:在 运行ning 之前,不要忘记 clean 你的 Xcode 项目。[=39] =]