iphoneios7ios8screen-orientationinterface-orientation

Restrict Interface Orientation in iOS 7 and iOS 8


I am trying to implement something where I stuck at one point.

I want some orientation restrictions as follows:

I have tried all the combination of coding. Finally I got solution for iOS 8

**(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
{**


        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

            if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
            {
                if(IS_IPHONE_4_AND_OLDER){

                    printf("Device Type : iPhone 4,4s ");
                    return UIInterfaceOrientationMaskPortrait;
                }
                else if(IS_IPHONE_5){

                    printf("Device Type : iPhone 5,5S/iPod 5 ");
                    return UIInterfaceOrientationMaskPortrait;
                }
                else if(IS_IPHONE_6){

                    printf("Device Type : iPhone 6 ");
                    return UIInterfaceOrientationMaskPortrait;
                }
                else if(IS_IPHONE_6P){

                    printf("Device Type : iPhone 6+ ");
                    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
                }
            }
            return UIInterfaceOrientationMaskPortrait;
        }
        else{

            printf("Device Type : iPad");
            return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
        }
}

By using above code I manage for iOS 8 But it does not work in iOS 7 or less

Please help me to solve this ...

Thank you


Solution

  • Simple but it work very fine. IOS 7.1 and 8

    AppDelegate.h

    @property () BOOL restrictRotation;
    

    AppDelegate.m

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if(self.restrictRotation)
    
        return UIInterfaceOrientationMaskPortrait;
    else
    
        return UIInterfaceOrientationMaskAll;
    

    }

    ViewController

    At top: To Detect Device Type

    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    
    #define IS_IPHONE_5_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
    #define IS_IPHONE_6_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
    #define IS_IPHONE_6P_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
    #define IS_IPHONE_4_AND_OLDER_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height < 568.0f)
    
    #define IS_IPHONE_5_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 568.0f)
    #define IS_IPHONE_6_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 667.0f)
    #define IS_IPHONE_6P_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 736.0f)
    #define IS_IPHONE_4_AND_OLDER_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) < 568.0f)
    
    #define IS_IPHONE_5 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_5_IOS8 : IS_IPHONE_5_IOS7 )
    #define IS_IPHONE_6 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6_IOS8 : IS_IPHONE_6_IOS7 )
    #define IS_IPHONE_6P ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6P_IOS8 : IS_IPHONE_6P_IOS7 )
    #define IS_IPHONE_4_AND_OLDER ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_4_AND_OLDER_IOS8 : IS_IPHONE_4_AND_OLDER_IOS7 )
    

    Then Add Function Below:

    -(void) restrictRotation:(BOOL) restriction{
    
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;}
    

    viewDidLoad

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
            if(IS_IPHONE_6P){
    
                printf("Device Type : iPhone 6+ ");
                [self restrictRotation:NO];
            }
            else{
    
                printf("Device Type : iPhone 6 ");
                [self restrictRotation:YES];
            }
        }
    }
    else{
    
        printf("Device Type : iPad");
        [self restrictRotation:NO];
    }