Swift集成友盟

作者: 迷了jiang | 来源:发表于2019-03-21 15:44 被阅读0次

    友盟是一个非常强大的数据平台,经过多年的沉淀,被广大应用厂商所青睐,而且是免费的;是数据集成的不二之选。但是很遗憾,友盟并没有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集成友盟,这边已经测试通过,欢迎大家讨论!

    相关文章

      网友评论

        本文标题:Swift集成友盟

        本文链接:https://www.haomeiwen.com/subject/ettcvqtx.html