是否可以在没有触摸的情况下与UIWebView交互?

时间:2013-03-20 19:39:16

标签: iphone html ios url uiwebview

我正在尝试与UIWebView中的已加载页面进行交互而不触摸屏幕。我尝试过mediaplaybackrequiresuseraction没有成功。我也尝试了here提供的方法,但显然它在ios 6中不起作用。我正在尝试加载this 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];
 }

EDIT // 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();"];
   }
 }

4 个答案:

答案 0 :(得分:4)

如果你只想播放一首歌,那么&当webview加载时然后你需要在

中调用播放功能
 `- (void)webViewDidFinishLoad:(UIWebView *)webView {

    // Do whatever you want here
   // play song
}` 

但请确保您已将代理人分配给您的网页浏览

<UIWebView_object>.delegate = self;

这将自动播放歌曲&amp;加载webview时

享受编程!!

答案 1 :(得分:1)

基于HTML 5 Youtube视频查看按钮的方法。
我找到了解决问题的方法,它并不优雅,但却在工作。

您的链接

  

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

使用flash对象下载页面。据我所知,没有办法开始使用JS或UIWebView播放它 在研究请求后,我看到UIWebView查询了另一个网址:

  

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启动视频。

所以我们需要转换初始网址。好像我们在网址中只有两个重要参数 - 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];

并加载新网址

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

加载网址后,我们可以访问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();"];
    }
}

我知道这不优雅,也许有人会找到一种方法来改善它 随意评论。

答案 2 :(得分:1)

您可以在网页视图中加载的网页的javascript中显示“自动播放”。

答案 3 :(得分:1)

- (void)videoThumb:(NSString *)urlString frame:(CGRect)frame {
      NSString *embedHTML = @"\
      <html><head>\
      <style type=\"text/css\">\
      body {\
      background-color: transparent;
      color: white;
      }\
      </style>\
      </head><body style=\"margin:0\">\
      <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
      width=\"%0.0f\" height=\"%0.0f\"></embed>\
      </body></html>";


   NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width,frame.size.height];

  [webView loadHTMLString:html baseURL:nil];
  [self.view addSubview:webView];
  [webView release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Include above function to your page and call it by passing the urlstring
    //frame : this parameter needs the size of the thumb you want to use.
    [self videoThumb:url:frame];
}

只需通过您想要播放的网址来调用videoThumb功能即可。