Why doesn't my Cordova/PhoneGap iOS app rotate when the device rotates?

北城以北 提交于 2019-12-28 02:06:27

问题


I am trying to make a landscape only app, but I am not able to produce any rotation at all.

There used to be a autorotate setting in PhoneGap.plist but in phonegap 1.8.0 I can find it. Does it still exists?

What else could be wrong that my application is not rotating?

UPDATE

I know have webpage containing only one word "test". I set target device to iPad only and enabled all four orientations. What could still be wrong?

Do need to have a special html document type? Do I need to include some cordova-1.8.0.js? I could not find one for iOS (!?!) so i tested it with the android version. I read the API is now the same so can I use the android .js file?


回答1:


PiTheNumber's answer looks OK for those fine with modifying the Cordova-generated native code.

Following this JIRA issue on Cordova, and as neatly explained in this blog, you can also use plist values or define a window.shouldRotateToOrientation function in your Javascript code, which suits me very well.

window.shouldRotateToOrientation = function(degrees) {
 return true;
}

This would enable device orientation for the current page (so, for your whole app if it's a "one page app" as most Cordova apps are). Note you can also decide to enable it based on the rotation value in degrees, or even, why not, enable it only on certain views, or letting the user choose within your HTML app... Nice, isn't it.

For the record, I didn't need to do anything to get an iOS 8 iPad handle rotation, while both iOS 6 and iOS 7 iPhones wouldn't handle it by default in the current Cordova version (4.2.0, cordova ios platform version "ios 3.7.0"). That is because one can grant different rotation settings per "device type" (tablet / phone) on Xcode. The thing to note is that Cordova will first check the JS function above if it exists, and then if the function does not exist or it did not allow the rotation, the Xcode rotation setting will be used.




回答2:


I tried the JavaScript solution above and got no joy In Visual Studio 2015 I change the config.xml to

<preference name="orientation" value="all" />

taken from Cordova 5 build command is deleting iOS device orientation settings

I didn't need the javascript just the config setting




回答3:


In Classes/MainViewController.m return true:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return true;
}

For iOS >= 6

- (BOOL)shouldAutorotate {
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

Source




回答4:


You can add UISupportedInterfaceOrientations

platroms/ios/{ProjectName}/{ProjectName-info.plist

add this rows:

For Iphone:

<key>UISupportedInterfaceOrientations</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>

For Ipad:

<key>UISupportedInterfaceOrientations~ipad</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
|improve this answer

来源:https://stackoverflow.com/questions/10999435/why-doesnt-my-cordova-phonegap-ios-app-rotate-when-the-device-rotates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!