问题
Cocoapods is not ready for Swift yet. When I implemented Mixpanel into my iOS project that uses Swift, I got several errors.
How to fix this issue and make mixpanel to work?
回答1:
Add this line of code to every file that is giving an error
#import <UIKit/UIKit.h>
Also need to add these in your linked frameworks and libraries:
libicucore.dylib
CFNetwork.framework
Security.framework
Then add this to your Bridging-Header.h file:
#import "Mixpanel.h"
Then add this piece of code in your appdelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//other stuff here
Mixpanel.sharedInstanceWithToken("yourtokennumbergoeshere")
var mixPanel = Mixpanel.sharedInstance()
mixPanel.track("Finished Launching", properties: ["name1":"property1","name2":"property2"])
return true
}
回答2:
Change your #import "mixpanel.h"
to #import "Mixpanel/Mixpanel.h"
回答3:
Use this unofficial Swift client! Probably not as good as the real thing, but it will do for basic tracking. https://github.com/soffes/Mixpanel
回答4:
Getting A/B Testing to work in Swift is not trivial as the macro is not supported. Feel free to use this gist :
https://gist.github.com/ohad7/8ce12a432773fe3b1bf3
import Foundation
import Mixpanel
public class Tweaks : NSObject, MPTweakObserver {
public static let sharedInstance = Tweaks(tweaks: [ ExecuteDiskCleanup])
public static let ExecuteDiskCleanup = Tweak.Booln(name: "Cleanup-Disk", defaultValue:false)
private var values = [String:AnyObject]()
private let tweaks: [Tweak]
public init(tweaks: [Tweak]) {
self.tweaks = tweaks
let defaults = NSUserDefaults.standardUserDefaults()
for tweak in tweaks {
if let value: AnyObject = defaults.objectForKey(Tweaks.generateKey(tweak.name)) {
self.values[tweak.name] = value
println("Initialized Tweak \(tweak.name) with value: \(value)")
}
}
}
public func setup() {
tweaks.foreach(){ tweak in
let theTweak = MPTweak(name: tweak.name, andEncoding: "UTF-8")
theTweak.defaultValue = tweak.defaultValue
theTweak.addObserver(self)
MPTweakStore.sharedInstance().addTweak(theTweak)
}
Mixpanel.sharedInstance().checkForVariantsOnActive = true
Mixpanel.sharedInstance().joinExperiments()
}
public func get(tweaks: [Tweak]) -> [String:AnyObject] {
var result = [String:AnyObject]()
synced(values) {
tweaks.foreach(){ tweak in
result[tweak.name] = self.values[tweak.name]
}
}
return result
}
public func tweakDidChange(tweak: MPTweak) {
println("tweakDidChange Tweak \(tweak.name) current value :\(tweak.currentValue)")
synced(self.values) {
let localTweak = self.tweaks.filter{ $0.name == tweak.name}.first
if let localTweak = localTweak{
switch (localTweak) {
case .Booln:
self.values[tweak.name] = Bool(tweak.currentValue as? Int == 1)
default:
self.values[tweak.name] = tweak.currentValue
}
println("Tweak \(localTweak.name) -> \(self.values[localTweak.name])")
NSUserDefaults.standardUserDefaults().setObject(self.values[localTweak.name], forKey: Tweaks.generateKey(localTweak.name))
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
private class func generateKey(key: String) -> String {
return "mp_tweak_\(key)"
}
}
public enum Tweak {
case Str(name: String, defaultValue: String)
case Booln(name: String, defaultValue: Bool)
case Integer(name: String, defaultValue: Int)
case Flt(name: String, defaultValue: Float)
var name : String {
switch (self) {
case let .Str(n, d): return n
case let .Booln(n, d): return n
case let .Integer(n, d): return n
case let .Flt(n, d): return n
}
}
var defaultValue : AnyObject {
switch (self) {
case let .Str(n, d): return d
case let .Booln(n, d): return d
case let .Integer(n, d): return d
case let .Flt(n, d): return d
}
}
public func get<T>() -> T? {
return get(self.defaultValue as? T)
}
public func get<T>(defaultValue: T?) -> T? {
var value = synced(Tweaks.sharedInstance.values){
return Tweaks.sharedInstance.values[self.name] as? T
}
return value != nil ? value : defaultValue
}
}
// Examples :
// public static let SomeIntegerZero = Tweak.Integer(name: "SomeIntegerZero", defaultValue:0)
// public static let SomeIntegerNonZero = Tweak.Integer(name: "SomeIntegerNonZero", defaultValue:6666)
// public static let SomeBooleanFalse = Tweak.Booln(name: "SomeBooleanFalse", defaultValue:false)
// public static let SomeBooleanTrue = Tweak.Booln(name: "SomeBooleanTrue", defaultValue:true)
// public static let SomeStringEmpty = Tweak.Str(name: "SomeEmptyString", defaultValue:"")
// public static let SomeStringFull = Tweak.Str(name: "SomeFullString", defaultValue:"full")
/*** Utilities - sync methods ***/
func synced<T>(lock: AnyObject, closure: () -> T) -> T {
objc_sync_enter(lock)
let result = closure()
objc_sync_exit(lock)
return result
}
func synced(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
回答5:
Looks like Mixpanel has posted some official documentation on this: https://mixpanel.com/blog/2015/08/17/community-tip-implement-mixpanel-in-swift-apps
I had trouble with podfiles and bridging-header imports, but the use_frameworks! suggested in the above link works perfectly for me.
来源:https://stackoverflow.com/questions/28016246/mixpanel-does-not-work-with-swift