'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?



Solution 1:[1]

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.

Solution 2:[2]

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

and do:

 import FBAudienceNetwork

it will work.

Solution 3:[3]

FBAdSettings is in the FBAudienceNetwork SDK, which is different from Facebook SDK. So you need to install the FBAudienceNetwork SDK(https://developers.facebook.com/docs/audience-network/guides/adding-sdk/ios) and import FBAdSettings instead.

Solution 4:[4]

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.

Solution 5:[5]

You have to import the FBAudienceNetwork library

In Objective-C

#include <FBAudienceNetwork/FBAdSettings.h>

In Swift

import FBAudienceNetwork

Solution 6:[6]

You need the Facebook audienceNetwork library.

Facebook has done a pretty horrible job of keeping its development docs updated.

Solution 7:[7]

My solution using Objective C:

in Podfile (located under ios/)

target 'my_app' do:
     pod 'FBAudienceNetwork'
     pod 'FBSDKCoreKit'
   ... other code ...

in AppDelegate.m:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#include <FBAudienceNetwork/FBAdSettings.h>


- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       [FBAdSettings setAdvertiserTrackingEnabled:YES];
... other code ...
}

Run rm -rf Pods where your pods directory is. Run pod install

Ended up installing these versions in case it matters:

  • FBSDKCoreKit (12.2.1)
  • FBAudienceNetwork (6.9.0)

Solution 8:[8]

A Little Tip

If you have a function named "Settings", it calls "setAdvertiserTrackingEnabled" in the methods inside the "Settings" function that you wrote for another job and gives an error.

You can change your "Settings" function name or use "FBSDKCoreKit.Settings". This way you can call "setAdvertiserTrackingEnabled".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ionut
Solution 2 Simas Joneliunas
Solution 3 roamer
Solution 4 Elias Al Zaghrini
Solution 5 DJTano
Solution 6 Justin A
Solution 7 AntiPawn79
Solution 8 Osman Y?ld?z