Thomas rocks
Thomas rocks

Reputation: 111

How do i include a variable in a nsurl iphone

this is what i have

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    responseData = [[NSMutableData data] retain];
    results = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=38.69747988499999,-121.3570182875&radius=9900&types=bar&sensor=false&key=fake_key"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    return YES;

}

and with the url i would like to have a variable in there for the location. basically my app is getting the location and i would like to implement this into the url so i can find the bars that are around.... how would i do this

i was thinking

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    responseData = [[NSMutableData data] retain];
    results = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@,%@&radius=9900&types=bar&sensor=false&key=fake_key", longitude,latitude]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    return YES;

}

but it didnt work

please help

Upvotes: 0

Views: 524

Answers (2)

Tony
Tony

Reputation: 4609

So I am seeing a lot of people using NSURL and I believe that if they were to look at an ASIFormDataRequest they would find that it is much safer and much easier to use. In order to use an ASIFormDataRequest you must download it from here http://allseeing-i.com/ASIHTTPRequest/How-to-use However once that is downloaded it makes life much easier. The reason I point this out is because it forces everything to conform to a protocol so you would know if you are having issues because of an %f or an %@ because it forces you to use a string. It make it a lot easier for debugging. Here is some example code of how to use an ASIFormData Request

NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json"];
ASIFormDataRequest *request_ror = [ASIFormDataRequest requestWithURL:url];
[request_ror setRequestMethod:@"POST"];
[request_ror setPostValue:[NSString stringWithFormat:@"%f", longitude] forKey:@"longitude"];
[request_ror setValidatesSecureCertificate:NO];
[request_ror setTimeOutSeconds:30];
[request_ror setDelegate:self];
[request_ror startAsynchronous];

Upvotes: 0

Sean
Sean

Reputation: 5820

Use +stringWithFormat from NSString:

[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@,%@&radius=9900&types=bar&sensor=false&key=fake_key", longitude,latitude]]

Upvotes: 3

Related Questions