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

标签 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];
 }

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

最佳答案

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

 `- (void)webViewDidFinishLoad:(UIWebView *)webView {

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

但请确保您已像这样将委托(delegate)分配给您的 WebView

<UIWebView_object>.delegate = self;

这将在 webview 加载时自动播放歌曲

享受编程吧!

关于iphone - 是否可以在没有触摸的情况下与 UIWebView 交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533130/

相关文章:

iphone - 以编程方式计算包含 "<br>"或 "\n"的文本的 UITableViewCell 高度

iphone - + (void) 未调用初始化( Objective-C )

ios - 在 App Store 的 itunesconnect 中删除图标光泽(不在应用程序中)

html - 一个组织的分散的 hCard 数据

ios - 无法为同一个 Firebase 项目中的第二个应用程序添加 Push Certificates

iphone - 使用 JSON 搜索 MKMapview 往往不会返回任何结果,或者返回不正确的结果

iphone - NSMetaDataQuery如何监控本地文件目录

javascript - 如何在所有函数中获取相同的 JavaScript 变量值

javascript - jQuery 在特定位置插入字符

ios - UInt8 无法转换为 iOS Swift 中的 CGFloat 错误