[ios]它可能与 UIWebView 没有触摸交互?

发布时间: 2017/3/23 2:33:17
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图与在 UIWebView 加载页面交互没有触摸屏幕。我用过 mediaplaybackrequiresuseraction,但没有成功。 我也试过的方法提供在这里,但显然它不工作在 ios 6。 我试图加载url 和使玩家开始玩。 有谁知道如何使这个工作 ios 6? 任何帮助大大赞赏。

我的代码

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


  int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

  NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
  //storyLink is various form of http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001 with different bctid=value

  NSURL *kitcoURL = [NSURL URLWithString:storyLink];
  NSURLRequest *requestURL = [NSURLRequest requestWithURL:kitcoURL];
  [videoScreen setUserInteractionEnabled:YES];
  [videoScreen setMediaPlaybackRequiresUserAction:NO];
  [videoScreen loadRequest:requestURL];

 }

方法在 ios6 不工作

 - (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];
 }

编辑 //videoScreen 是我 UIWebView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *urlString = @"http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001";

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"bckey=([^&]*)" options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bckey = [urlString substringWithRange:[match rangeAtIndex:1]];

regex = [NSRegularExpression regularExpressionWithPattern:@"bctid=([0-9]*)" options:0 error:&error];
match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bctid = [urlString substringWithRange:[match rangeAtIndex:1]];

NSString *newUrlString = [NSString stringWithFormat:@"http://c.brightcove.com/services/viewer/htmlFederated?&playerKey=%@&videoID=%@", bckey, bctid];

[videoScreen loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newUrlString]]];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (!webView.isLoading)
{
    [self checkForVideoReady];
}

}
 // Checking player object for ready. If it ready - starting video. Otherwise waiting 1 second 
- (void)checkForVideoReady
{
    if (![[videoScreen stringByEvaluatingJavaScriptFromString:@"player.ready"]   isEqualToString:@"true"])
    {
    [self performSelector:@selector(checkForVideoReady) withObject:nil afterDelay:1];
   }
   else
   {
        [videoScreen stringByEvaluatingJavaScriptFromString:@"player.videoPlayer.playButton.element.click();"];
   }
 }

解决方法 1:

你的方法找到按钮在视图中基于 HTML 5 Youtube 视频。
我为你的问题找到了解决方法,不是优雅,而是工作。

您的链接

http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ ~ ~,AAAAC59qSJk ~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn & bclid = 1644543007001 & bctid = 2238216269001

下载页面的 flash 对象。并且,只要知道没有办法开始播放它使用 JS 或 UIWebView
研究了请求后见到, UIWebView 为另一个 url 的查询︰

http://c.brightcove.com/services/viewer/htmlFederated?&width=966&height=546&flashID=myExperience&bgcolor=%23FFFFFF&playerID=1683318714001&playerKey=AQ~~%2CAAAAC59qSJk~%2CvyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&isVid=true&isUI=true&dynamicStreaming=true&autoStart=true&debuggerID=&videoID=2238216269001&%40videoPlayer=2238216269001&lineupID=1644543007001&startTime=1364279975091&refURL=not%20available&%40videoPlayer=2238216269001

第二个是更有趣,因为我们可以访问到 player 变量和视频使用 JavaScript 的开始。

所以我们需要转换初始 URL。看来我们还有只有 2 重要参数在 URL- bckeybctid :

NSString *urlString = @"http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001";

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"bckey=([^&]*)" options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bckey = [urlString substringWithRange:[match rangeAtIndex:1]];

regex = [NSRegularExpression regularExpressionWithPattern:@"bctid=([0-9]*)" options:0 error:&error];
match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bctid = [urlString substringWithRange:[match rangeAtIndex:1]];

NSString *newUrlString = [NSString stringWithFormat:@"http://c.brightcove.com/services/viewer/htmlFederated?&playerKey=%@&videoID=%@", bckey, bctid];

和加载新的 URL

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newUrlString]]];

URL 加载以后我们可以访问 player 对象。此对象可以告诉准备玩,开始播放视频时

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (!webView.isLoading)
    {
        [self checkForVideoReady];
    }

}
// Checking player object for ready. If it ready - starting video. Otherwise waiting 1 second 
- (void)checkForVideoReady
{
    if (![[_webView stringByEvaluatingJavaScriptFromString:@"player.ready"] isEqualToString:@"true"])
    {
        [self performSelector:@selector(checkForVideoReady) withObject:nil afterDelay:1];
    }
    else
    {
        [_webView stringByEvaluatingJavaScriptFromString:@"player.videoPlayer.playButton.element.click();"];
    }
}

我知道这不是优雅,或许有人会找到如何改进它。
感受自由评论。

官方微信
官方QQ群
31647020