开发者

iphone: can i detect a url type?

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)
  • YouTub开发者_高级运维e 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


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");
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