Skip to content

Commit

Permalink
Wired the activity continuation code in, it all works spiffingly well
Browse files Browse the repository at this point in the history
The region is extracted from the activity and the map updated to display
it as appropriate.

Ready for writeup
  • Loading branch information
Sam Davies committed Nov 24, 2014
1 parent 6cfcb0b commit c7b055e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion 38-handoff/MapOff/MapOff/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool {
println("Continuing...")
if let rootVC = window?.rootViewController {
restorationHandler([rootVC])
}
return true
}

Expand Down
16 changes: 15 additions & 1 deletion 38-handoff/MapOff/MapOff/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import MapKit
class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
let activityType = "com.shinobicontrols.MapOff.viewport"

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let activityType = "com.shinobicontrols.MapOff.viewport"

if userActivity?.activityType != activityType {
userActivity?.invalidate()
userActivity = NSUserActivity(activityType: activityType)
Expand All @@ -34,11 +35,24 @@ class ViewController: UIViewController, MKMapViewDelegate {
mapView.delegate = self
}

// MARK:- UIResponder Activity Handling
override func updateUserActivityState(activity: NSUserActivity) {
let regionData = NSData(bytes: &mapView.region, length: sizeof(MKCoordinateRegion))
activity.userInfo = ["region" : regionData]
}

override func restoreUserActivityState(activity: NSUserActivity) {
if activity.activityType == activityType {
// Extract the data
let regionData = activity.userInfo!["region"] as NSData
// Need an empty coordinate region to populate
var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0),
span: MKCoordinateSpan(latitudeDelta: 0.0, longitudeDelta: 0.0))
regionData.getBytes(&region, length: sizeof(MKCoordinateRegion))
mapView.setRegion(region, animated: true)
}
}

// MARK:- MKMapViewDelegate
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
userActivity?.needsSave = true
Expand Down

0 comments on commit c7b055e

Please sign in to comment.