UIWebView不使用方法stringByEvaluatingJavaScriptFromString评估java脚本:

时间:2014-01-13 19:20:50

标签: javascript ios uiwebview youtube-api youtube-javascript-api

我无法从UIWebView评估youtube api javascript命令

命令在Safari和Firefox中正确评估

使用选项

初始化webview
allowsInlineMediaPlayback = YES;
mediaPlaybackRequiresUserAction = NO;

我正在加载的Html是:

<div id="player"></div>

<script>
    var tag = document.createElement('script');
    //playerVars: { 'autoplay': 0, 'modestbranding': 1, 'rel': 0, 'showinfo': 0, 'iv_load_policy': 3, 'controls': 1, 'playsinline':1 },
    var configuration = {
        playerVars: {
            'playsinline':1,
            'showinfo': 0,
            'modestbranding': 1
        },
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
        }
    };

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function setup(key, value) {
    configuration[key] = value;
}

function configurePlayer(key, value) {
    configuration.playerVars[key] = value;
}
// area for the code injection
setup('videoId', 'cDhlx60nTCU');
setup('startingTime', 10);
setup('autoPlay' , true);
setup('width', 400);
setup('height', 200);
configurePlayer('controls', true);


var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', configuration);
}

function onPlayerReady(event) {
    if (configuration['autoPlay']) {
        if (configuration['startingTime']) {
            player.playVideoAt(configuration['startingTime']);
        } else {
            event.target.playVideo();
        }
    }
}

function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
        // Do something...
    }
}

function stopVideo() {
    player.stopVideo();
    return "OK";
}
</script>
Printing description of html:
<div id="player"></div>

<script>
    var tag = document.createElement('script');
    //playerVars: { 'autoplay': 0, 'modestbranding': 1, 'rel': 0, 'showinfo': 0, 'iv_load_policy': 3, 'controls': 1, 'playsinline':1 },
    var configuration = {
        playerVars: {
            'playsinline':1,
            'showinfo': 0,
            'modestbranding': 1
        },
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
        }
    };

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function setup(key, value) {
    configuration[key] = value;
}

function configurePlayer(key, value) {
    configuration.playerVars[key] = value;
}
// area for the code injection
setup('videoId', 'cDhlx60nTCU');
setup('startingTime', 0);
setup('autoPlay' , true);
setup('width', 320);
setup('height', 288);
configurePlayer('controls', true);


var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', configuration);
}

function onPlayerReady(event) {
    if (configuration['autoPlay']) {
        if (configuration['startingTime']) {
            player.playVideoAt(configuration['startingTime']);
        } else {
            event.target.playVideo();
        }
    }
}

function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
        // Do something...
    }
}

function stopVideo() {
    player.stopVideo();
    return "OK";
}
</script>

我正在使用此

加载webview页面
-(void)viewDidAppear:(BOOL)animated
{
    [self.webview loadHTMLString:html baseURL:[NSURL URLWithString:@"html"]];
}
webview委托调用webViewDidFinishLoad

我尝试使用此语法在播放器上调用方法

-(void)pause
{
    [self.webview stringByEvaluatingJavaScriptFromString:@"player.pauseVideo();"];
}

是否有任何标志干扰了java脚本评估?

1 个答案:

答案 0 :(得分:1)

更改此行:

[webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"html"]];

到此:

[webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"about:blank"]];

我注意到当您不使用有效的网址时,stringByEvaluatingJavaScriptFromString将无效(您会发现当您使用NULL作为值时也会发生这种情况。我刚刚测试过您的代码包含两个版本,并且发现about:blank版本会正确评估JavaScript,而html版本的NSURL值则不会。