I have a URL string that I need to replaces values with.
For example:
http:\\www.domain.com\id=[USER_ID]&record=[CALL_KEY]&someother=[OTHER_STUFF]
I need to loop through the string looking for the [ ] then replacing it with whatever is inside it.
As it stands now I have to hard code each individual key and replace it with the value.
- (NSString*) injectValuesIntoURL:(NSString*) url{
NSString * injected = @"";
//[CALL KEY]
injected = [url stringByReplacingOccurrencesOfString:@"[CALL_KEY]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kCallKey"]];
//[USER_ID]
injected = [injected stringByReplacingOccurrencesOfString:@"[USER_ID]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kUsername"]];
//[OTHER_STUFF]
injected = [injected stringByReplacingOccurrencesOfString:@"[OTHER_STUFF]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kOtherStuff"]];
return injected;
}
I have a bunch of different values that can be plugged in, and instead of hard coding [CALL KEY], I would like to dynamically read what is in there and inject the value. So the initial URL may be
http:\\www.domain.com\id=[kUsername]&record=[kCallKey]&someother=[kOtherStuff]
So I can just loop through the string finding the [ ] and replacing it with whatever is inside.
How do I search a string and find the [ character and then copy that string up to the ], then continue on to the next [ so and and so forth?