Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to validate a user-entered URL in a Cocoa application?

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.

like image 804
mclaughj Avatar asked Jan 25 '23 01:01

mclaughj


1 Answers

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.

like image 168
Jon Shea Avatar answered Feb 05 '23 15:02

Jon Shea