I am developing Twitter API to my application. In my app, I want to display the tiny url with parsed xml link in UITexField as dynamically. statically, I could able to display tiny url in UITextField, but dynamically I am not able to display the tiny url. Please help me!
Code: statically(Working fine),
NSURL *url = [NSURL URLWithString"http://tinyurl.com/api-create.php?
url=https://gifts.development.xxxxx.edu/xxxx/Welcome.aspx?appealcode=23430"];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
Dynamically,
NSString * tiny = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php? url=%@", shtUrl];//shtUrl is parsed string
NSURL *url = [NSURL URLWithString:tiny];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
In above, dynamically app running without warning and error, but url not displayed and when I am checking debugger, url and link both are show nil value. But shtUrl show whole url value as properly.
I tried with all NSURL class and instance methods and String methods also.
In this line of your code:
there is a space after the ‘api-create.php?’. This will result in a space in the formated string you are creating and will probably result in
URLWithString:being unable to parse the url and returning nil.Remove the extra space (assuming that it is really there and not just a cut-n-paste error) and see if that fixes the problem.
It’s also possible that the
shtUrlthat you are building a tinyurl for contains special characters that would need to be urlencoded (i.e. percent escaped.) Try adding this:to encode the
shtUrl, then use theencodedShtUrlwhen creatingtiny:See http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ for more about the escaping.