Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would take a different approach. Here's how I'd do it:</p> <pre><code>NSString * q = [NSString stringWithFormat:@"%@ %@, %@", self.address, self.city, self.state]; NSDictionary * queryDictionary = [NSDictionary dictionaryWithObjectsAndKeys:q, @"q", @"h", @"t", nil]; NSMutableArray * fields = [NSMutableArray array]; for (NSString * key in queryDictionary) { NSString * value = [queryDictionary objectForKey:key]; NSString * encoded = [NSString stringWithFormat:@"%@=%@", [key URLEncodedString_ch], [value URLEncodedString_ch]]; [fields addObject:encoded]; } NSString * queryString = [fields componentsJoinedByString:@"&amp;"]; NSString * googleString = [NSString stringWithFormat:@"http://maps.google.com?%@", queryString]; NSURL * googleURL = [NSURL URLWithString:googleString]; [[UIApplication sharedApplication] openURL:googleURL]; </code></pre> <p><code>-URLEncodedString_ch</code> can be found <a href="https://stackoverflow.com/questions/3423545/objective-c-iphone-percent-encode-a-string/3426140#3426140">here</a></p> <p>Why is this better? There are several reasons:</p> <ol> <li>The keys in a query string should be URL encoded. Granted that right now they're just one letter that's in the ASCII set, but can you guarantee that they'll always be?</li> <li>The values in a query string should be URL encoded. Right now you're only trying to plus-encode the spaces. What if your address contains an <code>&amp;</code> or <code>=</code>? It would be unusual for an address, but not impossible (especially the <code>&amp;</code> in a street name).</li> <li>This is highly extensible. If you decide to add support for foreign address and need more than a simple ASCII address, it's rather trivial to add the <code>@"UTF-8"</code> and <code>@"oe"</code> object and key to the dictionary for inclusion in the query string.</li> <li>The percent encoding (if you use the category method linked to above) is more accurate than <code>stringByAddingPercentEscapesUsingEncoding:</code></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload