[Résolu] Problème rotation UIWebView

savepandasavepanda Membre
avril 2010 modifié dans Langages Web & serveurs #1
Bonjour à  tous,

J'ai crée une application qui intègre un portail dans une webview.
Cette webview est contenue dans la vue de mon ViewController. Lorsque j'effectue une rotation avec l'iPhone, le viewcontroller
modifie bien sa vue pour l'orienter correctement en portrait ou en paysage.
Jusque là  tout est ok.

Le problème vient du fait que dans le code javascript du portail web, il intercepte la rotation avec l'évènement
onorientationchange afin de modifier la vue pour en afficher une autre.
Lorsque je teste le portail dans Safari Mobile, ce comportement est vérifié, alors que dans mon application en utilisant la webview la vue en paysage ne change pas, elle est juste redimensionnée.

Je pense que le problème vient du fait que l'évènement onorientationchange ne semble pas être déclenché au sein de la WebView. J'ai même essayé à  la fin de la rotation de de faire cet appel :
[webView stringByEvaluatingJavaScriptFromString:@"window.onorientationchange();"];


Avez-vous déjà  eu un problème similaire avec la WebView ?


Merci d'avance.



Réponses

  • savepandasavepanda Membre
    01:20 modifié #2
    Finalement j'ai trouvé une solution à  mon problème sur stackoverflow.

    Il suffit de redéfinir le getter de la variable orientation de l'objet window en javascript lors de la détection de la rotation et de l'appel de la méthode willRotateToInterfaceOrientation:duration: du ViewController courant :

    <br />- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration<br />{<br />&nbsp; &nbsp; switch (toInterfaceOrientation)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; case UIDeviceOrientationPortrait:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [webView stringByEvaluatingJavaScriptFromString:@&quot;window.__defineGetter__(&#39;orientation&#39;,function(){return 0;});window.onorientationchange();&quot;];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; case UIDeviceOrientationLandscapeLeft:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [webView stringByEvaluatingJavaScriptFromString:@&quot;window.__defineGetter__(&#39;orientation&#39;,function(){return 90;});window.onorientationchange();&quot;];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; case UIDeviceOrientationLandscapeRight:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [webView stringByEvaluatingJavaScriptFromString:@&quot;window.__defineGetter__(&#39;orientation&#39;,function(){return -90;});window.onorientationchange();&quot;];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; case UIDeviceOrientationPortraitUpsideDown:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [webView stringByEvaluatingJavaScriptFromString:@&quot;window.__defineGetter__(&#39;orientation&#39;,function(){return 180;});window.onorientationchange();&quot;];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; default:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; }<br />}<br />
    



    J'ai eu aussi un autre soucis avec une page web utilisant JQuery Touch. J'ai dû forcé l'appel du trigger orientationchange sur l'élément body :

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {<br />	[mainWebView stringByEvaluatingJavaScriptFromString:@&quot;$(&#092;&quot;body&#092;&quot;).trigger(&#092;&quot;orientationchange&#092;&quot;);&quot;]; <br />}
    



    En espérant que cela puisse aider ceux qui ont pester avec ce bug de la webview pendant des heures ou des jours ;).
Connectez-vous ou Inscrivez-vous pour répondre.