Adding App Events in iOS app produces undeclared identifier 'AppEvent'
Asked Answered
P

3

8

I am trying to add facebook app events, and I installed FacebookSDK. Then I have next code:

import FacebookCore

static func logViewContentEvent(contentType : String, contentData : String, contentId : String, currency : String, price : Double) {
        let params : AppEvent.ParametersDictionary = [
            .contentType : contentType,
            .content : contentData,
            .contentId : contentId,
            .currency : currency
        ]
        let event = AppEvent(name: .viewedContent, parameters: params, valueToSum: price)
        AppEventsLogger.log(event)
    }

But I get error from the title. What I am doing wrong, and why this type is missing?

Pfosi answered 27/11, 2019 at 16:42 Comment(11)
what is .viewedContentin the name param of AppEvent?Sty
@AndresGomez I wonder that too. That is code generated by facebook's app event code generator for the ViewContent event.Pfosi
developers.facebook.com/docs/app-events/…Pfosi
have you tried to change . viewedContent for some string?Sty
Its not only that. @AndresGomez the problem is AppEvent is undeclared, so it doesn't compile. I don't know what I should import so to make that type available.Pfosi
try import FBSDKCoreKitSty
I have that it imported, and FacebookCore as well.Pfosi
I have the same issue. The docs are really bad! I'm not even sure which pods to include. FacebookCore doesn't seem to be necessary to log events which I do with AppEvents.log(AppEvents.Name("foobar")) . But I cannot log custom events!Moores
how did you add the sdk? pod? carthage?Rasp
I added it using pod. @RaspPfosi
@Pfosi got that. Shared you my setup and it works for me.Rasp
P
8

It's a knowed facebook annoying issue.

Before FBSDK 5.0.1 your code works well. After, you can change your code with for example:

static func logViewContentEvent(contentType : String, contentData : String, contentId : String, currency : String, price : Double) {
        let params =  [
            "contentType": contentType,
            "content" : contentData,
            "contentID" : contentId,
            "currency" : currency
        ]
        AppEvents.logEvent(AppEvents.Name(rawValue: "viewedContent"), valueToSum: price, parameters: params)
    }
Propraetor answered 3/12, 2019 at 13:54 Comment(1)
It works like this, but its kinda hacky way to achive what I wanted. Well, its only solution for now I guess. Thanks for the answer man.Pfosi
B
4

In top of file get rid of FacebookCore use only

         import FBSDKCoreKit
         // implemented  FBSDKCoreKit (5.8.0)
         let event =  "custom event"
         AppEvents.logEvent(AppEvents.Name.init(rawValue: event))

or with parameters

    let a:[String: String] = ["key": "value"]
    AppEvents.logEvent(AppEvents.Name.init(rawValue: event), parameters: a )
Basinger answered 6/12, 2019 at 11:27 Comment(0)
R
0

I have mine working with these set up:

  • Installed via cocoapod with use_frameworks! configuration

pod 'FBSDKCoreKit', '~> 5.11.1'

  • Since my project supports both ObjC and Swift I imported <FBSDKCoreKit/FBSDKCoreKit.h> on the bridging file
static func logContent(id: String, type: String, valueToSum: Double) {
        AppEvents.logEvent(.viewedContent,
                           valueToSum: valueToSum,
                           parameters: [contentType: type,
                                        contentID: id,
                                        currency: "USD"])
    }
Rasp answered 6/12, 2019 at 14:22 Comment(4)
This ways work for me to. Checkout actually my code in original question. There is no type AppEvent etc... AppEvents exists.Pfosi
oh. sorry. I missed out that you're referring to logging custom events. Let me try and see if I can find something that will helpRasp
I am trying to log standard events with the code generated with Facebook code generator. The code is broken, it doesnt work. What you are posting works for me to.Pfosi
@Pfosi Sorry for the confusion. I would stick with what Alessandro Ornano suggested. I think the docs are probably outdated and AppEvent is something they've entirely removed.Rasp

© 2022 - 2024 — McMap. All rights reserved.