On May 25, 2014, at 10:05 AM, Koen van der Drift wrote:

> At one point in my code I need to recognize an URL to show in a different 
> color. Pretty standard:
> 
>    NSURL *url = [NSURL URLWithString: aString];
> 
>    if (url)  // if url is created change color

> Works great, if aString starts with eg http:// it will be in blue; regular 
> strings won't.
> 
> 
> However, I noticed that strings that have the form P12345 also turn blue.  
> Any idea why that string would turn into an NSURL?  The url scheme is nil, 
> and the path components just show P12345.  
> 
> (I could test above for if (url.scheme) to avoid this, but knowing why this 
> happens would actually help me with other parts in my app).

The docs for +URLWithString: say that the string "[m]ust be a URL that conforms 
to RFC 2396. This method parsesURLString according to RFCs 1738 and 1808."  RFC 
1808 is about the syntax for relative URLs.  I suspect that "P12345" meets the 
(fairly loose) criteria for a relative URL string.  I suspect that many strings 
will.

You are abusing the API.  If you call +URLWithString:, Cocoa assumes that the 
string is a URL and will do its best to interpret it as such.  It will fail 
only if the string _can't_ be interpreted as a URL.  This is not a method for 
determining if a string is a typical URL suitable for any particular purpose 
(like opening in a browser).

You might want to see if NSDataDetector with NSTextCheckingTypeLink would work 
better for your purposes.

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to