16

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?

Denys Triasunov
  • 539
  • 1
  • 4
  • 24
  • 3
    You might need to import another Facebook package. Maybe [FBAudienceNetwork](http://cocoadocs.org/docsets/FBAudienceNetwork/4.1.0/Classes/FBAdSettings.html)? – Cristik Nov 11 '20 at 18:30
  • Did you ever figure this out? I'm trying to do the same thing, having same issue – Michael Nov 18 '20 at 19:52

9 Answers9

25

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.

Ionut
  • 374
  • 4
  • 10
  • 1
    I'm having the same problem as OP, and this doesn't seem to work for me ether. That function can't be found in the Settings module either. The closest thing I've found is `Settings.isAdvertiserIDCollectionEnabled` – Michael Nov 17 '20 at 23:53
  • 2
    For me this solution seems to work, I will mark it as correct after some further "field" testing) I used ```import FBSDKCoreKit``` and ```FBSDKCoreKit.Settings.setAdvertiserTrackingEnabled(true)``` in request function. – Denys Triasunov Nov 28 '20 at 11:58
  • 2
    I have no `setAdvertiserTrackingEnabled` function on `Settings`, but I can use: `Settings.isAdvertiserIDCollectionEnabled = true` – Lyck Dec 15 '20 at 00:45
  • Was running an old version of Facebook SDK - updated and then could use 'setAdvertiserTrackingEnabled' too. – Lyck Apr 12 '21 at 23:57
  • Note that for this to work you need `pod FBSDKCoreKit` — *not* `pod FBSDKCoreKit/Swift`! The latter would pull in the older version `6.5.2` of the SDK in which this `setAdvertiserTrackingEnabled` property does not exist. – PDK Apr 19 '21 at 15:03
  • How long does it take to reflect on Event Manager dashboard ? – Nizzam Oct 01 '21 at 13:36
  • Settings.shared.isAutoLogAppEventsEnabled = true Settings.shared.isAdvertiserTrackingEnabled = true Settings.shared.isAdvertiserIDCollectionEnabled = true – Michael42 May 13 '22 at 09:23
5

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

and do:

 import FBAudienceNetwork

it will work.

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
4

You have to import the FBAudienceNetwork library

In Objective-C

#include <FBAudienceNetwork/FBAdSettings.h>

In Swift

import FBAudienceNetwork
DJTano
  • 1,055
  • 12
  • 12
3

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.

2

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.

roamer
  • 326
  • 2
  • 5
2

You need the Facebook audienceNetwork library.

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

Justin A
  • 3,891
  • 2
  • 18
  • 22
1

To manually change tracking, from version 13.0.0 and beyond, you need to use Settings.shared in places where Settings was used before.

Changelog:

Removed deprecated Settings.setAdvertiserTrackingEnabled(_:) (use the Settings.shared.isAdvertiserTrackingEnabled property to set a value instead")

If you are using version 13.0.0 and beyond, use

 Settings.shared.isAdvertiserTrackingEnabled = true 

instead of

 Settings.isAdvertiserTrackingEnabled = true

Be advised that beyond iOS 14.5 you need to ask the user for permissions first, example usaged below:


   func requestTracking(){
                     if #available(iOS 14, *) {
                         ATTrackingManager.requestTrackingAuthorization(completionHandler: { (status) in
                             switch status {
                             case .authorized:
                                 Settings.shared.isAutoLogAppEventsEnabled = true
                                 Settings.shared.isAdvertiserTrackingEnabled = true
                                 Settings.shared.isAdvertiserIDCollectionEnabled = true
                                 break
                             case .denied:
                                 Settings.shared.isAutoLogAppEventsEnabled = false
                                 Settings.shared.isAdvertiserTrackingEnabled = false
                                 Settings.shared.isAdvertiserIDCollectionEnabled = false
                                 break
                             default:
                                 break
                             }
                         })
                     } else {
                         Settings.shared.isAutoLogAppEventsEnabled = true
                         Settings.shared.isAdvertiserTrackingEnabled = true
                         Settings.shared.isAdvertiserIDCollectionEnabled = true
                     }

0

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)
AntiPawn79
  • 385
  • 2
  • 4
  • 16
0

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".