Get in touch
or send us a question?
CONTACT

[Unity] Cập nhật admob để tương thích với iOS 14

Vào tháng Sáu 2020, Apple đã đưa ra bản update cho iOS 14 yêu cầu các ứng dụng phải xin cấp quyền để truy cập vào Apple identifier for advertisers(viết tắt là IDFA, tạm dịch ra tiếng Việt là định danh quảng cáo) thông qua 1 bảng thông báo gọi là App Tracking Transparency(ATT) framework. Bài post này sẽ hướng dẫn cách cập nhật Google Admob SDK và các bước để thỏa mãn yêu cầu từ phía Apple.

Các bước thực hiện

Đầu tiên, chúng ta phải download Google Admob SDK cho Unity từ đường link sau: https://github.com/googleads/googleads-mobile-unity/releases/tag/v5.4.0. Lưu ý, chúng ta cần phải chọn đúng phiên bản sdk 5.4.0 vì từ phiên bản này mới hỗ trợ tích hợp SKAdNetwork vào Xcode project. Sau đó, import sdk này vào Unity.

Tiếp theo, sau khi đã import xong sdk, ta cần import thêm 1 package nữa để show popup yêu cầu người dùng cho phép ứng dụng truy cập vào IDFA hay không. Package này đang trong trạng thái preview nên khi bật Package Manager ta cần cho phép hiển thị nhưng preview packages.

Sau khi đã cài xong package iOS 14 Advertisement Support thì ta sẽ cần viết code để show popup yêu cầu cấp quyền. Code này nên gọi ở ngay lúc ban đầu khi ứng dụng vừa khởi động. Đoạn code như sau:

#if UNITY_IOS
    if(ATTrackingStatusBinding.GetAuthorizationTrackingStatus() == 
    ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED)
    {
        ATTrackingStatusBinding.RequestAuthorizationTracking();
    }
#endif

Việc cuối cùng còn lại là viết Editor build script để add quyền NSUserTrackingUsageDescription vào file plist để ta ko cần nhập giá trị này bằng tay mỗi khi build:

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

public class AppTrackingTransparencyPostBuild
{
    /// <summary>
    /// Description for IDFA request notification 
    /// [sets NSUserTrackingUsageDescription]
    /// </summary>
    const string TrackingDescription =
        "This identifier will be used to deliver personalized ads to you. ";

    [PostProcessBuild(0)]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToXcode)
    {
        if (buildTarget == BuildTarget.iOS)
        {
            AddPListValues(pathToXcode);
        }
    }

    static void AddPListValues(string pathToXcode)
    {
        // Get Plist from Xcode project 
        string plistPath = pathToXcode + "/Info.plist";

        // Read in Plist 
        PlistDocument plistObj = new PlistDocument();
        plistObj.ReadFromString(File.ReadAllText(plistPath));

        // set values from the root obj
        PlistElementDict plistRoot = plistObj.root;

        // Set value in plist
        plistRoot.SetString("NSUserTrackingUsageDescription", TrackingDescription);

        // save
        File.WriteAllText(plistPath, plistObj.WriteToString());
    }

}

Lưu file này vào trong thư mục editor của project là đã hoàn thành việc cập nhật IDFA cho ứng dụng. Nếu bạn muốn thay đổi nội dung text trong popup cấp quyền thì có thể thay đổi nội dung của biến TrackingDescription để phù hợp cho ứng dụng của bạn.

Sau khi hoàn thành các bước thì ta sẽ build ứng dụng và test việc cập nhật IDFA. Nếu khi mở ứng dụng lần đầu mà bạn thấy popup hiện ra thì xin chúc mừng bạn đã cập nhật IDFA cho iOS 14 thành công.

Mọi người có thể download sourcecode tại đây: https://drive.google.com/file/d/1LYY1x9g7CT40kPRAfQJ-_YPQrvhpGCPt/view?usp=sharing. Thank you for reading 🙂