NSString *pageString = @"www.youtube.com/watch?v=MOST VIDEOS ARE SUPPORTED";
NSString *agentString = @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:pageString]];
[request setValue:agentString forHTTPHeaderField:@"User-Agent"];
NSData *data = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];</p>
<pre><code>NSString *page = [[NSString alloc] initWithBytes: [data bytes] length:[data length] encoding: NSUTF8StringEncoding];
NSRange range1 = [page rangeOfString:@"ajax?v="];
NSRange range2 = [page rangeOfString:@"&action_get_comments"];
NSUInteger lok = range1.location + range1.length;
NSRange finRange = NSMakeRange(lok, range2.location - lok);
NSString *subString3 = [page substringWithRange:finRange];
range1 = [page rangeOfString:@"\"t\": \""];// "t":"
range2 = [page rangeOfString:@"\", \"rv.6.id\":"];// ", "rv.6.id":
lok = range1.location + range1.length;
finRange = NSMakeRange(lok, range2.location - lok);
NSString *subString4 = [page substringWithRange:finRange];
NSString *demoURL = [NSString stringWithFormat:@"http://www.youtube.com/get_video?video_id=%@&t=%@&fmt=18",subString3,subString4];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:demoURL]];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: player];
[player play];