Youtube video autoplay on iPhone's Safari or UIWebView

前端 未结 5 1687
遇见更好的自我
遇见更好的自我 2020-12-02 19:35

Is it possible to get a youtube video to autoplay on Safari and/or UIWebView? I\'ve seen this done in an iPhone app, the tableview displays cells that do not have Youtube pr

5条回答
  •  既然无缘
    2020-12-02 20:03

    the trick is to find the button in the webview. Use the following snippet.

    - (void)loadWebUIViewWithYoutubeLink {
        webView.delegate = self;
        NSString *htmlString = [NSString stringWithFormat:[NSString
                                 stringWithContentsOfFile:[[NSBundle mainBundle]
            pathForResource:@"YouTubeTemplate" ofType:@"txt"]],
                                 @"b85hn8rJvgw",  @"b85hn8rJvgw", nil];
        [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://youtube.com"]];
    }
    
    - (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];
    }
    

提交回复
热议问题