小网址问题

发布于 2024-09-08 00:42:19 字数 863 浏览 6 评论 0原文

我正在为我的应用程序开发 Twitter API。在我的应用程序中,我想在 UITexField 中动态显示带有已解析 xml 链接的小 url。静态地,我可以在 UITextField 中显示微小的网址,但动态地我无法显示微小的网址。请帮我!

代码:静态(工作正常)、

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]; 

动态、

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]; 

在上面,动态应用程序运行时没有警告和错误,但 url 未显示,当我检查调试器时,url 和链接都显示 nil 值。但 shtUrl 也可以正确显示整个 url 值。

我也尝试了所有 NSURL 类和实例方法以及字符串方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2024-09-15 00:42:19

在这行代码中:

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

“api-create.php?”后面有一个空格。这将导致您创建的格式化字符串中出现空格,并可能导致 URLWithString: 无法解析 url 并返回 nil。

删除多余的空间(假设它确实存在,而不仅仅是剪切粘贴错误),看看是否可以解决问题。

您正在为其构建tinyurl的shtUrl也可能包含需要进行urlencoded的特殊字符(即转义百分比。)尝试添加以下内容:

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

shtUrl进行编码,然后在创建 tiny 时使用 encodedShtUrl

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

请参阅 http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ 了解有关转义的更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文