Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 14 get user consent with Facebook SDK

I'm not new to iOS development but this is my first time dealing with Facebook SDK. I've been following the FB guide to set up event in my app (installed SDK Swift package, added FBSDKCoreKit methods to AppDelegate), up until the very last instruction on getting user consent with iOS 14.

The Facebook guide provides this code snippet to use when getting consent with requestTrackingAuthorization():

FBAdSettings.setAdvertiserTrackingEnabled(true)

Problem is the FBAdSettings class doesn't seem to be valid in my code (Xcode complains it cannot be found in scope), although I did import FBSDKCoreKit and there's no other FB modules to import.

Here is my code in full:

import UIKit
import AppTrackingTransparency
import FBSDKCoreKit

extension ViewController {
    func requestTrackingPermission() {
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization { (status) in
                switch status {
                    case .authorized:
                        FBAdSettings.setAdvertiserTrackingEnabled(true)  //Cannot find 'FBAdSettings' in scope
                    ...
                }
            }
        } else {
            // Fallback on earlier versions
        }
    }
}

What am I missing here?

like image 366
Denys Triasunov Avatar asked Nov 11 '20 18:11

Denys Triasunov


4 Answers

the right solution is to use iOS Audience Network SDK >6.0

and do:

 import FBAudienceNetwork

it will work.

like image 131
Muhammad Rafiq Avatar answered Oct 18 '22 23:10

Muhammad Rafiq


The documentation is at least misleading. You have to import FBSDKCoreKit.FBSDKSettings and the snippet is Settings.setAdvertiserTrackingEnabled(true). As you can see, it is not FBAdSettings but only Settings.

like image 42
Ionut Avatar answered Oct 18 '22 21:10

Ionut


I found out that you don't even need the method Settings.setAdvertiserTrackingEnabled(true) if you just want to log events.

So you can remove it from the authorization request, in fact the request is sufficient to activate event logging.

You can verify it by using Facebook's Event Manager to test your logged events.

like image 3
Elias Al Zaghrini Avatar answered Oct 18 '22 22:10

Elias Al Zaghrini


You have to import the FBAudienceNetwork library

In Objective-C

#include <FBAudienceNetwork/FBAdSettings.h>

In Swift

import FBAudienceNetwork
like image 3
DJTano Avatar answered Oct 18 '22 23:10

DJTano