Is there any API or good function to format strings like
“www.example.com” or just “example.com” to valid urlstrings like
“http://www.example.com”?
+ (NSString*)complete:(NSString *)urlString
{
NSArray * urlParts = [urlString componentsSeparatedByString:@"."];
if(urlParts.count==3)
return [NSString stringWithFormat:@"http://www.%@.%@",[urlParts objectAtIndex:1],[urlParts objectAtIndex:2]];
else if(urlParts.count==2)
return [NSString stringWithFormat:@"http://www.%@.%@",[urlParts objectAtIndex:0],[urlParts objectAtIndex:1]];
return nil;
}
this is what current solution looks like, but this doesn’t look very good and solid to me.
You can do this fairly easily with
NSDataDetector:You should not assume that
wwwis a valid subdomain for any given domain btw.If you need the result as a string, you can use the
absoluteStringmethod ofNSURL, it’s typically better to useNSURLinstead ofNSStringfor URLs though.