1

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.

4

1 に答える 1

1

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.

于 2010-07-01T14:02:01.743 に答える