No, non c'è alcun modo per ottenere direttamente evento pagina web da UIWebView
. Ma possiamo farlo usando Javascript.
- Per prima cosa si utilizza il codice JavaScript incorporato nel codice HTML personalizzato, per rilevare il video che termina l'evento di riproduzione.
- Quindi si tenta di caricare una richiesta personalizzata di schema utilizzando JavaScript e UIWebView potrebbe ricevere la richiesta.
Questi link possono aiutare:
Calling Objective-C from JavaScript in an iPhone UIWebView
Javascript callback in Objective-C
YouTube iFrame API
aggiornato con un Esempio:
nella delegato di UIWebView, ho messo:
#pragma - mark WebView Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqualToString:@"callback"]) {
NSLog(@"get callback");
return NO;
}
return YES;
}
la pagina web è carico quando viewDidLoad
:
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"youtube" ofType:@"html" ]]]];
e youtube.html ho messo:
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:anything"; //here's the key
};
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
si può vedere aggiungo
if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:anything";
};
al di YouTube iFrame API demo, e cattura la fine giocatore che gioca eventi e cercare di caricare una richiesta con schema "callback", allora il Delegato di UIWebView potrebbe prenderlo.
È possibile utilizzare questo metodo per attivare qualsiasi evento utilizzando JavaScript;
Ma dipende se il player di youtube supporta tale evento, no? Hai avuto successo? – user1105951
@ user1105951 sì, evento finale di supporto giocatore youtube denominato "onStateChange", e dovrai aggiungere un listener che segua l'API di YouTube JavaScript [API JavaScript di YouTube - Eventi] (https://developers.google.com/youtube/js_api_reference#Events – ZeR0
@ Zero, controllo il tuo link e ricordo di averlo provato. prova a eseguire questa dimostrazione https://developers.google.com/youtube/youtube_player_demo -UN IL browser web IPAD- safari, e vedrai: "hai bisogno di Flash Player 9+ e javascript abilitato per vedere questo video". Il safari – user1105951