We're developing an app which contains a TableView with some standard cells, but also a list of YouTube videos. We know how to embed the YouTube video data into a UIWebView and play it when the user touches it. But in order to do that, users must touch the UIWebView.
Since it's a TableView, every cell is the typical cell with the thumbnail on the left, some info and an accessory button on the right. We want to let users touch anywhere in the cell, not just the WebView (didSelectRow) so the video would start.
How can we achieve this?
Answering myself. I've found the solution. This code will autoplay the webview.
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *b = [self findButtonInView:_webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Youtube video autoplay on iPhone's Safari or UIWebView
Hope it helps somebody else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With