Search code examples
swiftgoogle-mapssharensurl

Swift: NSURL with parameters is invalid


This is my code to share a Google Maps link with a UIActivityVieController:

 if let myWebsite = NSURL(string: "http://maps.google.com/?q=<\(myLatitude)>,<\(myLongitude)>") {
        let objectsToShare = [alertMessage, myWebsite]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        //New Excluded Activities Code
        activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
        self.presentViewController(activityVC, animated: true, completion: nil)
    }

This link is not valid though; the if statement is not called. How can I fix this?


Solution

  • The format of your NSURL should look like this:

    let myWebsite = NSURL(string: "http://maps.google.com/?q=\(myLatitude),\(myLongitude)")
    

    To use a string inside a string you should write

    "My string \(otherString)";