Search code examples
iphonensurl

Setting an NSURL with %@ making value equal to %@ instead of inserting %@


I'm using ASIHTTP and trying to perform a GET request for a site:

NSURL *url = [[NSURL URLWithString:@"/verifyuser.aspx?user=%@" relativeToURL:@"http://domain.com"],userName  retain];       
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTemporaryFileDownloadPath:@"myfile2.txt"];
[request setDownloadDestinationPath:@"myfile.txt"];

[request startSynchronous];

However, when I put a breakpoint on [request startSynchronous] and go into the debugger, the url value of the request object is equal to the userName variable. I'm trying to insert the userName variable into a string and then use that as the url, so something's not right in my NSURL declaration.

Thanks for your help!


Solution

  • Your code is incorrect, you are not properly doing string formatting, it should look like this:

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"/verifyuser.aspx?user=%@", userName] relativeToURL:@"http://domain.com"];
    

    Note that you don't need to retain the url as you are just passing it to the request as well.