友盟是一個非常強大的數(shù)據(jù)平臺,經過多年的沉淀譬正,被廣大應用廠商所青睞宫补,而且是免費的;是數(shù)據(jù)集成的不二之選曾我。但是很遺憾粉怕,友盟并沒有Swift版的sdk(期待推出啊),可能是有自己的考慮抒巢,所以在集成的時候只能通過橋接OC的方式贫贝;在頁面統(tǒng)計的時候有兩種方式,一種是通過集成在基類的viewWillAppear和viewWillDisappear進行統(tǒng)計,另一種是采用AOP方式,調用運行時的Method Swizzing笔诵。我們這里采用了AOP的方式克蚂。
一、集成友盟sdk
1.我們這邊只有用友盟做了數(shù)據(jù)統(tǒng)計,沒有集成推送和分享等,所有在pods里配置統(tǒng)計的sdk即可(這邊接入的是非idfa的sdk)
pod 'UMengAnalytics-NO-IDFA'
2.進行橋接,首先創(chuàng)建一個OC的header文件也搓,把友盟的SDK頭文件導入,橋接的時候在Build settings里搜索Objective-C Bridging Header填入我們剛才創(chuàng)建好的頭文件路徑涵紊,配置好編譯一下傍妒,編譯成功之后就可以coding了
3.在應用啟動時候進行配置
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 統(tǒng)計
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
}
此時已經可以統(tǒng)計到日活和新增數(shù)據(jù)了,但是產品還有頁面路徑的需求摸柄,所有我們還要在頁面里面做埋點
二颤练、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里執(zhí)行一下
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIViewController.swizzleViewWillAppear()
UIViewController.swizzleViewWillDismissAppear()
return true
}
好的昔案,以上就是我們的swift集成友盟尿贫,這邊已經測試通過,歡迎大家討論!