Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 3 and 4 migration #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Example/TVSwiftMeasurementProtocol.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,16 @@
TargetAttributes = {
63FDDF041C043AA400A6E364 = {
CreatedOnToolsVersion = 7.1;
DevelopmentTeam = LRYVNX57AP;
LastSwiftMigration = 0830;
};
63FDDF151C043AA400A6E364 = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0830;
TestTargetID = 63FDDF041C043AA400A6E364;
};
63FDDF201C043AA400A6E364 = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0830;
TestTargetID = 63FDDF041C043AA400A6E364;
};
};
Expand Down Expand Up @@ -348,6 +350,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = appletvos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
Expand Down Expand Up @@ -384,6 +387,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = appletvos;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VALIDATE_PRODUCT = YES;
Expand All @@ -396,6 +400,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image";
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = TVSwiftMeasurementProtocol/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.analyticspros.TVSwiftMeasurementProtocol;
Expand All @@ -409,6 +414,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image";
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = TVSwiftMeasurementProtocol/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.analyticspros.TVSwiftMeasurementProtocol;
Expand Down
14 changes: 7 additions & 7 deletions Example/TVSwiftMeasurementProtocol/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
GATracker.setup("UA-1234567-89")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
_ = GATracker.setup("UA-1234567-89")
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
49 changes: 23 additions & 26 deletions Example/TVSwiftMeasurementProtocol/GATracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

private var _analyticsTracker: GATracker!
private var _analyticsTracker: GATracker! = nil

class GATracker {
/*
Expand All @@ -20,7 +20,7 @@ class GATracker {
@MPVersion = Measurement Protocol version
@ua = User Agent string
*/
private var tid : String
fileprivate var tid : String
var cid: String
var appName : String
var appVersion : String
Expand All @@ -29,11 +29,8 @@ class GATracker {
var ul : String

//Set up singleton object for the tracker
class func setup(tid: String) -> GATracker {
struct Static {
static var onceToken: dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
class func setup(_ tid: String) -> GATracker {
if _analyticsTracker == nil {
_analyticsTracker = GATracker(tid: tid)
}
return _analyticsTracker
Expand All @@ -58,29 +55,28 @@ class GATracker {
#endif

self.tid = tid
self.appName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]
self.appName = Bundle.main.infoDictionary!["CFBundleName"] as! String
let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as AnyObject
self.appVersion = nsObject as! String
self.ua = "Mozilla/5.0 (Apple TV; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13T534YI"
self.MPVersion = "1"
let defaults = NSUserDefaults.standardUserDefaults()
if let cid = defaults.stringForKey("cid") {
let defaults = UserDefaults.standard
if let cid = defaults.string(forKey: "cid") {
self.cid = cid
}
else {
self.cid = NSUUID().UUIDString
defaults.setObject(self.cid, forKey: "cid")
self.cid = UUID().uuidString
defaults.set(self.cid, forKey: "cid")
}

let language = NSLocale.preferredLanguages().first
if language?.characters.count > 0 {
self.ul = language!
if let language = Locale.preferredLanguages.first, language.characters.count > 0 {
self.ul = language
} else {
self.ul = "(not set)"
}
}

func send(type: String, params: Dictionary<String, String>) {
func send(_ type: String, params: Dictionary<String, String>) {
/*
Generic hit sender to Measurement Protocol
Consists out of hit type and a dictionary of other parameters
Expand All @@ -93,16 +89,16 @@ class GATracker {
}

//Encoding all the parameters
if let paramEndcode = parameters.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet()){
if let paramEndcode = parameters.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed){
let urlString = endpoint + paramEndcode;
let url = NSURL(string: urlString);
let url = URL(string: urlString);

#if DEBUG
print(urlString)
#endif

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
if let httpReponse = response as? NSHTTPURLResponse {
let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in
if let httpReponse = response as? HTTPURLResponse {
let statusCode = httpReponse.statusCode
#if DEBUG
print(statusCode)
Expand All @@ -115,12 +111,12 @@ class GATracker {
#endif
}
}
}
})
task.resume()
}
}

func screenView(screenName: String, customParameters: Dictionary<String, String>?) {
func screenView(_ screenName: String, customParameters: Dictionary<String, String>?) {
/*
A screenview hit, use screenname
*/
Expand All @@ -133,7 +129,8 @@ class GATracker {
self.send("screenview", params: params)
}

func event(category: String, action: String, var label: String?, customParameters: Dictionary<String, String>?) {
func event(_ category: String, action: String, label: String?, customParameters: Dictionary<String, String>?) {
var label = label
/*
An event hit with category, action, label
*/
Expand All @@ -151,7 +148,7 @@ class GATracker {
self.send("event", params: params)
}

func exception(description: String, isFatal:Bool, customParameters: Dictionary<String, String>?) {
func exception(_ description: String, isFatal:Bool, customParameters: Dictionary<String, String>?) {
/*
An exception hit with exception description (exd) and "fatality" (Crashed or not) (exf)
*/
Expand All @@ -169,4 +166,4 @@ class GATracker {
self.send("exception", params: params)

}
}
}
4 changes: 2 additions & 2 deletions Example/TVSwiftMeasurementProtocol/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class SecondViewController: UIViewController {

}

override func viewDidAppear(animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
GATracker.sharedInstance.screenView("SecondScreen", customParameters: ["cd1": "Seattle", "cm2": "2"])
}

@IBAction func exceptionFIred(sender: AnyObject) {
@IBAction func exceptionFIred(_ sender: AnyObject) {
print("Exception Fired")
GATracker.sharedInstance.send("transaction", params: ["tid":"10001", "tr":"425,00", "cu":"USD"])
GATracker.sharedInstance.exception("This test failed", isFatal: true, customParameters: nil)
Expand Down
8 changes: 4 additions & 4 deletions Example/TVSwiftMeasurementProtocol/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ class ViewController: UIViewController {

}

override func viewDidAppear(animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
GATracker.sharedInstance.screenView("FirstScreen", customParameters: nil)
}

@IBAction func fireEvent(sender: AnyObject) {
@IBAction func fireEvent(_ sender: AnyObject) {
print("Fire Event")
GATracker.sharedInstance.event("a", action: "b", label: nil, customParameters: nil)
}

@IBAction func nextScreenPressed(sender: AnyObject) {
self.performSegueWithIdentifier("toSecondViewController", sender: nil)
@IBAction func nextScreenPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "toSecondViewController", sender: nil)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TVSwiftMeasurementProtocolTests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
self.measure {
// Put the code you want to measure the time of here.
}
}
Expand Down
Loading