首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在iPhone上验证url

如何在iPhone上验证url
EN

Stack Overflow用户
提问于 2009-09-24 11:53:22
回答 22查看 72.3K关注 0票数 90

在我正在开发的一个iPhone应用程序中,有一个可以输入网址的设置,由于表单和功能的原因,这个网址需要在线和离线验证。

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

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

EN

Stack Overflow用户

发布于 2016-04-03 21:43:53

扩展@Anthony对swift的回答,我在String上写了一个类别,它返回一个可选的NSURL。如果无法验证String是否为URL,则返回值为nil

代码语言:javascript
运行
复制
import Foundation

// A private global detector variable which can be reused.
private let detector = try! NSDataDetector(types: NSTextCheckingType.Link.rawValue)

extension String {
  func URL() -> NSURL? {
    let textRange = NSMakeRange(0, self.characters.count)
    guard let URLResult = detector.firstMatchInString(self, options: [], range: textRange) else {
      return nil
    }

    // This checks that the whole string is the detected URL. In case
    // you don't have such a requirement, you can remove this code
    // and return the URL from URLResult.
    guard NSEqualRanges(URLResult.range, textRange) else {
      return nil
    }

    return NSURL(string: self)
  }
}
票数 0
EN
查看全部 22 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1471201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档