diff --git a/README.md b/README.md index 05117dd..2ad7bd5 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,9 @@ Issue [#1](https://github.com/yoik/cordova-yoik-screenorientation/issues/1) @dok >It seems to be related to having width=device-width, height=device-height in the meta viewport (which is part of the boilerplate phonegap/cordova app). It can be solved by updating the viewport with width=device-height, height=device-width or simply removing width and height altogether. +####iOS8 + +Versions prior to 1.2.0 will cause an application crash in iOS8. ##BB10 Notes diff --git a/plugin.xml b/plugin.xml index d710d69..33a7849 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="1.2.0"> Screen Orientation Adds Screen Orientation API lock and unlock functions to the global screen object in android, iOS and BB10. diff --git a/src/ios/YoikScreenOrientation.m b/src/ios/YoikScreenOrientation.m index 34d5fda..e1f077b 100644 --- a/src/ios/YoikScreenOrientation.m +++ b/src/ios/YoikScreenOrientation.m @@ -30,15 +30,41 @@ SOFTWARE. // this method does not control the orientation, it is set in the .js file. // SEE https://github.com/Adlotto/cordova-plugin-recheck-screen-orientation - // ------------------ - // HACK: Force rotate by changing the view hierarchy. Present modal view then dismiss it immediately. - [self.viewController presentViewController:[UIViewController new] animated:NO completion:^{ [self.viewController dismissViewControllerAnimated:NO completion:nil]; }]; - // Assume everything went ok - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + UIViewController *vc = [[UIViewController alloc] init]; + vc.view.alpha = 0; + + [self.viewController presentViewController:vc animated:NO completion:^{ + // added to support iOS8 beta 5, @see issue #19 + dispatch_after(0, dispatch_get_main_queue(), ^{ + [self.viewController dismissViewControllerAnimated:NO completion:nil]; + }); + }]; + + // grab the device orientation so we can pass it back to the js side. + NSString *orientation; + switch ([[UIDevice currentDevice] orientation]) { + case UIDeviceOrientationLandscapeLeft: + orientation = @"landscape-secondary"; + break; + case UIDeviceOrientationLandscapeRight: + orientation = @"landscape-primary"; + break; + case UIDeviceOrientationPortrait: + orientation = @"portrait-primary"; + break; + case UIDeviceOrientationPortraitUpsideDown: + orientation = @"portrait-secondary"; + break; + default: + orientation = @"portait"; + break; + } + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK + messageAsDictionary:@{@"device":orientation}]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - // ------------------ } @end \ No newline at end of file diff --git a/www/screenorientation.ios.js b/www/screenorientation.ios.js index 50e2c6e..22ed12d 100644 --- a/www/screenorientation.ios.js +++ b/www/screenorientation.ios.js @@ -1,35 +1,37 @@ var exec = require('cordova/exec'), - screenOrientation = {}; + screenOrientation = {}, + iosOrientation = 'unlocked', + orientationMap = { + 'portrait': [0,180], + 'portrait-primary': [0], + 'portrait-secondary': [180], + 'landscape': [-90,90], + 'landscape-primary': [-90], + 'landscape-secondary': [90], + 'default': [-90,90,0] + }; screenOrientation.setOrientation = function(orientation) { - exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', orientation]); + iosOrientation = orientation; + + var success = function(res) { + if (orientation === 'unlocked' && res.device) { + iosOrientation = res.device; + + setTimeout(function() { + iosOrientation = 'unlocked'; + },0); + } + }; + + exec(success, null, "YoikScreenOrientation", "screenOrientation", ['set', orientation]); }; module.exports = screenOrientation; // ios orientation callback/hook window.shouldRotateToOrientation = function(orientation) { - var currOrientation = cordova.plugins.screenorientation.currOrientation; - switch (currOrientation) { - case 'portrait': - case 'portrait-primary': - if (orientation === 0) return true; - break; - case 'landscape': - case 'landscape-primary': - if (orientation === -90) return true; - break; - case 'landscape': - case 'landscape-secondary': - if (orientation === 90) return true; - break; - case 'portrait': - case 'portrait-secondary': - if (orientation === 180) return true; - break; - default: - if (orientation === -90 || orientation === 90 || orientation === 0) return true; - break; - } - return false; -}; \ No newline at end of file + var currOrientation = iosOrientation, + map = orientationMap[currOrientation] || orientationMap['default']; + return map.indexOf(orientation) >= 0; +};