How to Validate an Url on the Iphone

How to validate an url on the iPhone

Thanks to this post, you can avoid using RegexKit.
Here is my solution (works for iphone development with iOS > 3.0) :

- (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];
}

If you want to check in Swift my solution given below:

 func isValidUrl(url: String) -> Bool {
let urlRegEx = "^(https?://)?(www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-z0-9]{0,61}[a-z0-9]\\.[a-z]{2,6}(/[-\\w@\\+\\.~#\\?&/=%]*)?$"
let urlTest = NSPredicate(format:"SELF MATCHES %@", urlRegEx)
let result = urlTest.evaluate(with: url)
return result
}

how to validate URL in iphone app

If you want to check URL is valid or not then use following RegEx.

NSString *urlString = [NSString stringWithFormat:@"URL_STRING"];
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlPredic = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
BOOL isValidURL = [urlPredic evaluateWithObject:urlString];

And put condition such like

if(isValidURL)
{
// your URL is valid;
}
else
{
// show alert message for invalid URL;
}

And also your can convert URL as legal URL, such like

NSString *urlString = [NSString stringWithFormat:@"URL_STRING"];
NSURL *youURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

For more information check stringByAddingPercentEscapesUsingEncoding:

How do I validate an URL?

I think what you're missing is open the url with UIApplication.sharedApplication().canOpenURL(url) for example, change your code to this one:

func verifyUrl (urlString: String?) -> Bool {
if let urlString = urlString {
if let url = NSURL(string: urlString) {
return UIApplication.sharedApplication().canOpenURL(url)
}
}
return false
}

verifyUrl("escola") // false
verifyUrl("http://wwww.google.com") // true

The constructor of NSURL not validate the url as you think, according to Apple:

This method expects URLString to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding

I hope this help you.

Validate URL exactly

try this method...

  - (BOOL) urlIsValiad: (NSString *) url 
{
NSString *regex =
@"((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?";
/// OR use this
///NSString *regex = "(http|ftp|https)://[\w-_]+(.[\w-_]+)+([\w-.,@?^=%&:/~+#]* [\w-\@?^=%&/~+#])?";
NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

if ([regextest evaluateWithObject: url] == YES) {
NSLog(@"URL is valid!");
} else {
NSLog(@"URL is not valid!");
}

return [regextest evaluateWithObject:url];
}

Check if URL is valid in Swift 4

You're probably crashing because you're using the ! operator and not checking that it will work. Instead try:

@IBAction func startBrowser(_ sender: Any) {
if let urlString = self.urlTextField.text {
let url: URL?
if urlString.hasPrefix("http://") {
url = URL(string: urlString)
} else {
url = URL(string: "http://" + urlString)
}
if let url = url {
let sfViewController = SFSafariViewController(url: url)
self.present(sfViewController, animated: true, completion: nil)
print ("Now browsing in SFSafariViewController")
}
}
}

This should give you the idea of how to handle the different cases, but you probably want something more sophisticated which can deal with https and strips whitespace.



Related Topics



Leave a reply



Submit