如何在 iPhone 上验证 url [英] How to validate an url on the iPhone

查看:30
本文介绍了如何在 iPhone 上验证 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的 iPhone 应用程序中,有一个设置,您可以在其中输入 URL,因为表单和功能此 URL 需要在线和离线验证.

In an iPhone app I am developing, there is a setting in which you can enter a URL, because of form & function this URL needs to be validated online as well as offline.

到目前为止,我还没有找到任何方法来验证 url,所以问题是;

So far I haven't been able to find any method to validate the url, so the question is;

如何在线和离线验证 iPhone (Objective-C) 上的 URL 输入?

How do I validate an URL input on the iPhone (Objective-C) online as well as offline?

推荐答案

感谢 这篇文章,你可以避免使用 RegexKit.这是我的解决方案(适用于 iOS > 3.0 的 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];
}

如果你想签入 Swift 我的解决方案如下:

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
    }

这篇关于如何在 iPhone 上验证 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