|
| 1 | +// |
| 2 | +// ViewController.swift |
| 3 | +// coding-events iOS |
| 4 | +// |
| 5 | +// Created by Goran Blažič on 24/09/14. |
| 6 | +// Copyright (c) 2014 goranche.net. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import MapKit |
| 11 | +import CoreLocation |
| 12 | + |
| 13 | +class ViewController: UIViewController, NSURLConnectionDelegate, MKMapViewDelegate { |
| 14 | + |
| 15 | + lazy var data = NSMutableData() |
| 16 | + @IBOutlet weak var mapView: MKMapView! |
| 17 | + |
| 18 | + override func viewDidLoad() { |
| 19 | + super.viewDidLoad() |
| 20 | + } |
| 21 | + |
| 22 | + override func viewWillAppear(animated: Bool) { |
| 23 | + super.viewWillAppear(animated) |
| 24 | + startConnection() |
| 25 | + } |
| 26 | + |
| 27 | + override func viewDidAppear(animated: Bool) { |
| 28 | + var locMgr = CLLocationManager() |
| 29 | + var auth = CLLocationManager.authorizationStatus() |
| 30 | + if (auth == CLAuthorizationStatus.NotDetermined) { |
| 31 | + locMgr.requestAlwaysAuthorization() |
| 32 | + // TODO: This no worky!!! |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + override func didReceiveMemoryWarning() { |
| 37 | + super.didReceiveMemoryWarning() |
| 38 | + } |
| 39 | + |
| 40 | + func startConnection() { |
| 41 | + let urlPath: String = "http://events.codeweek.eu/api/event/list/?format=json" |
| 42 | + var url: NSURL = NSURL(string: urlPath) |
| 43 | + var request: NSURLRequest = NSURLRequest(URL: url) |
| 44 | + var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false) |
| 45 | + connection.start() |
| 46 | + } |
| 47 | + |
| 48 | + func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ |
| 49 | + self.data.appendData(data) |
| 50 | + } |
| 51 | + |
| 52 | + func connectionDidFinishLoading(connection: NSURLConnection!) { |
| 53 | + var err: NSError? |
| 54 | + |
| 55 | + var events = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &err) as Array<NSDictionary> |
| 56 | + |
| 57 | + // TODO: Show the markers from the events array |
| 58 | + // each entry is a NSDictionary, use the "geoposition" field for the markers |
| 59 | + } |
| 60 | + |
| 61 | + func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) { |
| 62 | + var region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800) |
| 63 | + self.mapView.setRegion(region, animated: true) |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments