简体   繁体   English

在 Swift 中使用 GTM v5 TAGCustomFunction

[英]Using GTM v5 TAGCustomFunction in Swift

I'm integrating GTM v5 (GTM + Firebase) in a Swift project, and I want to be able to call some methods when tags are triggered.我正在 Swift 项目中集成 GTM v5 (GTM + Firebase),我希望能够在触发标签时调用一些方法。 However, it doesn't seem to work with Swift, although similar implementations in Objective C and Android projects did work.然而,它似乎不适用于 Swift,尽管 Objective C 和 Android 项目中的类似实现确实有效。

Here's the class conforming to the TAGCustomFunction protocol :这是符合TAGCustomFunction协议的类:

import Foundation
import GoogleTagManager

final class Tags: NSObject, TAGCustomFunction {

    func execute(withParameters parameters: [AnyHashable : Any]!) -> NSObject! {
        print("YEAH ! IT WORKS !")
        return nil
    }
}

Everything is working well, even though I see these kind of logs:一切都运行良好,即使我看到这些日志:

GoogleTagManager info: Processing logged event: applicationStart with parameters: (null) GoogleTagManager 信息:处理记录的事件:applicationStart 参数:(空)

But the log I'm printing is not showing…但是我正在打印的日志没有显示......

I'm sure about the configuration of the container since this one is correctly loaded, and I use the exact same container for my Objective C project, in which it works perfectly.我确定容器的配置,因为这个容器已正确加载,并且我为我的 Objective C 项目使用了完全相同的容器,它可以完美地工作。

I think TAGCustomFunction needs to have class/method @objc annotations, and the class-level @objc annotation needs to specify the class name, ie我认为TAGCustomFunction需要有类/方法@objc批注类级别@objc注释需要指定类名,即

import Foundation
import GoogleTagManager

@objc(Tags)
final class Tags: NSObject, TAGCustomFunction {

    @objc func execute(withParameters parameters: [AnyHashable : Any]!) -> NSObject! {
        print("YEAH ! IT WORKS !")
        return nil
    }
}

After reading this SO post , I suddenly remembered that a Swift project could embed some objective C classes.读完这篇 SO post 后,我突然想起一个 Swift 项目可以嵌入一些客观的 C 类。 The solution to my problem was ridiculously easy to set up once I realized that, eventhough I've never had to do it before.一旦我意识到这一点,我的问题的解决方案就非常容易设置,即使我以前从未这样做过。

I created a new Cocoa Touch Class like the following :我创建了一个新的Cocoa Touch Class ,如下所示:

  • Here is the .m :这是 .m :

     #import "MyCustomTagClass.h" #import "MySwiftClass-Swift.h" @implementation MyCustomTagClass - (NSObject*)executeWithParameters:(NSDictionary*)parameters { [MySwiftClass myMethod]; } @end
  • And here is the .h :这是 .h :

     #import <Foundation/Foundation.h> #import <GoogleTagManager/TAGCustomFunction.h> @interface MyCustomTagClass : NSObject <TAGCustomFunction> - (NSObject*)executeWithParameters:(NSDictionary*)parameters; @end

Note that I import a header for my Swift class, which is automatically generated by Xcode.请注意,我为我的 Swift 类导入了一个标头,它是由 Xcode 自动生成的。 Just add -Swift.h after the name of your class to import it, just as I did in the .m example above.只需在类名后添加-Swift.h即可导入它,就像我在上面的 .m 示例中所做的那样。 Last, but not least, update your Swift class with @objc annotations at class and method declaration lines :最后但并非最不重要的是,在类和方法声明行使用@objc注释更新您的 Swift 类:

import Foundation

@objc class MySwiftClass: NSObject {

   //...

   @objc static func myMethod() {
        // do something...
    }
}

I hope this helped !我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM