2

I want to use this but need a solution for this..

NSString *googleAddress = @"http://maps.google.com?q=";
googleAddress = [googleAddress stringByAppendingString:self.address];
googleAddress = [googleAddress stringByAppendingString:@"+"];
googleAddress = [googleAddress stringByAppendingString:self.city];
googleAddress = [googleAddress stringByAppendingString:@",+"];
googleAddress = [googleAddress stringByAppendingString:self.state];
googleAddress = [googleAddress stringByAppendingString:@"&t=h"];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleAddress]];

I need to replace all the spaces in the address, city and state values with plus signs to get google maps to work.

Thanks

Mike Martin
  • 453
  • 5
  • 13

4 Answers4

4
googleAddress = [googleAddress stringByReplacingOccurancesOfString:@" " withString:@"+"];
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60
  • googleAddress = [googleAddress stringByReplacingOccurrencesOfString:@" " withString:@"+"]; Thanks Brad. This is a good stop gap solution. I can come back to this code later tomorrow. Watch the spelling... There is no "a" and a second "r" is in occurrences. Have a great night. – Mike Martin Dec 11 '10 at 08:34
  • This is a good temporary solution (and it is explicitly and correctly answering the question), but it is subtly the wrong answer. The format of a URL is a bit more involved than simply replace " " with "+". – Dave DeLong Dec 11 '10 at 09:01
  • True. I just used it as proof of concept. Now I can show the Marketing guy how it's supposed to work before fixing it the right way. Thanks again Dave. – Mike Martin Dec 11 '10 at 09:06
3

I would take a different approach. Here's how I'd do it:

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:@"&"];
NSString * googleString = [NSString stringWithFormat:@"http://maps.google.com?%@", queryString];
NSURL * googleURL = [NSURL URLWithString:googleString];
[[UIApplication sharedApplication] openURL:googleURL];

-URLEncodedString_ch can be found here

Why is this better? There are several reasons:

  1. 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?
  2. 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 & or =? It would be unusual for an address, but not impossible (especially the & in a street name).
  3. 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 @"UTF-8" and @"oe" object and key to the dictionary for inclusion in the query string.
  4. The percent encoding (if you use the category method linked to above) is more accurate than stringByAddingPercentEscapesUsingEncoding:
Community
  • 1
  • 1
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Thanks Dave. This is beautiful code. I added the ,nil after @"t" on line 2. However I'm getting "warning: 'NSString' may not respond to '-URLEncodedString_ch'" – Mike Martin Dec 11 '10 at 08:14
  • @Mike whoops! Thanks for catching the missing sentinel. :) As for the warning, you need to create an `NSString` category and declare the method in the .h file, then copy the implementation in the other post into the .m file, then `#import` the .h file into wherever you're going to be using the method. – Dave DeLong Dec 11 '10 at 08:17
  • Sorry I did copy the NSString but forgot to declare it in the .h file. I'll try that now. – Mike Martin Dec 11 '10 at 08:21
  • Hi Dave I'm interested in your approach but am wondering if line 1 & 2 would actually do what they are supposed to? If you define a NSString q and then use it as a object when creating the dictionary on the second line, wouldn't it treat the whole string as one object then you will get key q = "address, city, state" and key t = @"h"? – Rog Dec 11 '10 at 08:27
  • 1
    Current language: auto; currently objective-c 2010-12-11 03:25:23.624 Santas_List[29726:207] -[NSCFString URLEncodedString_ch]: unrecognized selector sent to instance 0x5c35990 2010-12-11 03:25:23.625 Santas_List[29726:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString URLEncodedString_ch]: unrecognized selector sent to instance 0x5c35990' – Mike Martin Dec 11 '10 at 08:27
  • @Rog The original question is treating the entire address (`address+city,+state`) as the value of the key `q`. – Dave DeLong Dec 11 '10 at 08:31
  • @Mike Read up on how to use a category: http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1 It's for adding a method to `NSString`, not to whatever class this code is in. – Dave DeLong Dec 11 '10 at 08:32
  • Thanks Dave I will print it off and read it while I'm getting ready for a nap. – Mike Martin Dec 11 '10 at 08:43
  • whats the deail with the invalidargumentexception... i do have the same "problem" – cV2 Mar 14 '11 at 17:53
  • @cV2: make sure you're linking in the category into your file. If you're building this to use in a static library, make sure you're using the appropriate linker flags. – Dave DeLong Mar 14 '11 at 17:55
0

Have a look at NSString's stringByReplacingOccurrencesOfString:withString: and NSMutableString's replaceOccurrencesOfString:withString:options:range:.

Costique
  • 23,712
  • 4
  • 76
  • 79
0

This snippet may be of use to some people, especially since Google treats + and %20 the same.

NSString *escapedUrlString =
   [unescapedString stringByAddingPercentEscapesUsingEncoding:
                    NSASCIIStringEncoding];

Source: http://blog.evandavey.com/2009/01/how-to-url-encode-nsstring-in-objective-c.html

Nathan C.
  • 319
  • 3
  • 8