Is it possible to detect links within an NSString that have spaces in them with NSDataDetector? Is it possible to detect links within an NSString that have spaces in them with NSDataDetector? objective-c objective-c

Is it possible to detect links within an NSString that have spaces in them with NSDataDetector?


I just got this response from Apple for a bug I filed on this:

We believe this issue has been addressed in the latest iOS 9 beta. This is a pre-release iOS 9 update.

Please refer to the release notes for complete installation instructions.

Please test with this release. If you still have issues, please provide any relevant logs or information that could help us investigate.

iOS 9 https://developer.apple.com/ios/download/

I will test and let you all know if this is fixed with iOS 9.


You could split the strings into pieces using the spaces so that you have an array of strings with no spaces. Then you could feed each of those strings into your data detector.

// assume str = <img src="http://www.blah.com/My First Image Here.jpg">NSArray *components = [str componentsSeparatedByString:@" "];for (NSString *strWithNoSpace in components) {    // feed strings into data detector}

Another alternative is to look specifically for that HTML tag. This is a less generic solution, though.

// assume that those 3 HTML strings are in a string array called strArrayfor (NSString *htmlLine in strArray) {    if ([[htmlLine substringWithRange:NSMakeRange(0, 8)] isEqualToString:@"<img src"]) {        // Get the url from the img src tag        NSString *urlString = [htmlLine substringWithRange:NSMakeRange(10, htmlLine.length - 12)];    }}


I've found a very hacky way to solve my issue. If someone comes up with a better solution that can be applied to all URLs, please do so.

Because I only care about URLs ending in .jpg that have this problem, I was able to come up with a narrow way to track this down.

Essentially, I break out the string into components based off of them beginning with "http:// into an array. Then I loop through that array doing another break out looking for .jpg">. The count of the inner array will only be > 1 when the .jpg"> string is found. I then keep both the string I find, and the string I fix with %20 replacements, and use them to do a final string replacement on the original string.

It's not perfect and probably inefficient, but it gets the job done for what I need.

- (NSString *)replaceSpacesInJpegURLs:(NSString *)htmlString{    NSString *newString = htmlString;    NSArray *array = [htmlString componentsSeparatedByString:@"\"http://"];    for (NSString *str in array)    {        NSArray *array2 = [str componentsSeparatedByString:@".jpg\""];        if ([array2 count] > 1)        {            NSString *stringToFix = [array2 objectAtIndex:0];            NSString *fixedString = [stringToFix stringByReplacingOccurrencesOfString:@" " withString:@"%20"];            newString = [newString stringByReplacingOccurrencesOfString:stringToFix withString:fixedString];        }    }    return newString;}