You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Michal Mocny <mm...@google.com> on 2013/07/05 16:18:51 UTC

iOS: shouldRotateToOrientation javascript handler

iOS seems to have an undocumented "feature" to call into javascript before
every device orientation change (pasted below for your viewing pleasure).

I can't find any such similar functionality on other platforms, should it
be removed?  Likely we will want to write a screen orientation (
http://www.w3.org/TR/screen-orientation/) plugin instead (eventually).


-
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // First, ask the webview via JS if it supports the new orientation
    NSString* jsCall = [NSString stringWithFormat:
        @"window.shouldRotateToOrientation &&
window.shouldRotateToOrientation(%d);"
        , [self mapIosOrientationToJsOrientation:interfaceOrientation]];
    NSString* res = [webView stringByEvaluatingJavaScriptFromString:jsCall];

    if ([res length] > 0) {
        return [res boolValue];
    }

    // if js did not handle the new orientation (no return value), use
values from the plist (via supportedOrientations)
    return [self supportsOrientation:interfaceOrientation];
}

-Michal

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Shazron <sh...@gmail.com>.
CDVInAppBrowser kinda does this already (hook into orientation changes)
https://github.com/apache/cordova-ios/blob/2.9.0/CordovaLib/Classes/CDVInAppBrowser.m#L730-L756
https://github.com/apache/cordova-ios/blob/2.9.0/CordovaLib/Classes/CDVInAppBrowser.m#L99-L101
https://github.com/apache/cordova-ios/blob/master/CordovaLib/Classes/CDVScreenOrientationDelegate.h
https://github.com/apache/cordova-ios/blob/master/CordovaLib/Classes/CDVViewController.h#L30


On Fri, Jul 5, 2013 at 10:04 AM, Shazron <sh...@gmail.com> wrote:

> +1. We planned to make this cross-platform sometime ago, but nothing came
> of it (see comments): https://issues.apache.org/jira/browse/CB-751
>
>
>
> On Fri, Jul 5, 2013 at 9:58 AM, Michal Mocny <mm...@chromium.org> wrote:
>
>> Yes, the use cases are real, but I thought it was odd that we only have it
>> for iOS.  Also odd that there is overhead for every rotation for every
>> app,
>> regardless of using this feature.  We could perhaps add a config.xml flag
>> to disable it by default, but its probably best to just replace it
>> altogether with a plugin that works on multiple platforms.
>>
>>
>> On Fri, Jul 5, 2013 at 12:51 PM, Shazron <sh...@gmail.com> wrote:
>>
>> > If we have a suitable replacement to point users to - I'm all for it.
>> The
>> > use case for this is if a user has an app that is portrait only, but
>> needs
>> > to display something (video?) in landscape thus "overriding" the app
>> > default.s
>> >
>> >
>> > On Fri, Jul 5, 2013 at 9:11 AM, Michal Mocny <mm...@chromium.org>
>> wrote:
>> >
>> > > Yeah, sorry, I meant the JS method: window.shouldRotateToOrientation
>> is
>> > > undocumented and not supported on other platforms.
>> > >
>> > >
>> > > On Fri, Jul 5, 2013 at 11:47 AM, Andrew Grieve <ag...@chromium.org>
>> > > wrote:
>> > >
>> > > > I think he means it's undocumented within Cordova's docs.
>> > > >
>> > > >
>> > > > On Fri, Jul 5, 2013 at 11:20 AM, Ian Clelland <iclelland@google.com
>> >
>> > > > wrote:
>> > > >
>> > > > > I'm not sure what you mean by "Undocumented" in this case -- it's
>> > not a
>> > > > > private API that would get you booted from the App Store; it's a
>> > > > documented
>> > > > > method on UIView, although it's been deprecated recently.[1]
>> > > > >
>> > > > > Agreed that supporting the screen-orientation spec is probably the
>> > way
>> > > to
>> > > > > go in the future, on any platforms that can support it.
>> > > > >
>> > > > > [1]
>> > > > >
>> > > > >
>> > > >
>> > >
>> >
>> http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9
>> > > > >
>> > > > >
>> > > > > On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com>
>> > > wrote:
>> > > > >
>> > > > > > iOS seems to have an undocumented "feature" to call into
>> javascript
>> > > > > before
>> > > > > > every device orientation change (pasted below for your viewing
>> > > > pleasure).
>> > > > > >
>> > > > > > I can't find any such similar functionality on other platforms,
>> > > should
>> > > > it
>> > > > > > be removed?  Likely we will want to write a screen orientation (
>> > > > > > http://www.w3.org/TR/screen-orientation/) plugin instead
>> > > (eventually).
>> > > > > >
>> > > > > >
>> > > > > > -
>> > > > > >
>> > > > > >
>> > > > >
>> > > >
>> > >
>> >
>> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
>> > > > > > {
>> > > > > >     // First, ask the webview via JS if it supports the new
>> > > orientation
>> > > > > >     NSString* jsCall = [NSString stringWithFormat:
>> > > > > >         @"window.shouldRotateToOrientation &&
>> > > > > > window.shouldRotateToOrientation(%d);"
>> > > > > >         , [self
>> > > > mapIosOrientationToJsOrientation:interfaceOrientation]];
>> > > > > >     NSString* res = [webView
>> > > > > > stringByEvaluatingJavaScriptFromString:jsCall];
>> > > > > >
>> > > > > >     if ([res length] > 0) {
>> > > > > >         return [res boolValue];
>> > > > > >     }
>> > > > > >
>> > > > > >     // if js did not handle the new orientation (no return
>> value),
>> > > use
>> > > > > > values from the plist (via supportedOrientations)
>> > > > > >     return [self supportsOrientation:interfaceOrientation];
>> > > > > > }
>> > > > > >
>> > > > > > -Michal
>> > > > > >
>> > > > >
>> > > >
>> > >
>> >
>>
>
>

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Shazron <sh...@gmail.com>.
+1. We planned to make this cross-platform sometime ago, but nothing came
of it (see comments): https://issues.apache.org/jira/browse/CB-751



