mardi 24 mars 2015

iOS8 how to turn a arbitrary string into a web search, like Google Chrome browser?

When working with Google Chrome browser, I can type in a fully formed address or a sequence of words and have the browser properly format it. For example:



cnn.com -> goes to website, adds http://
cnn -> does search for cnn
//etc


Below is my method implementation to turn an arbitrary string into a web browser search.


Is there a comprehensive open source alternative to properly translating string into a web view address?



-(NSString*)cleanFormatLink:(NSString*)link
{
//remove leading or trailing whitespace
NSString* returnString = [link stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];;

//check if it has spaces - do google search instead
BOOL hasSpaces = ([returnString rangeOfString:@" "].location!= NSNotFound);
if(hasSpaces)
{
// http://ift.tt/1laD9bU

returnString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)returnString,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]\" "),
kCFStringEncodingUTF8));
returnString = [@"http://ift.tt/vgrAeu" stringByAppendingString:returnString];

}else if(([returnString rangeOfString:@"."].location!= NSNotFound))
{
//found a dot - most likely an address

if(
[[link lowercaseString] hasPrefix:@"http://"] == NO ||
[[link lowercaseString] hasPrefix:@"https://"] == NO
)
{
//fixes google.com into http://google.com
returnString = [@"http://" stringByAppendingString:returnString];
}

}
return returnString;
}



Aucun commentaire:

Enregistrer un commentaire