检查URL是否有http://前缀 [英] Check if an URL has got http:// prefix
问题描述
在我的应用程序中,当用户添加一个对象时,也可以为该对象添加一个链接,然后可以在webView中打开该链接。
我试过保存没有http://前缀的链接,然后在webView中打开但无法打开它!
webView开始加载之前,是否有方法检查保存的URL是否有http: // 字首?如果它没有得到它,我怎么能在URL中添加前缀?
谢谢!
In my application, when the user add an object, can also add a link for this object and then the link can be opened in a webView.
I tried to save a link without http:// prefix, then open it in the webView but that can't open it!
Before webView starts loading, is there a method to check if the URL saved has got http:// prefix? And if it hasn't got it, how can I add the prefix to the URL?
Thanks!
推荐答案
您可以在NSString上使用 - (BOOL)hasPrefix:(NSString *)aString
方法查看包含您的URL的NSString是否以http://前缀开头,如果没有添加前缀。
You can use the - (BOOL)hasPrefix:(NSString *)aString
method on NSString to see if an NSString containing your URL starts with the http:// prefix, and if not add the prefix.
NSString *myURLString = @"www.google.com";
NSURL *myURL;
if ([myURLString.lowercaseString hasPrefix:@"http://"]) {
myURL = [NSURL URLWithString:myURLString];
} else {
myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",myURLString]];
}
我目前离开了我的mac,无法编译/测试这个代码,但我相信上面应该有效。
I'm currently away from my mac and can't compile/test this code, but I believe the above should work.
这篇关于检查URL是否有http://前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!