Scott Scott - 3 months ago 9
Swift Question

Xcode Swift - Spacing for Apple's build-in maps

I'm new to Xcode and swift.

When the user types in their address in the

textFields
and then goes to press the 'ViewOnMap'
Button
,(Redirect the user to Apple's built-in maps) it makes their address search into one line without any spacing. This result obviously returns with 'No location Found'.

I'm pretty sure its because there are no spacing?

I've tried looking at other StackOverflows solved questions on how to add spacing which i've done below:

code:

@IBAction func btnViewOnMap(sender: AnyObject) {

var urlString: String?

let stringURLSearch = "http://maps.apple.com/?q=\(urlString)" + self.textFieldAddress1!.text! + self.textFieldAddress2!.text! + self.textFieldAddress3!.text! + self.textFieldTownCity!.text! + self.textFieldCounty!.text! + self.textFieldPostcode!.text!
urlString = stringURLSearch.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
UIApplication.sharedApplication().openURL(NSURL(string: stringURLSearch)!)
}


As you can see the 'urlString' is what others have been saying to put, so that it adds the '%20' but it keeps returning
nil
.

Thanks in advance!

Answer

Have You try to use encoded urlString instead of stringURLSearch with NSURL like this

let stringURLSearch = "http://maps.apple.com/?q=" + self.textFieldAddress1!.text! + self.textFieldAddress2!.text! + self.textFieldAddress3!.text! + self.textFieldTownCity!.text! + self.textFieldCounty!.text! + self.textFieldPostcode!.text!
urlString = stringURLSearch.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)