0

Hello guys I am working with MKMap and populating some pins into a map everything is good with that in the pin I can show name and last name + when I click the pin shows an URL but the idea is when I tap the URL to open the web browser and redirect to the URL and for some reason is not working.

Here is a screenshot of my method that I use to open my url:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        if let toOpen = view.annotation?.subtitle! {
            if let url = URL(string: toOpen) {
                if #available(iOS 10, *) {
                    UIApplication.shared.open(url, options: [:],
                                              completionHandler: {
                                                (success) in
                                                print("Open \(url): \(success)")
                    })
                } else {
                    _ = UIApplication.shared.openURL(url)
                }
            }
        }
    }
}

this is my method where I define the pin properties, a weird thing is that I am giving my pin a color blue but still show it in red.

private func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.pinTintColor = UIColor.blue
        pinView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    } else {
        pinView!.annotation = annotation
    }

    return pinView

}

here is my Github project. I would like to know what I am doing wrong because I can tap my pin shows the information but it doesn't open the URL.

Thanks for your time guys!

Felipe Valdivia
  • 369
  • 3
  • 18
  • How are we supposed to test the app if it requires a login? :( – Nerdy Bunz Apr 09 '18 at 00:42
  • what happens if you change the root view controller to the map? sorry I forgot that we need to use our Udacity user to login in. :/ that's the required for the project – Felipe Valdivia Apr 09 '18 at 00:48
  • Please post code as text, not as pictures. Pictures can't be referenced, tested, searched, and pictures are harder to read. – rmaddy Apr 09 '18 at 00:51
  • I changed the pictures and use code instead, sorry about the login thing is requirement for this project use Udacity user name and password :/ – Felipe Valdivia Apr 09 '18 at 01:09

2 Answers2

0

Maybe your viewForAnnotation method is not called. Replace

private func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

with

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
0

I solved it.

I realized that my mapView functions weren't working at all. I comment the code and move stuff and those two functions weren't doing anything.

So after awhile watching videos and reading, I found that I need it to use the mapView delegate to the controller itself, so I could use my mapView functions and just adding this line of code:

override func viewDidLoad() {
    super.viewDidLoad()
    //MIRACLE HAPPENS HERE
    mapView.delegate = self
}

Just with that line of code in my viewDidLoad function, all my code inside the mapView function started working.

Pang
  • 9,564
  • 146
  • 81
  • 122
Felipe Valdivia
  • 369
  • 3
  • 18