I have a problem with Swift 2 (Swift 3) and Google Analytics.
This is the line with the problem:
tracker.send(GAIDictionaryBuilder.createScreenView().build())
Xcode tell's me:
Cannot invoke 'send' with an argument list of type '(NSMutableDictionary!)'
Update for Swift 3 (2016.10.19)
let tracker = GAI.sharedInstance().defaultTracker
let build = (GAIDictionaryBuilder.createScreenView().build() as NSDictionary) as! [AnyHashable: Any]
tracker?.send(build)
Still an ugly approach, let me know if there's an cleaner conversion.
Original
Same here, struggling to resolve tons of errors.
What I did (deprecated):
var build = GAIDictionaryBuilder.createAppView().build() as [NSObject : AnyObject]
tracker.send(build)
Edit (2015)
Thanks to #George Poulos. . Recently they updated the options, now createAppView is deprecated, should use createScreenView instead.
var build = GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject]
tracker.send(build)
In addition to the accepted answer:
Changed this:
tracker.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "User sign in", label: nil, value: nil).build())
To this:
tracker.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "User sign in", label: nil, value: nil).build() as [NSObject : AnyObject])
This might be a bit of an overkill, but I prefer creating a short extension and not need to type the castings every time:
In any swift file, paste the following code:
extension GAIDictionaryBuilder
{
func buildSwiftCompatible() -> [NSObject:AnyObject]
{
return self.build() as [NSObject:AnyObject]
}
}
Then you can call buildSwiftCompatible() instead of the usual build():
tracker.send(GAIDictionaryBuilder.createScreenView().buildSwiftCompatible())
Have fun.
This is a solution I came up with.. Maybe it could help some of you. It's a struct you need to instantiate in every UIViewController, but it helps with the boilerplate.
import UIKit
struct Analytics {
fileprivate let viewController: UIViewController
fileprivate let tracker = GAI.sharedInstance().defaultTracker
init (forScreen viewController: UIViewController) {
self.viewController = viewController
}
func startTracking () {
let screenView = GAIDictionaryBuilder.createScreenView().build() as NSDictionary
guard
let tracker = tracker,
let build = screenView as? [AnyHashable: Any]
else { return }
tracker.set(kGAIScreenName, value: String(describing: viewController))
tracker.send(build)
}
}
class HomeViewController: UIViewController {
lazy var analytics: Analytics = {
return Analytics(forScreen: self)
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear() {
super.viewWillAppear()
analytics.startTracking()
}
}
For swift 3:
let build:NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker?.send(build as! [AnyHashable: Any])
let build = GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject]
tracker?.send(build)
Related
I am working on my first Apple Watch app (an extension to my iOS app). I am facing a small problem in sending data from one WKInterfaceController to another.
My First Controller (InterfaceController.swift) has didReceiveMessage where it receives data from my iOS app.
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
let myQrValue = message["qrCode"] as! String
let myQrImage = message["qrCodeImageData"] as! Data
var myData: [AnyHashable: Any] = ["myQrValue": myQrValue, "myQrImage": myQrImage]
if myQrValue.isEmpty == false {
WKInterfaceController.reloadRootControllers(withNames: ["QrScreen"], contexts: [myData])
}
}
Then in my Second Controller (QrInterfaceController.swift), I am having below to fetch the data sent from the first controller -
override func awake(withContext context: Any?) {
super.awake(withContext: context)
print("context \(context)")
if let myData = context {
print("myData \(myData)")
// userQrNumber.setText(myData)
}
if let myQrImage = myQrImage {
userQrImage.setImageData(myQrImage)
}
if let myQrLabel = myQrLabel {
userQrNumber.setText(myQrLabel)
}
self.setTitle("")
}
I am stuck (could be simple/silly question) as how to parse my data from the context in the second controller?
Also, the didReceiveMessage works only the second time when I launch my ViewController where the sendMessage code is placed. Is it normal?
First, you might want to redeclare myData as this:
var myData: [String: Any] = ...
which makes it a bit simpler. Then, in the awake function, you’d go ahead like this:
if let myData = context as? [String: Any] {
if let myQrImage = myData["myQrValue"] as? Date {
...
Does this show you the right direction?
I'm searching for a clean way to retrieve (and sometimes save) data from Firebase in Swift. It's annoying me that all my database calls are written in the middle of the view controller code. So I'm looking for some kind of custom data service class. I found this tutorial that's close to what I want: http://www.mobilecyberpunks.com/?p=82.
They promised a Part II but I cannot find this second part, so I guess this was never made. In this second part they promised to cover retrieving and saving data with this custom data service (which is the most important part of the whole thing for me).
I'm thinking of an API class (like in the tutorial) and when I'm retrieving data, and it finishes retrieving from firebase, I save it in a data set in this api class. Then I will posting a notification with Notification Center. But I'm am not sure whether this is best practice or a good way to do this.
Has anyone an idea how to do this (finishing this tutorial I found or in another way)?
Thanks in advance!
Making a custom Class for the communicating is generally a good idea if you need extensive function's and make numerous calls to your server.
The two preferred methods for this are:-
Protocol-Delegate Method
_completionBlocks:
Below answer contains both.
Custom Class
import Foundation
import Firebase
#objc protocol FIRShowAlertDelegate {
func showFIRAlert(_ message : String)
#objc optional func activityIndic()
}
class FIRController :{
var delegate : FIRShowAlertDelegate!
func loginUser(_ emailAddress : String!, password : String , completionBlock : #escaping ((currentUserID : String!) -> Void)){
FIRAuth.auth()?.signIn(withEmail: emailAddress, password: password,
completion: {(user,err) in
if err != nil{
self.delegate.showFIRAlert("Error logging you in,\(err?.localizedDescription)")
}else{
completionBlock(user!.uid)
}
})
}
func retrieveUserData(_ currentId : String!, completionBlock : #escaping ((_ userName : String?) -> Void)){
FIRDatabase.database().reference().child("Users").child(currentId).observeSingleEvent(of: .value, with: {(userSnap) in
if userSnap.exists(){
if let userDict = userSnap.value! as? [String:AnyObject]{
completionBlock(userDict["username"] as! String
}
}else{
completionBlock(nil, nil)
print("No such user exists: \(currentId)")
}
})
}
}
Your ViewController
class AnyViewController : UIViewController, FIRShowAlertDelegate{
let firebaseControllerHandle : FIRController = FIRController()
override func viewDidLoad() {
super.viewDidLoad()
firebaseControllerHandle.delegate = self
firebaseControllerHandle.loginUser("abc#xyz.com", password: "123454321", completionBlock: { (userID) in
print("user : \(userID), logged in")
})
}
func showFIRAlert(_ message : String){
let alertController : UIAlertController = UIAlertController(title: "MyApp", message: message, preferredStyle: .alert)
let okAction : UIAlertAction = UIAlertAction(title: "Ok", style: .default) { (alert) in
print("User pressed ok function")
}
alertController.addAction(okAction)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
self.present(alertController, animated: true, completion: nil)
}
func activityIndic() {
// Use for showing the activity indicator while the data is being retrieved
}
}
I started to use this solution and polished it a little bit, and I came to a pretty handy solution.
I created a custom class named FirebaseAPI. This is a singleton class. This class contains all the methods for Firebase (Authentication, Database, Storage, ...).
Example:
FirebaseAPI.swift
import FirebaseAuth
import FirebaseDatabase
class FirebaseAPI {
static let shared = FirebaseAPI()
private init() {}
//Authentication
func logInUser(onCompletion: #escaping (String?) -> Void {
FIRAuth.auth().signInAnonymously(completion: {(user, error) in
if error == nil {
onCompletion(user!.uid)
} else {
onCompletion(nil)
}
})
}
//Database
func getObjects(parameter: ParamaterClass, onCompletion: #escaping ([ObjectClass]) -> Void) {
Constants.Firebase.References.Object?.observe(.value, with: { snapshot in
var objects = [ObjectClass]()
if snapshot.exists() {
for child in snapshot.children.allObjects {
let object = Object(snapshot: child as! FIRDataSnapshot)
objects.append(object)
}
}
onCompletion(objects)
})
}
}
Constants.swift
import FirebaseDatabase
struct Constants {
struct Firebase {
static var CurrentUser: FIRDatabaseReference?
static var Objects: FIRDatabaseReference?
}
}
AppDelegate.swift
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
FirebaseAPI.shared.logInUser(onCompletion { uid in
if uid != nil {
Constants.Firebase.References.CurrentUser = FIRDatabase.database().reference().child("users").child(uid!)
Constants.Firebase.References.CurrentUser.keepSynced(true)
Constants.Firebase.References.Objects = FIRDatabase.database().reference().child("objects")
Constants.Firebase.Reference.Objects?.keepSynced(true)
}
})
}
return true
}
I can give you a example of calling methods in the FirebaseAPI in a ViewController, but an example of such a method is given in the code of the AppDelegate.swift up here (the FirebaseAPI.shared.logInUser method).
Used this structure in 3 different projects up till now and it works fluently!
It's my first time integrating google analytics in my app.I'm just following this official document here
I already have a tracker ID. I don't want to create configuration file.
How can I use that tracker ID and how can I integrate Google analytics?
Create extension of UIView Conroller.
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])
}
}
For Each view Controller viewdidload() you add following code
self.title = self.navigationItem.title!
self.sendScreenView()
Between the pod spec and what is currently on S.O. I had a tough time figuring out how to get speech-to-text working using SpeechKit + CocoaPod + Swift. Finally got it working so figured I'd help the next poor soul that comes looking for help! :)
First install the CocoaPod: https://cocoapods.org/pods/SpeechKit
Add #import <SpeechKit/SpeechKit.h> to your bridging header
Login to Nuance's dev portal and create an app: https://developer.nuance.com/
Clean up the demo code so that is is more organized. I just wanted as much of the code to be in one place as possible so you can see a fully working implementation.
Then create a UIViewController and add the following code with the correct credentials:
import UIKit
import SpeechKit
class SpeechKitDemo: UIViewController, SKTransactionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//!!link this to a corresponding button on the UIViewController in I.B.
#IBAction func tappedButton(sender: AnyObject) {
// All fields are required.
// Your credentials can be found in your Nuance Developers portal, under "Manage My Apps".
let SKSAppKey = "[Get this from the nuance app info page]";
let SKSAppId = "[Get this from the nuance app info page]";
let SKSServerHost = "[Get this from the nuance app info page]";
let SKSServerPort = "[Get this from the nuance app info page]";
let SKSLanguage = "eng-USA";
let SKSServerUrl = "nmsps://\(SKSAppId)#\(SKSServerHost):\(SKSServerPort)"
let session = SKSession(URL: NSURL(string: SKSServerUrl), appToken: SKSAppKey)
//this starts a transaction that listens for voice input
let transaction = session.recognizeWithType(SKTransactionSpeechTypeDictation,
detection: .Short,
language: SKSLanguage,
delegate: self)
print(transaction)
}
//required delegate methods
func transactionDidBeginRecording(transaction: SKTransaction!) { }
func transactionDidFinishRecording(transaction: SKTransaction!) { }
func transaction(transaction: SKTransaction!, didReceiveRecognition recognition: SKRecognition!) {
//Take the best result
let topRecognitionText = recognition.text;
print("Best rec test: \(topRecognitionText)")
//Or iterate through the NBest list
let nBest = recognition.details;
for phrase in (nBest as! [SKRecognizedPhrase]!) {
let text = phrase.text;
let confidence = phrase.confidence;
print("\(confidence): \(text)")
}
}
func transaction(transaction: SKTransaction!, didReceiveServiceResponse response: [NSObject : AnyObject]!) { }
func transaction(transaction: SKTransaction!, didFinishWithSuggestion suggestion: String!) { }
func transaction(transaction: SKTransaction!, didFailWithError error: NSError!, suggestion: String!) { }
}
I'm calling on my ExtensionDelegate from ComplicationController to give an array of evnts.
Seems to work fine calling ExtensionDelegate from InterfaceController, both of which are in my watch app.
But for some reason, I get 0 items in the evnts array when calling on my ExtensionDelegate from my ComplicationController.
Any ideas? Thanks!
ExtensionDelegate:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
static var evnts = [Evnt]()
ComplicationController:
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
// extEvnts = 0 somehow here
let extEvnts = ExtensionDelegate.evnts
This all works fine when I do it from my InterfaceController though:
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
ExtensionDelegate.evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))
doTable()
} else {
print("matchValue are not same as dictionary value")
}
}
func doTable() {
let extEvnts = ExtensionDelegate.evnts
self.rowTable.setNumberOfRows(extEvnts.count, withRowType: "rows")
for (index, evt) in extEvnts.enumerate() {
if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {
row.mLabel.setText(evt.eventMatch)
} else {
print("nope")
}
}
}
When you declare evnts, you've initialised it to an empty array ([Evnt]()).
When you access it from getCurrentTimelineEntryForComplication(complication: withHandler:), if nothing has modified the array, it will still be empty.
Inside session(session:didReceiveUserInfo:), you add items to the array, then immediately call doTable(), at which point ExtensionDelegate.evnts is not empty, as it contains the items you added just moments previously.
Given that you have no items when getCurrentTimelineEntryForComplication(complication: withHandler:) is being called, it would appear that this is happening before session(session:didReceiveUserInfo:) occurs.
If you want to make sure that you have data when getCurrentTimelineEntryForComplication(complication: withHandler:) is called, you should load some data before or at that point in the WatchKit application lifecycle.