Search code examples
iphoneyoutubemedia-player

playing youtube links not working


Here is my code

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {  
    UIWebView * videoView;
    NSString* embedHTML = @"<html><head><style type=\"text/css\">body {background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\"width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>";  
    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];  
    if(videoView == nil) {  
        videoView = [[UIWebView alloc] initWithFrame:frame];  
        [self.view addSubview:videoView];  
    }  
    [videoView loadHTMLString:html baseURL:nil];  
}  

so in viewDidLoad I call

[self embedYouTube:@"http://www.youtube.com/watch?v=2mcjR3TsK4s&feature=g-logo&context=G2e376ceFOAAAAAAAAAA" frame:self.view.bounds];

I'm testing it in ipad2 ios 5.0 and it is an error

-[_UIAppearance loadHTMLString:baseURL:]: unrecognized selector sent to instance 0x3ec3e63c 2012-01-15 22:50:22.158 test2[3853:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAppearance loadHTMLString:baseURL:]: unrecognized selector sent to instance 0x3ec3e63c' * First throw call stack: (0x380cf8bf 0x37c1b1e5 0x380d2acb 0x380d1945 0x3802c680 0x3513 0x3407 0x31bb07ff 0x31baef9d 0x31ba1941 0x31c13541 0x3241 0x31baf7eb 0x31ba93bd 0x31b77921 0x31b773bf 0x31b76d2d 0x37dfddf3 0x380a3553 0x380a34f5 0x380a2343 0x380254dd 0x380253a5 0x31ba8457 0x31ba5743 0x3029 0x2f84) terminate called throwing an exception(gdb)

Thanks for helping


Solution

  • When you defined variable like UIWebView * videoView; it's assumes that it refers to some memory. Usually it refers to nil and all works fine, but in your case it refers to another object and comparing videoView == nil won't work. To avoid it always declare variables and refer it to nil explicity.

    UIWebView *videoView = nil;