CLLocationManager swift does´t work - ios

Im trying to do a simple app. Just a map with the users location. I search in here the solution, but I think that I have everything and still does´t work.
Here is the code:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var MapView: MKMapView = nil
var location : CLLocationManager! = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
location = CLLocationManager()
location.delegate = self
location.requestWhenInUseAuthorization()
location.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I added NSLoscationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription in info.plist. I activated Debug -> Location -> Custom Location in the iOS simulator.
I am not sure if I have to put something in AppDelegate.swift (It's my first app).
I will be very grateful if you can help me!!
Thanks. :)

Related

Could not cast value of type 'MKMapView' to 'MKOverlay'

I created a library in Xcode8 with a method used to add a map to MapKitView and I import that library to a sample project and I called the method in the library then I get an error called "Could not cast value of type 'MKMapView' (0xdc7a48) to 'MKOverlay' (0xdcca0c).
(lldb) "
the code in library
import Foundation
import MapKit
public class mapLib: NSObject{
public class func createMap(mapView: MKMapView) ->MKMapView{
let mapView = mapView
//custom map URL
let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
mapView.add(overlay, level: .aboveLabels)
return mapView;
}
}
the code used in the sample app
import UIKit
import MapKit
import mapLib
class ViewController: UIViewController {
#IBOutlet weak var mapV: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let view = mapLib.createMap(mapView: mapV)
mapV.add(view as! MKOverlay) /////// Thread 1: signal SIGABRT
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have commented the error in sample app code
MKOverlay is a protocol that MKMapView doesn't conform to by default, hence the error. Adding an MKMapView object to another MKMapView as an overlay simply cannot work, since MKMapView isn't just a simple overlay object.
If what you actually need is to add all overlays of one MKMapView to the other, you need to use below code:
let view = mapLib.createMap(mapView: mapV)
mapV.addOverlays(view.overlays)

Xcode 7.3 doesn't autocomplete when using MapKit

In Xcode 7.3 (using Swift) I've created the most simple map example:
Imported MapKit
Created an outlet called mapView
For some reason, there's no autocomplete with my outlet. Even when I type 'ma' it does not come up with the outlet name 'mapView'.
Can anyone help get autocomplete working?
Code from: ViewController.swift
import UIKit
import MapKit
class ViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// mapView doesnt appear when I begin to type it
// also it doesn't show any functions when I type mapView.
}
I've also uploaded my project to dropbox:
https://www.dropbox.com/sh/rgefwafhanti1wa/AABhQt8TsbvesaO9zg-SydlRa?dl=0
You also need to add the MapKit framework in Build Phases for the target.

Location GPS in WebView APP

I have a doubts acquiring location using the WebView. I created an application with a simple WebView to run on my website. The site has Google Maps showing the location of some places. Mac Safari asks to open GPS to calculate the route since the app WebView does not pull up my location to the map. I've already searched many forums and done many tests however, nothing works.
I use Xcode7.
import UIKit
import CoreLocation
class ViewController: UIViewController , CLLocationManagerDelegate {
#IBOutlet weak var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let URL = NSURL(string: "http://inibox.com.br")
WebView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Setting up CVCalendar via Cocoapods: Views Aren't Showing

I've been trying to set up the CocoaPods CVCalendar for my app, but after integrating it into my Xcode project, the content fails to show through the UIViews. Essentially, whenever I connect the views to the CVCalendarView and CVCalendarMenuView variables, the views simply do not show. Here's the ViewController code I have so far, and help would be greatly appreciated!
import UIKit
import CVCalendar
class ViewController: UIViewController, CVCalendarViewDelegate, CVCalendarMenuViewDelegate {
#IBOutlet weak var menuView: CVCalendarMenuView!
#IBOutlet weak var calendarView: CVCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
calendarView.commitCalendarViewUpdate()
menuView.commitMenuViewUpdate()
}
func presentationMode() -> CalendarMode {
return .MonthView
}
func firstWeekday() -> Weekday {
return .Sunday
}
}
Your ViewController class is the class in the storyboard?
Did you hook up the delegates? Delegate setup
You're delegating the rendering to your ViewController/
You need to drag to the yellow view controller nub.
If for some reason you'd like to setup CVCalendar manually you have to do the following steps.
Refer here

Location Data from CoreLocation swift

I'm trying to get the users coordinates, but I cant work out how to retrieve them? Here is my code for finding their location on a map, what should I add to get numerical values for longitude/latitude? It works fine, and I've added the requestAuthorization key into info.plist.I've tried using the CLLocation object, but I cant seem to get it to work.
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var Map: MKMapView!
var locationMgr = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationMgr.delegate = self
locationMgr = CLLocationManager()
locationMgr.requestAlwaysAuthorization()
locationMgr.startUpdatingLocation()
Map.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Implement the CLLocationManager delegate protocol methods, specifically:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
You've initialised the location manager twice. so the delegate you set after first time the manager initialised is nil. so delete this locationMgr = CLLocationManager() in your viewDidLoad method. it should work

Resources