检查字符串是否为URL-Objective-C [英] Checking if a string is a url - Objective-C
本文介绍了检查字符串是否为URL-Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何在iPhone上验证网址
Possible Duplicate:
How to validate an url on the iPhone
在Objective-C中,没有人能测试给定字符串是否为URL的好方法吗?
In Objective-C, does anyone have a good method to test if a given string appears to be a URL?
推荐答案
您可以使用正则表达式.对于iPhone 3及更高版本,您可以在没有框架的情况下进行操作.否则,请使用RegexKitLite之类的东西.
You can use a regular expression. For iPhone 3 and up, you can do it without a framework. Otherwise use RegexKitLite or something.
这是用于检查URL的正则表达式模式:
Here is a regex pattern for checking URLs:
"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+"
在没有框架的情况下进行操作:
Doing it without a framework:
- (BOOL)validateUrl:(NSString *)candidate {
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
这篇关于检查字符串是否为URL-Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文