在Google分析初始化之后,viewController.title为零 [英] viewController.title is nil after init of Google analytics
问题描述
我遵循Google Analytics(分析)文档添加它适用于我的swift应用程序
我已经完成了所有这些,
,但是我的代码在运行时失败。
这是我在AppDelegate中的代码:
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject :AnyObject]?) - > Bool {
loadGoogleAnalytics()
chooseAndLuanchStoryboard()
initMembersAfterViewControllerInit()
返回true
}
func loadGoogleAnalytics()
{
//从GoogleService-Info.plist中配置跟踪器。
var configureError:NSError?
GGLContext.sharedInstance()。configureWithError(& configureError)
assert(configureError == nil,配置Google服务时出错:\(configureError))
//可选:配置GAI选项。
let gai = GAI.sharedInstance()
gai.trackUncaughtExceptions = true //报告未捕获的异常
gai.logger.logLevel = GAILogLevel.Error
}
这是我在ViewController中的代码:
override public func viewWillAppear(animated:Bool){
super.viewWillAppear(true)
let name =Pattern〜\(self.title!)
// UA-XXXXX-Y跟踪器ID是由AppDelegate中的`GGLContext`的
// GoogleService-Info.plist自动加载的。
//如果您只是使用Google Analytics将其复制到应用程序中,则需要
//在此处配置您的跟踪ID。
// [START screen_view_hit_swift]
让tracker = GAI.sharedInstance()。defaultTracker
tracker.set(kGAIScreenName,value:name)
builder = GAIDictionaryBuilder .createScreenView()
tracker.send(builder.build()as [NSObject:AnyObject])
// [END screen_view_hit_swift]
}
override public func viewWillDisappear(animated:Bool){
keyboardManager.deregisterFromKeyboardNotifications()
stopRefreshTimer()
}
我得到这个运行时错误是因为 self.title
在 let name =Pattern〜\(self.title !)
是nil
2016-02-05 19:44:44.377内联[95803: 3750273]配置文件'GoogleService-Info.plist'用于另一个包标识符('com.Inline.inline-ios')。使用此文件可能无法正确配置服务。要继续使用此配置文件,可以将应用程序的包标识符更改为'com.Inline.inline-ios'。或者,您可以从https://developers.google.com/mobile/add下载与您的捆绑包标识符相匹配的新配置文件,并替换当前的配置文件。
2016-02-05 19:44:44.383内嵌[95803:]< GMR / INFO>应用程序测量v.1302000开始
2016-02-05 19:44:44.384内嵌[95803:]< GMR / INFO>要启用调试日志记录,请设置以下应用程序参数:-GMRDebugEnabled(请参阅http://goo.gl/Y0Yjwu)
2016-02-05 19:44:45.999内联[95803:]< GMR / INFO>启用应用程序测量
2016-02-05 19:44:46.034内联[95803:3750273] Interface Builder文件中的未知类标识。
2016-02-05 19:44:46.048 Inline [95803:3750273]未能在(UIView)上设置(keyPath)用户定义的检查属性:[< UIView 0x7faf9a079730> setValue:forUndefinedKey:]:此类不是密钥keyPath的密钥值编码。
2016-02-05 19:44:46.048内联[95803:3750273]未能在(UIView)上设置(keyPath)用户定义的检查属性:[< UIView 0x7faf9a0729a0> setValue:forUndefinedKey:]:此类不是密钥keyPath的密钥值编码。
2016-02-05 19:44:46.048内联[95803:3750273]未能设置(keyPath)用户定义的检查属性(UIView):[< UIView 0x7faf9a07f8b0> setValue:forUndefinedKey:]:此类不是密钥keyPath的密钥值编码。
2016-02-05 19:44:46.051内联[95803:3750273]设备ID:(空)
2016-02-05 19:44:46.051内联[95803:3750273] RegisterDevice RPC request
扫描信标
致命错误:意外地发现nil,同时展开可选值
我怎样才能使 self.title
为非零?
Init on didFinishLaunchingWithOptions
let gai = GAI.sharedInstance()
let id =UA-ID
gai.trackerWithTrackingId(id)
gai.trackUncaughtExceptions = true
gai.logger.logLevel = GAILogLevel.Warning
gai.defaultTracker .allowIDFACollection = false
通过 setScreenName
on viewDidAppear
。
扩展UIViewController {
func setScreeName(name:String){
self.title = name
self.sendScreenView()
}
$ b $ 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])
}
}
I followed Google analytics doc to add it to my swift app
I have done all that is said there, and yet my code fails in run time.
Here is my code in 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
}
Here is my code in the 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()
}
I get this run-time error because self.title
in let name = "Pattern~\(self.title!)"
is nil
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
how can I make the self.title
be non-nil?
Init on didFinishLaunchingWithOptions
let gai = GAI.sharedInstance()
let id = "UA-ID"
gai.trackerWithTrackingId(id)
gai.trackUncaughtExceptions = true
gai.logger.logLevel = GAILogLevel.Warning
gai.defaultTracker.allowIDFACollection = false
Easy way to call google analytics via setScreenName
on viewDidAppear
.
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])
}
}
这篇关于在Google分析初始化之后,viewController.title为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!