无法通过 NSDataDetector 分析 NSString 中的 url 当此字符串包含中文时

Can not analyse url in NSString when this string contains chinese by NSDataDetector

没有匹配项========================================= ===========

我知道,是"blank space"的问题,如果我在"https"前面加个空格就可以了。那么NSDataDetector能不能解决这个问题呢?

NSString *originString = @"【mans】下单立减5.00元https://kdt.im/uYMI4r";
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches1 = [linkDetector matchesInString:originString options:0 range:NSMakeRange(0, [originString length])];

for (NSTextCheckingResult *match in matches1) {
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
    }  
}

这是 NSDataDetector 的一个已知问题,如果协议前有一个 space,即 http:// 或 https://,那么它可以工作,否则它不会。

例如

    let input = "This is a test with the URL https://www.sharpkits.com to be detected."
    let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))

for match in matches {
    let url = (input as NSString).substring(with: match.range)
    print(url)
}

输出: https://www.sharpkits.com

让输入="This is a test with the URLhttps://www.sharpkits.com to be detected."

输出:URLhttps://www.sharpkits.com

所以不能用 NSDataDetector