facebook twitter hatena line email

Unity/GoogleMobileAds/SKAdNetwork

提供: 初心者エンジニアの簡易メモ
2024年9月3日 (火) 03:22時点におけるAdmin (トーク | 投稿記録)による版 (ダイアログが出ない場合)

移動: 案内検索

SKAdNetworkとは

ios14のidfa対応するために、使えば良いじゃんとadmobに推奨されてるライブラリ

admobからios14対応時に推奨されてるお知らせ

To help you prepare for these changes and improve iOS monetization rates, Google recommends that you take the following actions as soon as possible:
1. 	Install the latest Google Mobile Ads SDK for iOS (for AdMob or
Ad Manager). Versions 7.64 or later include critical features for iOS 14 compatibility.
2. 	Configure SKAdNetwork with Google’s network identifier. Advertisers will be using Apple’s SKAdNetwork API to measure the value they get from your app, so doing so is the best way to get credit for your app’s ads performance. Learn more about configuring SKAdNetwork for mobile and video.
3. 	Review Apple’s ATT consent prompt and determine if implementing is right for your app. Conducting a limited user experiment (via server configuration) can help you understand what implementation works best for your users. Google’s Funding Choices user messaging is an option you may want to evaluate for creating and managing the ATT prompt & explainer messaging. Learn more about configuring Funding Choices IDFA messages.

unityのadmobライブラリ

https://github.com/googleads/googleads-mobile-unity/releases

Google Mobile Ads Unity Plugin v5.4.0

であれば、

Google Mobile Ads iOS SDK 7.68.0

となっていて、7.64より上なので、Google Mobile Ads Unity Plugin 5.4.0を入れておけば良い。

unityのadmobライブラリでSKAdNetwork対応方法

iosをビルドすると Info.plistに以下が出力される

    <key>SKAdNetworkItems</key>
    <array>
      <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>cstr6suwn9.skadnetwork</string>
      </dict>
    </array>

SKAdNetworkIdentifierの値は、以下ページにある値と同じか、確認しておく。

https://developers.google.com/admob/ios/ios14?hl=ja

App Tracking Transparency で許可をリクエストする

Info.plistに以下を追加

<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>

自動反映する場合

Assets/Editor/XcodeAttPostProcess.cs

#if UNITY_IOS
using System.IO;
using UnityEditor.iOS.Xcode;
using UnityEditor;
using UnityEditor.Callbacks;

namespace iOS.Editor
{
    public class XcodeAttPostProcess
    {
        private const string ATT_FRAMEWORK = "AppTrackingTransparency.framework";

        [PostProcessBuild]
        public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath)
        {
            if (buildTarget != BuildTarget.iOS)
            {
                return;
            }
            var pbxPath = PBXProject.GetPBXProjectPath(buildPath);
            var pbx = new PBXProject();
            pbx.ReadFromFile(pbxPath);
            string target = GetUnityMainTargetGuidWithCompatible(pbx);
            pbx.AddFrameworkToProject(target, ATT_FRAMEWORK, true);
            pbx.WriteToFile(pbxPath);

            var path  = buildPath + "/Info.plist";
            var plist = new PlistDocument();
            plist.ReadFromFile(path);
            var root = plist.root;
            root.SetString("NSUserTrackingUsageDescription", "本アプリは、広告効果の測定や分析などのためにIDFA(広告識別子)を利用します。");
            plist.WriteToFile(path);
        }

        private static string GetUnityMainTargetGuidWithCompatible(PBXProject pbx)
        {
#if UNITY_2019_3_OR_NEWER
            return pbx.GetUnityFrameworkTargetGuid();
#else
            return pbx.TargetGuidByName(PBXProject.GetUnityTargetName());
#endif
        }
    }
}
#endif

上記を設置して、iosをビルドすれば良い。

unityからのリクエスト方法

Assets/Plugins/iOS/AttObjC.mm

#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>

