UIViewControllers display in Portrait regardless of app orientation


#1

 

I have been struggling to try to get UIViewControllers to display with the correct orientation of my app (landscape mode) for my JUCE 3.07 iOS app for over a week.  Time to ask for some help!  The problem is demonstrated with the code below, based on HelloWorld iOS project (rename Main.cpp to Main.mm and paste in the code below).  For JUCE iOS apps that are in landscape mode, all UIViewControllers (not just UIActivityViewController) display in portrait orientation, despite the rest of the UI being dispalyed in landscape.  Even subclassing a viewcontroller to control the shouldAutorotate method doesn't help, even though the orientation methods are being called.

I have made a simple iOS (non-JUCE) single-view app as a sanity check and it works correctly.

What could be under the hood in JUCE causing this problem?  I believe others have run into it as well, so perhaps we can sort this out...

Thanks-

Steve

/*

  ==============================================================================


   Demonstration "Hello World" application in JUCE

   Copyright 2008 by Julian Storer.


  ==============================================================================

*/


#include "../JuceLibraryCode/JuceHeader.h"

#include "MainComponent.h"


#import <UIKit/UIKit.h>


//==============================================================================

/**

    This is the top-level window that we'll pop up. Inside it, we'll create and

    show a component from the MainComponent.cpp file (you can open this file using

    the Jucer to edit it).

*/



@interface MyUIActivityVC : UIActivityViewController

@end


@implementation MyUIActivityVC


-(id)init

{

    int shareCount=0;

    

    // create a "placeholder" file - we will

    NSString* fileNameWithExtension = @"test.m4v";

    NSString* fileName = [fileNameWithExtension stringByDeletingPathExtension];

    NSString* extension = fileNameWithExtension.pathExtension;

    NSString* newFileName = [NSString stringWithFormat:@"%@-%i.%@", fileName, shareCount++, extension];

    

    NSURL *saveTempURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:newFileName]];

    

    File newFile(File::createTempFile("TempSave"));

    newFile = newFile.withFileExtension(".m4v");

    //    [ssui getMCC]->exportAsset(newFile.getFullPathName(), 2);

    

    // delete it first if it exists

    //    [[NSFileManager defaultManager] createFileAtPath:[saveTempURL path] contents:nil attributes:nil];

    

    NSArray *items = @[[NSURL fileURLWithPath:[NSString stringWithUTF8String:newFile.getFullPathName().toUTF8()]]];//@[saveTempURL];

    

    self = [super initWithActivityItems:items applicationActivities:nil];

    

    self.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard];

    

    self.completionHandler = ^(NSString *activityType, BOOL completed)

    {

        

        NSLog(@" activityType: %@", activityType);

        NSLog(@" completed: %i", completed);

    };

    

    return self;

}


-(NSUInteger)supportedInterfaceOrientationsForWindow

{

    return UIInterfaceOrientationLandscapeRight;

}


- (BOOL)shouldAutorotate

{

    return YES;

}


- (NSUInteger)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskLandscape;

}


- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation

{

    return true;

}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return UIInterfaceOrientationLandscapeRight;

}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

    ;

}


@end



class HelloWorldWindow  : public DocumentWindow

{

public:

    //==============================================================================

    HelloWorldWindow()

        : DocumentWindow ("JUCE Hello World!",

                          Colours::lightgrey,

                          DocumentWindow::allButtons,

                          true)

    {

        // Create an instance of our main content component, and add it to our window..

        setContentOwned (new MainComponent(), true);


        // Centre the window on the screen

        centreWithSize (getWidth(), getHeight());


        // And show it!

        setVisible (true);


        // show the UIActivityViewController (subclassed as MyUIActivityVC to try to control orientation)

        UIResponder* responder = ((UIView*) Component::getCurrentlyFocusedComponent()->getWindowHandle()).nextResponder;

        

        if ([responder isKindOfClass: [UIViewController class]])

        {

            UIViewController* controller = (UIViewController*) responder;

            

            MyUIActivityVC *myuiavc = [[MyUIActivityVC alloc] init];

            

            [controller presentViewController:myuiavc animated:YES completion:nil];

            

        }

        

    }


    ~HelloWorldWindow()

    {

        // (the content component will be deleted automatically, so no need to do it here)

    }


    //==============================================================================

    void closeButtonPressed()

    {

        // When the user presses the close button, we'll tell the app to quit. This

        // HelloWorldWindow object will be deleted by the JUCEHelloWorldApplication class.

        JUCEApplication::quit();

    }

};


//==============================================================================

/** This is the application object that is started up when Juce starts. It handles

    the initialisation and shutdown of the whole application.

*/

class JUCEHelloWorldApplication : public JUCEApplication

{

public:

    //==============================================================================

    JUCEHelloWorldApplication() {}


    //==============================================================================

    void initialise (const String& commandLine)

    {

        // restrict to LANDSCAPE

        Desktop::getInstance().setOrientationsEnabled(Desktop::rotatedClockwise + Desktop::rotatedAntiClockwise);


        // For this demo, we'll just create the main window...

        helloWorldWindow = new HelloWorldWindow();


        /*  ..and now return, which will fall into to the main event

            dispatch loop, and this will run until something calls

            JUCEAppliction::quit().


            In this case, JUCEAppliction::quit() will be called by the

            hello world window being clicked.

        */

    }


    void shutdown()

    {

        // This method is where you should clear-up your app's resources..


        // The helloWorldWindow variable is a ScopedPointer, so setting it to a null

        // pointer will delete the window.

        helloWorldWindow = nullptr;

    }


    //==============================================================================

    const String getApplicationName()

    {

        return "Hello World for JUCE";

    }


    const String getApplicationVersion()

    {

        // The ProjectInfo::versionString value is automatically updated by the Jucer, and

        // can be found in the JuceHeader.h file that it generates for our project.

        return ProjectInfo::versionString;

    }


    bool moreThanOneInstanceAllowed()

    {

        return true;

    }


    void anotherInstanceStarted (const String& commandLine)

    {

    }


private:

    ScopedPointer<HelloWorldWindow> helloWorldWindow;

};



//==============================================================================

// This macro creates the application's main() function..

START_JUCE_APPLICATION (JUCEHelloWorldApplication)