From 8b37acd82b0fb804377980db1ae7fa41bba04142 Mon Sep 17 00:00:00 2001 From: Yosra Nagati Date: Tue, 6 Nov 2018 14:09:10 +0200 Subject: [PATCH 1/2] starting point --- MapKitTutorial.xcodeproj/project.pbxproj | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/MapKitTutorial.xcodeproj/project.pbxproj b/MapKitTutorial.xcodeproj/project.pbxproj index c8db51c..c33ae23 100644 --- a/MapKitTutorial.xcodeproj/project.pbxproj +++ b/MapKitTutorial.xcodeproj/project.pbxproj @@ -254,10 +254,11 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = MapKitTutorial/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.thorntech.MapKitTutorial; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 4.2; }; name = Debug; }; @@ -266,10 +267,11 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = MapKitTutorial/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.thorntech.MapKitTutorial; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 4.2; }; name = Release; }; From 20ed5ff3f02f86ac3765cbbafd5ff70e1d33fec0 Mon Sep 17 00:00:00 2001 From: Yosra Nagati Date: Tue, 6 Nov 2018 14:40:09 +0200 Subject: [PATCH 2/2] upgrade to swift 4 --- MapKitTutorial.xcodeproj/project.pbxproj | 7 +++-- MapKitTutorial/AppDelegate.swift | 32 ++++++++++---------- MapKitTutorial/Info.plist | 4 +-- MapKitTutorial/LocationSearchTable.swift | 25 ++++++++-------- MapKitTutorial/ViewController.swift | 37 +++++++++++++----------- 5 files changed, 57 insertions(+), 48 deletions(-) diff --git a/MapKitTutorial.xcodeproj/project.pbxproj b/MapKitTutorial.xcodeproj/project.pbxproj index c33ae23..eb3c12c 100644 --- a/MapKitTutorial.xcodeproj/project.pbxproj +++ b/MapKitTutorial.xcodeproj/project.pbxproj @@ -99,6 +99,7 @@ TargetAttributes = { FC1E24771C2B10F5009E9908 = { CreatedOnToolsVersion = 7.2; + DevelopmentTeam = 3NMBA2WU8N; }; }; }; @@ -253,10 +254,11 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 3NMBA2WU8N; INFOPLIST_FILE = MapKitTutorial/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.thorntech.MapKitTutorial; + PRODUCT_BUNDLE_IDENTIFIER = yosra.MapKitTutorial; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.2; }; @@ -266,10 +268,11 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 3NMBA2WU8N; INFOPLIST_FILE = MapKitTutorial/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = com.thorntech.MapKitTutorial; + PRODUCT_BUNDLE_IDENTIFIER = yosra.MapKitTutorial; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 4.2; }; diff --git a/MapKitTutorial/AppDelegate.swift b/MapKitTutorial/AppDelegate.swift index 453423c..e1edec1 100644 --- a/MapKitTutorial/AppDelegate.swift +++ b/MapKitTutorial/AppDelegate.swift @@ -12,33 +12,35 @@ import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - - - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. 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. + // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. 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) { - // 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 applicationWillEnterForeground(_ application: UIApplication) { + // Called as part of the transition from the background to the active 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:. + // Saves changes in the application's managed object context before the application terminates. + } diff --git a/MapKitTutorial/Info.plist b/MapKitTutorial/Info.plist index f2c178e..5b03e74 100644 --- a/MapKitTutorial/Info.plist +++ b/MapKitTutorial/Info.plist @@ -2,8 +2,6 @@ - NSLocationWhenInUseUsageDescription - This tutorial wants to use your location CFBundleDevelopmentRegion en CFBundleExecutable @@ -24,6 +22,8 @@ 1 LSRequiresIPhoneOS + NSLocationWhenInUseUsageDescription + This tutorial wants to use your location UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/MapKitTutorial/LocationSearchTable.swift b/MapKitTutorial/LocationSearchTable.swift index 413c87a..c66b922 100644 --- a/MapKitTutorial/LocationSearchTable.swift +++ b/MapKitTutorial/LocationSearchTable.swift @@ -52,17 +52,17 @@ class LocationSearchTable: UITableViewController { } extension LocationSearchTable : UISearchResultsUpdating { - - func updateSearchResultsForSearchController(searchController: UISearchController) { + func updateSearchResults(for searchController: UISearchController) { + guard let mapView = mapView, let searchBarText = searchController.searchBar.text else { return } - let request = MKLocalSearchRequest() + let request = MKLocalSearch.Request() request.naturalLanguageQuery = searchBarText request.region = mapView.region let search = MKLocalSearch(request: request) - search.startWithCompletionHandler { response, _ in + search.start { response, _ in guard let response = response else { return } @@ -76,15 +76,16 @@ extension LocationSearchTable : UISearchResultsUpdating { extension LocationSearchTable { - override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return matchingItems.count } - override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCellWithIdentifier("cell")! + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let selectedItem = matchingItems[indexPath.row].placemark + //let selectedItem = matchingItems[indexPath.row] cell.textLabel?.text = selectedItem.name - cell.detailTextLabel?.text = parseAddress(selectedItem) + cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem) return cell } @@ -92,10 +93,10 @@ extension LocationSearchTable { extension LocationSearchTable { - override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let selectedItem = matchingItems[indexPath.row].placemark - handleMapSearchDelegate?.dropPinZoomIn(selectedItem) - dismissViewControllerAnimated(true, completion: nil) + handleMapSearchDelegate?.dropPinZoomIn(placemark: selectedItem) + dismiss(animated: true, completion: nil) } -} \ No newline at end of file +} diff --git a/MapKitTutorial/ViewController.swift b/MapKitTutorial/ViewController.swift index e54adeb..cd39c6a 100644 --- a/MapKitTutorial/ViewController.swift +++ b/MapKitTutorial/ViewController.swift @@ -30,7 +30,7 @@ class ViewController: UIViewController { locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.requestLocation() - let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable + let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable resultSearchController = UISearchController(searchResultsController: locationSearchTable) resultSearchController.searchResultsUpdater = locationSearchTable let searchBar = resultSearchController!.searchBar @@ -45,30 +45,33 @@ class ViewController: UIViewController { } - func getDirections(){ + @objc func getDirections(){ guard let selectedPin = selectedPin else { return } let mapItem = MKMapItem(placemark: selectedPin) let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] - mapItem.openInMapsWithLaunchOptions(launchOptions) + mapItem.openInMaps(launchOptions: launchOptions) } } extension ViewController : CLLocationManagerDelegate { - func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { - if status == .AuthorizedWhenInUse { + func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { + + if status == .authorizedWhenInUse { locationManager.requestLocation() } } - func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + guard let location = locations.first else { return } - let span = MKCoordinateSpanMake(0.05, 0.05) + let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) let region = MKCoordinateRegion(center: location.coordinate, span: span) mapView.setRegion(region, animated: true) } - func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { + func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { + print("error:: \(error)") } @@ -91,29 +94,29 @@ extension ViewController: HandleMapSearch { } mapView.addAnnotation(annotation) - let span = MKCoordinateSpanMake(0.05, 0.05) - let region = MKCoordinateRegionMake(placemark.coordinate, span) + let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) + let region = MKCoordinateRegion(center: placemark.coordinate, span: span) mapView.setRegion(region, animated: true) } } extension ViewController : MKMapViewDelegate { - - func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{ + func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { + guard !(annotation is MKUserLocation) else { return nil } let reuseId = "pin" - var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView + var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) } - pinView?.pinTintColor = UIColor.orangeColor() + pinView?.pinTintColor = UIColor.orange pinView?.canShowCallout = true let smallSquare = CGSize(width: 30, height: 30) - let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare)) - button.setBackgroundImage(UIImage(named: "car"), forState: .Normal) - button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside) + let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare)) + button.setBackgroundImage(UIImage(named: "car"), for: .normal) + button.addTarget(self, action: #selector(getDirections), for: .touchUpInside) pinView?.leftCalloutAccessoryView = button return pinView