今日Code打。iOS14 廣告追蹤選擇,Yes or No

Photo by Jen Theodore on Unsplash

最近剛好遇到專案提出這個需求,趁著記憶猶新,來做個紀錄。

在2020年WWDC,Apple提出了即將在iOS14加入反廣告追中功能,重視隱私的Apple認為在未經用戶同意的情況下,追蹤用戶的數位軌跡,是侵犯隱私的。

在提出後很快就有許多廣告投放廠商和開發商發出反對聲浪,不過行銷團隊和廣告商總會找出辦法來的,而且Apple軟體工程資深副總裁 Craig Federighi 接受外媒採訪時表示

應用程式開發者要是不遵循這項政策,蘋果就會直接從 App Store 刪除此 App

(看來還是得乖乖遵守遊戲規則啊)

  1. 引用廣告追蹤相關SDK
import AppTrackingTransparency
import AdSupport

2. 找個適當的時間,開啟廣告追蹤確認彈窗,彈窗顯示一次後便不會再出現,直到移除App重新安裝。(目前的使用情境:用戶登入後顯示)

if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization {
// To do something when request ADtracking Alert end to display
}
}

3. Add privacy description in Info.plist(記得一定要加,不然會出現Error)

// Add Info.plist<key>NSUserTrackingUsageDescription</key><string>您將看到更適合您的廣告</string>

Error Message

AdTrackPermit[23166:391501] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSUserTrackingUsageDescription key with a string value explaining to the user how the app uses this data.

更新 2022/06/08

若ATT彈窗無法顯示,有可能是因為同時要求推播權限彈窗,當推播權限彈窗消失時,一起把ATT彈窗關閉。造成無法顯示

解法1: 延遲呼叫ATT彈窗

解法2: call applicationDidBecomeActive(_:) in AppDelegate

但在iOS 15仍有機會不會顯示

解法3: 在didFinishLaunchingWithOptions監聽UIApplication.didBecomeActiveNotification

NotificationCenter.default.addObserver(self, selector: #selector(showTrackingAction), name: UIApplication.didBecomeActiveNotification, object: nil)

--

--