views:

38

answers:

1

Hi Guys!

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.

+1  A: 

In this line of your code:

NSString * tiny = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php? url=%@", shtUrl];

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 shtUrl that you are building a tinyurl for contains special characters that would need to be urlencoded (i.e. percent escaped.) Try adding this:

NSString * encodedShtUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                               (CFStringRef)shtUrl,
                                                                               NULL,
                                                                               (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                               kCFStringEncodingUTF8 );

to encode the shtUrl, then use the encodedShtUrl when creating tiny:

NSString * tiny = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php? url=%@", encodedShtUrl];

See http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ for more about the escaping.

Jason Jenkins
@Jason Jenkins Thanks, Now I removed space, so NSURL sowing the link correctly. But still Link variable showing nil value only.
Sivanathan
What is the value of tiny and url if shtUrl is "http://stackoverflow.com" for example? Use NSLog or the debugger to examine the values? Does the url that is generated work if you hit the url by hand (with a browser or wget/curl)? Could the problem be related to needing to urlencoding escape special characters in the shtUrl?
Jason Jenkins
@Jason Jenkins Thanks a Lot! I solved by removed special char, newline and all.
Sivanathan