小网址问题
我正在为我的应用程序开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这行代码中:
“api-create.php?”后面有一个空格。这将导致您创建的格式化字符串中出现空格,并可能导致
URLWithString:
无法解析 url 并返回 nil。删除多余的空间(假设它确实存在,而不仅仅是剪切粘贴错误),看看是否可以解决问题。
您正在为其构建tinyurl的
shtUrl
也可能包含需要进行urlencoded的特殊字符(即转义百分比。)尝试添加以下内容:对
shtUrl
进行编码,然后在创建tiny
时使用encodedShtUrl
:请参阅 http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ 了解有关转义的更多信息。
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
shtUrl
that 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 theencodedShtUrl
when creatingtiny
:See http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ for more about the escaping.