Curtis Boylan
Curtis Boylan

Reputation: 827

iOS NSURL String

I am trying to place strings inside my NSURL, I have tried most suggestions but all seem to fail, i'm trying to find a easy way to do it instead of having to use some long request with long code. I'm wondering is there anything small here i'm forgetting?

NSString *lat = @"53.350628";
NSString *lng = @"-6.254997";
NSURL *blogURL = [NSURL URLWithString:@"http://url.com/jsontest.php?lat=%@&lng=%@&max=5", lat, lng];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

Thanks,

Curtis

Upvotes: 0

Views: 185

Answers (2)

Bobby W
Bobby W

Reputation: 824

Format the string, than add that to the NSURL

NSString *lat = @"53.350628";
NSString *lng = @"-6.254997";

//Format String
NSString *urlString = [NSString stringWithFormat:@"http://url.com/jsontest.php?lat=%@&lng=%@&max=5", lat, lng];

NSURL *blogURL = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

Upvotes: 0

TylerP
TylerP

Reputation: 9829

+[NSURL URLWithStirng:] does not take a format string and a variable argument list. It just takes a string, so you should use something like +[NSString stringWithFormat:] first before passing it to URLWithString, like this:

NSURL *blogURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://url.com/jsontest.php?lat=%@&lng=%@&max=5", lat, lng]];

Upvotes: 2

Related Questions