views:

48

answers:

1

I'm currently creating a twitter client for iphone.

Basically, I have three kinds of url types I want to handle:

  • Standard URL's (e.g. http://www.msn.com)
  • YouTube URL's
  • URL's containing links to a stream.

When a user clicks a link.. instead of launching the web view and letting the webview decide whether to load quicktime player & youtube player, I want to be able to handle it within the app.

i.e. how can i code something like:

check link if link = link.youtube then load youtube player if link = link.mp3 stream then load quicktime player else load in webview

+1  A: 

Checking youtube is easy, you can just check the domain like

[NSString rangeOfSubstring:@"youtube.com"].location != NSNotFound

The mp3 stream is not so hard, you check for the last extension by getting the last "." and then check it agains the set of video stream format like:

NSArray *possibleExtensions = [url componentsSeparatedByString:@"."];
NSString *extension = [possibleExtensions objectAtIndex:([possibleExtensions count] - 1)];  
NSSet *extensionSet = [NSSet setWithObjects: @"mp4", @"mov", @"m4v", @"mpv", @"3gp", nil];    
BOOL isStream = [extensionSet containsObject:extension];

If you want to check for normal link, you can use Regular expression:

  NSString *regrexUrl = @"\\b((?:[\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|(?:[^\\p{Punct}\\s]|/)))";
  // Based on http://daringfireball.net/2009/11/liberal_regex_for_matching_urls

  NSString *urlString = [YOUR_STRING_HERE stringByMatching:NCPARSER_REGEX_URL capture:1];
  if (urlString) {
    NSLog(@"It is an url");
  }
vodkhang
dpigera
Then, you may need to detect for this whole substring : "http://www.youtube.com/watch"
vodkhang