4

First off, I have viewed many of the other threads on force portrait/landscape orientation for iOS.

I have a hybrid application that is built using IBM Worklight (I'm mainly focused on the iOS platform, since I have a solution already for Android that's working fine). I've added a cordova plugin to gain access to the device features. My application needs to be set to portrait on all screens except one. On one screen(view), I need to support landscape and portrait. However, when I navigate to another screen, I need to revert back to portrait only, but in my many attempts, its staying in landscape.

I'm using supportedInterfaceOrientations and shouldAutorotate methods to lock & unlock and switch between supporting only portrait or landscape & portrait.

Note: My application has only one view controller which contains the webview, so I can't use solutions to dismiss and present modal view controller.

I have also viewed other threads mentioning force rotation using transform in iOS, but that doesn't seem to work either. Is there a documented way to set the iPhone orientation?

(My app supports iOS7 and above) The only thing that has worked for me is

[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

and

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait );

However, Apple will reject my app if I use these because of private api.

Is there some other way to force an orientation to portrait while the device is rotated to landscape?

Community
  • 1
  • 1
user1361551
  • 159
  • 1
  • 9
  • When you say "screens", are these HTML pages, or native pages? It is not clear if your Cordova code is done only on the background (a plug-in that implements native code), or also goes to a native page using WL.NativePages – Idan Adar Jun 20 '14 at 03:30
  • It's all HTML pages. I'm using a custom Cordova plugin by creating a class in Xcode in order to lock and unlock orientation on certain HTML pages. Since the app only has one view controller which houses the webview, I'm manipulating its supportInterfaces and shouldautorotate functions to unlock and lock orientation. – user1361551 Jun 23 '14 at 11:57

2 Answers2

1

I know a weird way of forcing orientation in phonegap plugin for ios. I am adding the ios and js code in the answer.

IOS

Below is the plugin i am using to set a count1=1 which will be used to force orientation.

HelloworldPlugin.m

#import "HelloworlPlugin.h"

@implementation HelloworlPlugin

int count=0;



-(void)sayhello:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)option
{
    [arguments pop];
    NSString *responseString=[NSString stringWithFormat:@"%@",[arguments objectAtIndex:0]];

 if([responseString isEqualToString:@"1"])
 {
     count=1;
 }
    else
    {
        count=0;
    }
    NSLog(@"Zero %@",responseString);
}

-(int)count1
{
    NSLog(@"count= %d",count);
    if(count<1)
    {

        Cv=0;
    }
    else
    {
        Cv=1;
    }

    NSLog(@"%d",Cv);
    return Cv;


}

@end

Below is the MainViewcontroller.h which has the shouldAutorotateTOInterfaceOrientation function in which based on the values of the count1 i am forcing only portrait in this code, you can set various values for count1 and based on which you can force orientation her

CDVMainViewController.h

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    HelloworlPlugin *object=[[HelloworlPlugin alloc] init];

    int fValue=[object count1];

    NSLog(@"%d",fValue);
    if(fValue==1)
    {
    return (interfaceOrientation ==UIInterfaceOrientationPortrait);

    }
    else
    {
        return true;
    }
}

clickbutton() is the button which is used to turn to portrait.The parameter 1 i am passed is being set as count 1 in the above objective c programming.

function clickbutton()
{
    cordova.exec(success,failure,"HelloworlPlugin","sayhello",[1]);
}

I have just given a example which you can blow up a little and use according to your needs.

BalajiG
  • 544
  • 2
  • 15
0

I recently faced a similar issue while using worklight 6.2. Even when I was setting the suppportedOrientatations property in my appname.plist file it was not working. In worklight 6.1 it worked perfectly alright when I set the above mentioned property, you dont need to do any other setting.