I'm working on an app which must display a YouTube video when a button is pressed.
I'm using code found elsewhere on the site which appears to function fine under most circumstances, however in my case it is not.
- (void)displayVideo{
CGRect frame = CGRectMake(5,55, 200,310);
NSLog(@"Trying to display video with ID: %@", videoID);
NSString *embedHTML =[NSString stringWithFormat:@"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe height=\"200\" width=\"310\" src=\"http://www.youtube.com/embed/%@\"></iframe>\
</body></html>", videoID];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
videoView.backgroundColor = [UIColor redColor];
[videoView loadHTMLString:embedHTML baseURL:nil];
[self.view addSubview:videoView];
[self.view setNeedsDisplay]; }
The method is run when the button is pressed. The NSLog goes through fine, so I know the code is being run - the view appears to refuse to update though.
On a whim, I tried putting the code in the ViewController's viewDidLoad method and surprisingly, it worked fine.
Does anyone have any ideas as to why this may be?
ViewDidAppear
which is the last state when a viewcontroller loads.. – iphonicUIWebView
on button click. Add it inviewDidLoad:
method and on button Action just load html string. – TheTiger