Paresh Navadiya
Paresh Navadiya

Reputation: 38259

Create URL based on entered data in iOS

If user entered say google then

i need to add http://www.google.com ie missing part.

User may enter any thing say google.in or www.google or anything.

Now goal to complete the left over url as we check url using regex like this:

NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";

That given url is valid or not

Upvotes: 1

Views: 135

Answers (3)

Mike Abdullah
Mike Abdullah

Reputation: 15013

Haven't tried this one myself yet, but an NSDataDetector for the type NSTextCheckingTypeLink should be able to do you a decent job.

Upvotes: 0

Marcus Adams
Marcus Adams

Reputation: 53880

Don't forget the third level domain names with suffixes like .co.uk, .co.us, .com.co, etc.

A fully qualified domain name must have at least one dot. If it doesn't have at least one dot, then you might add .com to the end.

If it does have at least one dot, then it gets more complicated. .google could be a top level domain in the future, though it isn't now. Perhaps you want to keep a white list of all "valid" first and second level domain names. You evaluate the entered domain name from the right until it stops matching domains from your list. The remainder is the "registered" domain name and any sub domains. If you don't find any matches, then add .com.

Alternatively, rather than parsing the domain name, you could just try to resolve it, and if it doesn't resolve, then add .com and try again.

Upvotes: 1

Dan
Dan

Reputation: 5173

I think I know what you're asking however other than the regex how are you actually validating that the URL is valid? It seems as though you're making an incorrect assumption that all URLs follow a common syntax. As as example, http://www.www.extra-www.org/ is a valid URL so if you apply your regex (as I understand your intent) the user would get to http://www.extra-www.org which may not be the same site as the one the user wanted (even though in this case it is because it forwards). Another example is http://www.www.com... if the user enters "www" your regex will kill it. A final example is if a site doesn't have its DNS registered WITH the "www" - your regex will incorrectly add the "www" piece.

EDIT: what happens if the user needs https as opposed to http and only enters "google"?

Upvotes: 0

Related Questions