On Fri, Jul 5, 2013 at 9:58 AM, Michal Mocny <mm...@chromium.org> wrote:

> Yes, the use cases are real, but I thought it was odd that we only have it
> for iOS.  Also odd that there is overhead for every rotation for every app,
> regardless of using this feature.  We could perhaps add a config.xml flag
> to disable it by default, but its probably best to just replace it
> altogether with a plugin that works on multiple platforms.
>
>
> On Fri, Jul 5, 2013 at 12:51 PM, Shazron <sh...@gmail.com> wrote:
>
> > If we have a suitable replacement to point users to - I'm all for it. The
> > use case for this is if a user has an app that is portrait only, but
> needs
> > to display something (video?) in landscape thus "overriding" the app
> > default.s
> >
> >
> > On Fri, Jul 5, 2013 at 9:11 AM, Michal Mocny <mm...@chromium.org>
> wrote:
> >
> > > Yeah, sorry, I meant the JS method: window.shouldRotateToOrientation is
> > > undocumented and not supported on other platforms.
> > >
> > >
> > > On Fri, Jul 5, 2013 at 11:47 AM, Andrew Grieve <ag...@chromium.org>
> > > wrote:
> > >
> > > > I think he means it's undocumented within Cordova's docs.
> > > >
> > > >
> > > > On Fri, Jul 5, 2013 at 11:20 AM, Ian Clelland <ic...@google.com>
> > > > wrote:
> > > >
> > > > > I'm not sure what you mean by "Undocumented" in this case -- it's
> > not a
> > > > > private API that would get you booted from the App Store; it's a
> > > > documented
> > > > > method on UIView, although it's been deprecated recently.[1]
> > > > >
> > > > > Agreed that supporting the screen-orientation spec is probably the
> > way
> > > to
> > > > > go in the future, on any platforms that can support it.
> > > > >
> > > > > [1]
> > > > >
> > > > >
> > > >
> > >
> >
> http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9
> > > > >
> > > > >
> > > > > On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com>
> > > wrote:
> > > > >
> > > > > > iOS seems to have an undocumented "feature" to call into
> javascript
> > > > > before
> > > > > > every device orientation change (pasted below for your viewing
> > > > pleasure).
> > > > > >
> > > > > > I can't find any such similar functionality on other platforms,
> > > should
> > > > it
> > > > > > be removed?  Likely we will want to write a screen orientation (
> > > > > > http://www.w3.org/TR/screen-orientation/) plugin instead
> > > (eventually).
> > > > > >
> > > > > >
> > > > > > -
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> > > > > > {
> > > > > >     // First, ask the webview via JS if it supports the new
> > > orientation
> > > > > >     NSString* jsCall = [NSString stringWithFormat:
> > > > > >         @"window.shouldRotateToOrientation &&
> > > > > > window.shouldRotateToOrientation(%d);"
> > > > > >         , [self
> > > > mapIosOrientationToJsOrientation:interfaceOrientation]];
> > > > > >     NSString* res = [webView
> > > > > > stringByEvaluatingJavaScriptFromString:jsCall];
> > > > > >
> > > > > >     if ([res length] > 0) {
> > > > > >         return [res boolValue];
> > > > > >     }
> > > > > >
> > > > > >     // if js did not handle the new orientation (no return
> value),
> > > use
> > > > > > values from the plist (via supportedOrientations)
> > > > > >     return [self supportsOrientation:interfaceOrientation];
> > > > > > }
> > > > > >
> > > > > > -Michal
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Michal Mocny <mm...@chromium.org>.
Yes, the use cases are real, but I thought it was odd that we only have it
for iOS.  Also odd that there is overhead for every rotation for every app,
regardless of using this feature.  We could perhaps add a config.xml flag
to disable it by default, but its probably best to just replace it
altogether with a plugin that works on multiple platforms.