#ifdef __cplusplus
extern "C" {
#endif

int Sge_Att_getTrackingAuthorizationStatus()
{
    if (@available(iOS 14, *)) {
        return (int)ATTrackingManager.trackingAuthorizationStatus;
    } else {
        return -1;
    }
}

typedef void (*Callback)(int status);
void Sge_Att_requestTrackingAuthorization(Callback callback)
{
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            if (callback != nil) {
                if (status == ATTrackingManagerAuthorizationStatusNotDetermined) {
                    NSLog(@"ATT status = NotDetermined(未設定) -> 端末設定でOK、アプリで未選択");
                } else if (status == ATTrackingManagerAuthorizationStatusRestricted) {
                    NSLog(@"ATT status = Restricted(制限あり)");
                } else if (status == ATTrackingManagerAuthorizationStatusDenied) {
                    NSLog(@"ATT status = Denied(不許可) -> 端末設定orアプリで不許可");
                } else if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                    NSLog(@"ATT status = Authorized(許可)");
                } else {
                    NSLog(@"ATT status = Other(その他)");
                }
                callback((int)status);
            }
        }];
    } else {
        NSLog(@"ATT status = iOS14 未満(非対応)");
        callback(-1);
    }
}
#ifdef __cplusplus
}
#endif

Assets/Scripts/AttObjC.cs

using System;                           // for Action
using System.Runtime.InteropServices;   // for DllImport
using System.Threading;                 // for SynchronizationContext
using UnityEngine;                      // for Application

public class AttObjC
{
#if UNITY_IOS
    private const string DLL_NAME = "__Internal";
    [DllImport(DLL_NAME)]
    private static extern int Sge_Att_getTrackingAuthorizationStatus();
    public static int GetTrackingAuthorizationStatus()
    {
        if (Application.isEditor) {
            return -1;
        }
        return Sge_Att_getTrackingAuthorizationStatus();
    }
    [DllImport(DLL_NAME)]
    private static extern void Sge_Att_requestTrackingAuthorization(OnCompleteCallback callback);

    private delegate void OnCompleteCallback(int status);
    private static SynchronizationContext _context;
    private static Action _onComplete;

    public static void RequestTrackingAuthorization()
    {
        if (Application.isEditor) {
            return;
        }
#if UNITY_IOS
        _context = SynchronizationContext.Current;
        Sge_Att_requestTrackingAuthorization(OnRequestComplete);
#endif
    }
    [AOT.MonoPInvokeCallback(typeof(OnCompleteCallback))]
    private static void OnRequestComplete(int status)
    {
        if (_onComplete != null) {
            _context.Post(_ => {
                _onComplete = null;
            }, null);
        }
    }
#endif
}

Assets/Scripts/AttStartup.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttStartup
{
    public static void Exec()
    {
#if UNITY_IPHONE
        int status = AttObjC.GetTrackingAuthorizationStatus();
        if (status == 0) {
            AttObjC.RequestTrackingAuthorization();
        }
#endif
    }
    void CallbackFunction(int status)
    {
        Debug.Log("ATT状況:" + status);
    }
}

MainScene.cs(起動時のStartなどから実行)

void Start() {
#if UNITY_IPHONE
        AttStartup.Exec(); // att
#endif
}

参考:https://creator.game.cyberagent.co.jp/?p=7382

Use of undeclared identifier 'ATTrackingManager'エラーが出る

以下2箇所のコードで"Use of undeclared identifier 'ATTrackingManager'"がエラーが出る場合

return (int)ATTrackingManager.trackingAuthorizationStatus;
[ATTrackingManager

mmファイルにimport追加すればよい。

#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>

attの起動ダイアログが出ない場合

ExecAtt();

のタイミングを少し遅らせてみるとよい。

iOSで説明文を多言語化する方法

アプリ名の変更と同じようなやり方でできる。

xcode側で、InfoPlist.stringsを作成し、Localizeをenglishで設定し、以下を追加する

InfoPlist.strings(English) 物理ファイル:en.lproj/InfoPlist.strings

NSUserTrackingUsageDescription = "Allowing tracking makes it easier for you to see relevant ads. Access to personal information by this I don't.";

InfoPlist.strings(Japanese) 物理ファイル:Japanese.lproj/InfoPlist.strings

NSUserTrackingUsageDescription = "追跡(トラッキング)を許可すると、関連性が高い広告が表示されやすくなります。これによる個人情報へのアクセスはありません。";

参考:https://i-app-tec.com/ios/localization.html

日本語の方も入れないと、すべて英語になってたので、日本語も入れる。

説明ダイアログを挟んでも大丈夫っぽい

https://twitter.com/Kumanbow/status/1364787562951233536

https://twitter.com/masaseat/status/1357387154658271233

参考

https://qiita.com/bosteri_bon/items/36233fdbc478d7e74cc9

http://blog.be-style.jpn.com/article/188329627.html