i have a String NSString *const kAUrl = @"http://www.google/start.php?show=best&usr=(here)&pwd=x";
and
i want to pass another String NSString *a = @"aa";
to above url at the place i have written (here) in above url.
How can i pass it?
regards.
i have a String NSString *const kAUrl = @"http://www.google/start.php?show=best&usr=(here)&pwd=x";
and
i want to pass another String NSString *a = @"aa";
to above url at the place i have written (here) in above url.
How can i pass it?
regards.
You will need to encode the string suitable for URL using
the NSString instance method -[stringByAddingPercentEscapesUsingEncoding:].
Then place into the URL using -[stringWithFormat:].
For example:
-(NSURL*)showBestURLWithUser:(NSString*)user {
{
user = [user stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
static NSString* template = @"http://www.google/start.php?show=best&usr=%@&pwd=x";
NSString* urlString = [NSString stringWithFormat:template, user];
return [NSURL URLWithString:urlString];
}
Use NSString stringWithFormat:
NSString *a = @"hello";
NSString *kAUrl = [NSString stringWithFormat:@"http://www.google/start.php?show=best&usr=%@&pwd=x", a];
kAUrl now contains the contents of a where the %@ was.
Watch out though - if a contains any URL reserved characters (i.e. an & etc) it might break your url.
NSString *a = @"aa";
NSString *kAUrl = [NSString stringWithFormat:@"http://www.google/start.php?show=best&usr=%@&pwd=x",a];
kAUrl = [kAUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
job done.