gpt4 book ai didi

ios - viewController.title 在 Google Analytics 初始化后为 nil

转载 作者:行者123 更新时间:2023-11-29 01:15:35 26 4
gpt4 key购买 nike

我关注了谷歌分析 doc将它添加到我的 swift 应用程序中

我已经完成了那里所说的一切,但我的代码在运行时失败了。

这是我在 AppDelegate 中的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
loadGoogleAnalytics()
chooseAndLuanchStoryboard()
initMembersAfterViewControllerInit()
return true
}

func loadGoogleAnalytics()
{
// Configure tracker from GoogleService-Info.plist.
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")

// Optional: configure GAI options.
let gai = GAI.sharedInstance()
gai.trackUncaughtExceptions = true // report uncaught exceptions
gai.logger.logLevel = GAILogLevel.Error
}

这是我在 ViewController 中的代码:

override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)

let name = "Pattern~\(self.title!)"

// The UA-XXXXX-Y tracker ID is loaded automatically from the
// GoogleService-Info.plist by the `GGLContext` in the AppDelegate.
// If you're copying this to an app just using Analytics, you'll
// need to configure your tracking ID here.
// [START screen_view_hit_swift]
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: name)

let builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
// [END screen_view_hit_swift]
}

override public func viewWillDisappear(animated: Bool) {
keyboardManager.deregisterFromKeyboardNotifications()
stopRefreshTimer()
}

我收到此运行时错误,因为 let name = "Pattern~\(self.title!)" 中的 self.title 为零

2016-02-05 19:44:44.377 Inline[95803:3750273] The configuration file 'GoogleService-Info.plist' is for another bundle identifier ('com.Inline.inline-ios'). Using this file the services may not be configured correctly. To continue with this configuration file, you may change your app's bundle identifier to 'com.Inline.inline-ios'. Or you can download a new configuration file that matches your bundle identifier from https://developers.google.com/mobile/add and replace the current one.
2016-02-05 19:44:44.383 Inline[95803:] <GMR/INFO> App measurement v.1302000 started
2016-02-05 19:44:44.384 Inline[95803:] <GMR/INFO> To enable debug logging set the following application argument: -GMRDebugEnabled (see http://goo.gl/Y0Yjwu)
2016-02-05 19:44:45.999 Inline[95803:] <GMR/INFO> App measurement enabled
2016-02-05 19:44:46.034 Inline[95803:3750273] Unknown class logo in Interface Builder file.
2016-02-05 19:44:46.048 Inline[95803:3750273] Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7faf9a079730> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.
2016-02-05 19:44:46.048 Inline[95803:3750273] Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7faf9a0729a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.
2016-02-05 19:44:46.048 Inline[95803:3750273] Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7faf9a07f8b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.
2016-02-05 19:44:46.051 Inline[95803:3750273] Device ID: (null)
2016-02-05 19:44:46.051 Inline[95803:3750273] RegisterDevice RPC request
scanning for beacons
fatal error: unexpectedly found nil while unwrapping an Optional value

如何使 self.title 不为零?

最佳答案

didFinishLaunchingWithOptions 上初始化

let gai = GAI.sharedInstance()
let id = "UA-ID"
gai.trackerWithTrackingId(id)
gai.trackUncaughtExceptions = true
gai.logger.logLevel = GAILogLevel.Warning
gai.defaultTracker.allowIDFACollection = false

通过 viewDidAppear 上的 setScreenName 调用谷歌分析的简单方法。

extension UIViewController {
func setScreeName(name: String) {
self.title = name
self.sendScreenView()
}

func sendScreenView() {
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: self.title)
let builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
}

func trackEvent(category: String, action: String, label: String, value: NSNumber?) {
let tracker = GAI.sharedInstance().defaultTracker
let trackDictionary = GAIDictionaryBuilder.createEventWithCategory(category, action: action, label: label, value: value)
tracker.send(trackDictionary.build() as [NSObject : AnyObject])
}
}

关于ios - viewController.title 在 Google Analytics 初始化后为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com