On Fri, Jul 5, 2013 at 12:51 PM, Shazron <sh...@gmail.com> wrote:

> If we have a suitable replacement to point users to - I'm all for it. The
> use case for this is if a user has an app that is portrait only, but needs
> to display something (video?) in landscape thus "overriding" the app
> default.s
>
>
> On Fri, Jul 5, 2013 at 9:11 AM, Michal Mocny <mm...@chromium.org> wrote:
>
> > Yeah, sorry, I meant the JS method: window.shouldRotateToOrientation is
> > undocumented and not supported on other platforms.
> >
> >
> > On Fri, Jul 5, 2013 at 11:47 AM, Andrew Grieve <ag...@chromium.org>
> > wrote:
> >
> > > I think he means it's undocumented within Cordova's docs.
> > >
> > >
> > > On Fri, Jul 5, 2013 at 11:20 AM, Ian Clelland <ic...@google.com>
> > > wrote:
> > >
> > > > I'm not sure what you mean by "Undocumented" in this case -- it's
> not a
> > > > private API that would get you booted from the App Store; it's a
> > > documented
> > > > method on UIView, although it's been deprecated recently.[1]
> > > >
> > > > Agreed that supporting the screen-orientation spec is probably the
> way
> > to
> > > > go in the future, on any platforms that can support it.
> > > >
> > > > [1]
> > > >
> > > >
> > >
> >
> http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9
> > > >
> > > >
> > > > On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com>
> > wrote:
> > > >
> > > > > iOS seems to have an undocumented "feature" to call into javascript
> > > > before
> > > > > every device orientation change (pasted below for your viewing
> > > pleasure).
> > > > >
> > > > > I can't find any such similar functionality on other platforms,
> > should
> > > it
> > > > > be removed?  Likely we will want to write a screen orientation (
> > > > > http://www.w3.org/TR/screen-orientation/) plugin instead
> > (eventually).
> > > > >
> > > > >
> > > > > -
> > > > >
> > > > >
> > > >
> > >
> >
> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> > > > > {
> > > > >     // First, ask the webview via JS if it supports the new
> > orientation
> > > > >     NSString* jsCall = [NSString stringWithFormat:
> > > > >         @"window.shouldRotateToOrientation &&
> > > > > window.shouldRotateToOrientation(%d);"
> > > > >         , [self
> > > mapIosOrientationToJsOrientation:interfaceOrientation]];
> > > > >     NSString* res = [webView
> > > > > stringByEvaluatingJavaScriptFromString:jsCall];
> > > > >
> > > > >     if ([res length] > 0) {
> > > > >         return [res boolValue];
> > > > >     }
> > > > >
> > > > >     // if js did not handle the new orientation (no return value),
> > use
> > > > > values from the plist (via supportedOrientations)
> > > > >     return [self supportsOrientation:interfaceOrientation];
> > > > > }
> > > > >
> > > > > -Michal
> > > > >
> > > >
> > >
> >
>

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Shazron <sh...@gmail.com>.
If we have a suitable replacement to point users to - I'm all for it. The
use case for this is if a user has an app that is portrait only, but needs
to display something (video?) in landscape thus "overriding" the app
default.s


On Fri, Jul 5, 2013 at 9:11 AM, Michal Mocny <mm...@chromium.org> wrote:

> Yeah, sorry, I meant the JS method: window.shouldRotateToOrientation is
> undocumented and not supported on other platforms.
>
>
> On Fri, Jul 5, 2013 at 11:47 AM, Andrew Grieve <ag...@chromium.org>
> wrote:
>
> > I think he means it's undocumented within Cordova's docs.
> >
> >
> > On Fri, Jul 5, 2013 at 11:20 AM, Ian Clelland <ic...@google.com>
> > wrote:
> >
> > > I'm not sure what you mean by "Undocumented" in this case -- it's not a
> > > private API that would get you booted from the App Store; it's a
> > documented
> > > method on UIView, although it's been deprecated recently.[1]
> > >
> > > Agreed that supporting the screen-orientation spec is probably the way
> to
> > > go in the future, on any platforms that can support it.
> > >
> > > [1]
> > >
> > >
> >
> http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9
> > >
> > >
> > > On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com>
> wrote:
> > >
> > > > iOS seems to have an undocumented "feature" to call into javascript
> > > before
> > > > every device orientation change (pasted below for your viewing
> > pleasure).
> > > >
> > > > I can't find any such similar functionality on other platforms,
> should
> > it
> > > > be removed?  Likely we will want to write a screen orientation (
> > > > http://www.w3.org/TR/screen-orientation/) plugin instead
> (eventually).
> > > >
> > > >
> > > > -
> > > >
> > > >
> > >
> >
> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> > > > {
> > > >     // First, ask the webview via JS if it supports the new
> orientation
> > > >     NSString* jsCall = [NSString stringWithFormat:
> > > >         @"window.shouldRotateToOrientation &&
> > > > window.shouldRotateToOrientation(%d);"
> > > >         , [self
> > mapIosOrientationToJsOrientation:interfaceOrientation]];
> > > >     NSString* res = [webView
> > > > stringByEvaluatingJavaScriptFromString:jsCall];
> > > >
> > > >     if ([res length] > 0) {
> > > >         return [res boolValue];
> > > >     }
> > > >
> > > >     // if js did not handle the new orientation (no return value),
> use
> > > > values from the plist (via supportedOrientations)
> > > >     return [self supportsOrientation:interfaceOrientation];
> > > > }
> > > >
> > > > -Michal
> > > >
> > >
> >
>

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Michal Mocny <mm...@chromium.org>.
Yeah, sorry, I meant the JS method: window.shouldRotateToOrientation is
undocumented and not supported on other platforms.


On Fri, Jul 5, 2013 at 11:47 AM, Andrew Grieve <ag...@chromium.org> wrote:

> I think he means it's undocumented within Cordova's docs.
>
>
> On Fri, Jul 5, 2013 at 11:20 AM, Ian Clelland <ic...@google.com>
> wrote:
>
> > I'm not sure what you mean by "Undocumented" in this case -- it's not a
> > private API that would get you booted from the App Store; it's a
> documented
> > method on UIView, although it's been deprecated recently.[1]
> >
> > Agreed that supporting the screen-orientation spec is probably the way to
> > go in the future, on any platforms that can support it.
> >
> > [1]
> >
> >
> http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9
> >
> >
> > On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com> wrote:
> >
> > > iOS seems to have an undocumented "feature" to call into javascript
> > before
> > > every device orientation change (pasted below for your viewing
> pleasure).
> > >
> > > I can't find any such similar functionality on other platforms, should
> it
> > > be removed?  Likely we will want to write a screen orientation (
> > > http://www.w3.org/TR/screen-orientation/) plugin instead (eventually).
> > >
> > >
> > > -
> > >
> > >
> >
> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> > > {
> > >     // First, ask the webview via JS if it supports the new orientation
> > >     NSString* jsCall = [NSString stringWithFormat:
> > >         @"window.shouldRotateToOrientation &&
> > > window.shouldRotateToOrientation(%d);"
> > >         , [self
> mapIosOrientationToJsOrientation:interfaceOrientation]];
> > >     NSString* res = [webView
> > > stringByEvaluatingJavaScriptFromString:jsCall];
> > >
> > >     if ([res length] > 0) {
> > >         return [res boolValue];
> > >     }
> > >
> > >     // if js did not handle the new orientation (no return value), use
> > > values from the plist (via supportedOrientations)
> > >     return [self supportsOrientation:interfaceOrientation];
> > > }
> > >
> > > -Michal
> > >
> >
>

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Andrew Grieve <ag...@chromium.org>.
I think he means it's undocumented within Cordova's docs.


On Fri, Jul 5, 2013 at 11:20 AM, Ian Clelland <ic...@google.com> wrote:

> I'm not sure what you mean by "Undocumented" in this case -- it's not a
> private API that would get you booted from the App Store; it's a documented
> method on UIView, although it's been deprecated recently.[1]
>
> Agreed that supporting the screen-orientation spec is probably the way to
> go in the future, on any platforms that can support it.
>
> [1]
>
> http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9
>
>
> On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com> wrote:
>
> > iOS seems to have an undocumented "feature" to call into javascript
> before
> > every device orientation change (pasted below for your viewing pleasure).
> >
> > I can't find any such similar functionality on other platforms, should it
> > be removed?  Likely we will want to write a screen orientation (
> > http://www.w3.org/TR/screen-orientation/) plugin instead (eventually).
> >
> >
> > -
> >
> >
> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> > {
> >     // First, ask the webview via JS if it supports the new orientation
> >     NSString* jsCall = [NSString stringWithFormat:
> >         @"window.shouldRotateToOrientation &&
> > window.shouldRotateToOrientation(%d);"
> >         , [self mapIosOrientationToJsOrientation:interfaceOrientation]];
> >     NSString* res = [webView
> > stringByEvaluatingJavaScriptFromString:jsCall];
> >
> >     if ([res length] > 0) {
> >         return [res boolValue];
> >     }
> >
> >     // if js did not handle the new orientation (no return value), use
> > values from the plist (via supportedOrientations)
> >     return [self supportsOrientation:interfaceOrientation];
> > }
> >
> > -Michal
> >
>

Re: iOS: shouldRotateToOrientation javascript handler

Posted by Ian Clelland <ic...@google.com>.
I'm not sure what you mean by "Undocumented" in this case -- it's not a
private API that would get you booted from the App Store; it's a documented
method on UIView, although it's been deprecated recently.[1]

Agreed that supporting the screen-orientation spec is probably the way to
go in the future, on any platforms that can support it.

[1]
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#jumpTo_9


On Fri, Jul 5, 2013 at 10:18 AM, Michal Mocny <mm...@google.com> wrote:

> iOS seems to have an undocumented "feature" to call into javascript before
> every device orientation change (pasted below for your viewing pleasure).
>
> I can't find any such similar functionality on other platforms, should it
> be removed?  Likely we will want to write a screen orientation (
> http://www.w3.org/TR/screen-orientation/) plugin instead (eventually).
>
>
> -
>
> (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> {
>     // First, ask the webview via JS if it supports the new orientation
>     NSString* jsCall = [NSString stringWithFormat:
>         @"window.shouldRotateToOrientation &&
> window.shouldRotateToOrientation(%d);"
>         , [self mapIosOrientationToJsOrientation:interfaceOrientation]];
>     NSString* res = [webView
> stringByEvaluatingJavaScriptFromString:jsCall];
>
>     if ([res length] > 0) {
>         return [res boolValue];
>     }
>
>     // if js did not handle the new orientation (no return value), use
> values from the plist (via supportedOrientations)
>     return [self supportsOrientation:interfaceOrientation];
> }
>
> -Michal
>