Last active
August 29, 2015 14:20
-
-
Save fabnoe/f438b9f0c3467a309240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) { | |
// Begin background task, recommended by apple so your app doesnt get terminated by the system for taking too long to complete the network task | |
var identifier : UIBackgroundTaskIdentifier! | |
identifier = UIApplication.sharedApplication().beginBackgroundTaskWithName("Hello", expirationHandler: { () -> Void in | |
UIApplication.sharedApplication().endBackgroundTask(identifier) | |
identifier = UIBackgroundTaskInvalid | |
}) | |
let request = userInfo!["request"] as! String | |
if(request == "getRandomLocation"){ | |
// Get GPS Location | |
var locationManager = LocationManager.sharedInstance | |
locationManager.showVerboseMessage = true | |
locationManager.autoUpdate = false | |
locationManager.startUpdatingLocationWithCompletionHandler { (latitude, longitude, status, verboseMessage, error) -> () in | |
if (error != nil) { | |
self.returnErrorMessage(reply, errorMessage: "No GPS location found. Turn on location services on your iPhone.") | |
UIApplication.sharedApplication().endBackgroundTask(identifier) | |
identifier = UIBackgroundTaskInvalid | |
return | |
} | |
// Call my API | |
var placeManager = PlaceManager() | |
placeManager.loadRandomLoaction(latitude, longitude: longitude) { ( place:Place?, error:String?) -> () in | |
if (error != nil) { | |
self.returnErrorMessage(reply, errorMessage: "Ohhps. We could not find any location near your.") | |
UIApplication.sharedApplication().endBackgroundTask(identifier) | |
identifier = UIBackgroundTaskInvalid | |
return | |
} | |
if (place != nil) { | |
var simplePlace = SimplePlace() | |
simplePlace.name = place?.name | |
simplePlace.category = place?.category | |
simplePlace.distance = place?.distance | |
simplePlace.longitude = place?.location.longitude | |
simplePlace.latitude = place?.location.latitude | |
simplePlace.image_url = place?.image_url | |
simplePlace.small_image_url = place?.small_blurred_image_url | |
simplePlace.small_blurred_image_url = place?.small_blurred_image_url | |
var returnResult : [String:AnyObject] = ["response":"place","data":NSKeyedArchiver.archivedDataWithRootObject(simplePlace)] | |
reply(returnResult) | |
UIApplication.sharedApplication().endBackgroundTask(identifier) | |
identifier = UIBackgroundTaskInvalid | |
return | |
} | |
} | |
} | |
} else { | |
self.returnErrorMessage(reply, errorMessage: "Ohhps. Something went terrible wrong.") | |
UIApplication.sharedApplication().endBackgroundTask(identifier) | |
identifier = UIBackgroundTaskInvalid | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@see https://github.com/varshylmobile/LocationManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WKInterfaceController.openParentApplication(["request": "getRandomLocation"], | |
reply: { (replyInfo, error) -> Void in | |
self.hideLoader() | |
let response = replyInfo["response"] as! String | |
if (response == "place") { | |
let data = replyInfo["data"] as! NSData | |
let place = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! SimplePlace | |
self.pushControllerWithName(WatchStoryboard.InterfaceControllerIdentifiers.placeViewController, context: place) | |
} else /* if (response == "error") */ { | |
self.pushControllerWithName(WatchStoryboard.InterfaceControllerIdentifiers.alertViewController, context: replyInfo["message"]) | |
return | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment