点击按钮直接播放webView视频

2017-04-13  本文已影响175人  王zuozuo

首先,在controller里设置一个webView的成员变量:
@property (nonatomic, strong) UIWebView *playerWebView;

然后设置这个webView的getter方法(防止重复add)

- (UIWebView *)playerWebView{
    if (!_playerWebView) {
        _playerWebView = [[UIWebView alloc] init];
        _playerWebView.allowsInlineMediaPlayback = NO;
        _playerWebView.mediaPlaybackRequiresUserAction = NO;
        _playerWebView.opaque = NO;
        [_playerWebView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.view addSubview:_playerWebView];
    }
    return _playerWebView;
}

之后在按钮点击事件中loadRequest

- (void)action:(UIButton*)sender
{
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self.playerWebView loadRequest:urlRequest];
}
上一篇下一篇

猜你喜欢

热点阅读