Vincent
Vincent

Reputation: 1645

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?

Upvotes: 0

Views: 111

Answers (1)

Nicklas Ridewing
Nicklas Ridewing

Reputation: 2451

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)";

Upvotes: 1

Related Questions