且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Cocoa应用程序中验证用户输入的URL的***方法是什么?

更新时间:2023-02-26 20:00:06

您可以从 > http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString: =nofollow noreferrer> NSURL ,如果字符串格式错误,则返回 nil



如果您需要进一步验证,可以使用 baseURL host parameterString path 等方法给你的URL的特定组件,然后你可以以任何你认为合适的方式评估。 p>

I am trying to build a homebrew web brower to get more proficient at Cocoa. I need a good way to validate whether the user has entered a valid URL. I have tried some regular expressions but NSString has some interesting quirks and doesn't like some of the back-quoting that most regular expressions I've seen use.

You could start with the + (id)URLWithString:(NSString *)URLString method of NSURL, which returns nil if the string is malformed.

If you need further validation, you can use the baseURL, host, parameterString, path, etc methods to give you particular components of the URL, which you can then evaluate in whatever way you see fit.