Swift集成友盟

友盟是一个非常强大的数据平台,经过多年的沉淀,被广大应用厂商所青睐,而且是免费的;是数据集成的不二之选。但是很遗憾,友盟并没有Swift版的sdk(期待推出啊),可能是有自己的考虑,所以在集成的时候只能通过桥接OC的方式;在页面统计的时候有两种方式,一种是通过集成在基类的viewWillAppear和viewWillDisappear进行统计,另一种是采用AOP方式,调用运行时的Method Swizzing。我们这里采用了AOP的方式。

一、集成友盟sdk
1.我们这边只有用友盟做了数据统计,没有集成推送和分享等,所有在pods里配置统计的sdk即可(这边接入的是非idfa的sdk)

    pod 'UMengAnalytics-NO-IDFA'

2.进行桥接,首先创建一个OC的header文件,把友盟的SDK头文件导入,桥接的时候在Build settings里搜索Objective-C Bridging Header填入我们刚才创建好的头文件路径,配置好编译一下,编译成功之后就可以coding了


创建头文件

配置桥接文件路径

3.在应用启动时候进行配置

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 统计
        let umconfig = UMAnalyticsConfig.sharedInstance()
        umconfig?.appKey = ""
        umconfig?.channelId = "AppStore"
        MobClick.start(withConfigure: umconfig)
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
        MobClick.setAppVersion(version)
        return true
}

此时已经可以统计到日活和新增数据了,但是产品还有页面路径的需求,所有我们还要在页面里面做埋点

二、Swift调用Method Swizzing
这里我们封装一个UIViewController的category,分别hook了viewWillAppear和viewWillDisappear两个方法。

extension UIViewController {
    @objc func newViewWillAppear(_ animated: Bool) {
        self.newViewWillAppear(animated) //Incase we need to override this method
        MobClick.beginLogPageView(type(of: self).description())
        print("WillAppear" + type(of: self).description())
    }
    
    @objc func newViewWillDismissAppear(_ animated: Bool) {
        self.newViewWillAppear(animated) //Incase we need to override this method
        MobClick.endLogPageView(type(of: self).description())
        print("WillDisAppear" + type(of: self).description())
    }
    
    static func swizzleViewWillAppear() {
        //Make sure This isn't a subclass of UIViewController, So that It applies to all UIViewController childs
        if self != UIViewController.self {
            return
        }
        let _: () = {
            let originalSelector = #selector(UIViewController.viewWillAppear(_:))
            let swizzledSelector = #selector(UIViewController.newViewWillAppear(_:))
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
            method_exchangeImplementations(originalMethod!, swizzledMethod!);
        }()
    }
    
    static func swizzleViewWillDismissAppear() {
        //Make sure This isn't a subclass of UIViewController, So that It applies to all UIViewController childs
        if self != UIViewController.self {
            return
        }
        let _: () = {
            let originalSelector = #selector(UIViewController.viewWillDisappear(_:))
            let swizzledSelector = #selector(UIViewController.newViewWillDismissAppear(_:))
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
            method_exchangeImplementations(originalMethod!, swizzledMethod!);
        }()
    }

}

封装好之后记得在appdelegate里执行一下

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UIViewController.swizzleViewWillAppear()
        UIViewController.swizzleViewWillDismissAppear()
        return true
}

好的,以上就是我们的swift集成友盟,这边已经测试通过,欢迎大家讨论!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从这个app开始用上了swift,最近又集成了友盟统计功能,根据友盟的文档一步一步来整体比较顺利,但是中间也遇到了...
    移动端_小刚哥阅读 9,607评论 12 4
  • github链接 我的博客 前言 友盟统计分析平台是国内最大的移动应用统计分析平台。用于帮助移动应用开发商统计和分...
    Cheero阅读 5,800评论 1 1
  • 公司运营部提了一个需求需要统计打开某个页面的次数,点击某个按钮的次数和定时器自动关闭的次数,上次因为就这一个地方,...
    玉思盈蝶阅读 5,870评论 4 4
  • 关于友盟统计和itunesConnect统计到的下载量不一致的问题解释: 友盟统计是根据设备来进行统计的,如果想要...
    流星大石头阅读 7,724评论 1 2
  • 1、前期准备 集成之前, 请在http://push.umeng.com/申请开通【友盟+】消息推送服务。下载 U...
    z小志阅读 6,340评论 0 0

友情链接更多精彩内容