Get device current orientation (App Extension)

后端 未结 10 1467
执笔经年
执笔经年 2020-12-09 10:02

How to get device current orientation in an App Extension, I have tried below two methods but no success.

  1. It always return UIDeviceOrientationUnknown

    <
相关标签:
10条回答
  • 2020-12-09 10:52

    In BroadcastExtension you can use sampleBuffer to understand orientation:

    if let orientationAttachment =  CMGetAttachment(sampleBuffer, RPVideoSampleOrientationKey as CFString, nil) as? NSNumber 
    {  
              let orientation = CGImagePropertyOrientation(rawValue: orientationAttachment.uint32Value)   
    }
    
    0 讨论(0)
  • 2020-12-09 10:54

    I know it's late, but the mistake in this question was in this line:

    [[NSNotificationCenter defaultCenter] addObserver:self.view selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    

    addObserver:self.view is wrong, the notification should be attached on self for being called. Work also on iOS8.

    0 讨论(0)
  • 2020-12-09 11:06

    I have done in my custom keyboard extension,Hope It will help you..

    In order to update your custom keyboard when the orientation changes, override viewDidLayoutSubviews in the UIInputViewController. In another word, we can say that viewDidLayoutSubviews always called when rotation done.

    In keyboard extension we're unable to use traditional flow as we usually used:

    [UIApplication sharedApplication] statusBarOrientation]
    

    So to detect current orientation, I used following code: In Objc :

    if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
    // Portrait Orientation
    }
    else{
    //Landscape Orientation
    }
    

    And in swift4, you can use this:

    if UIScreen.main.bounds.size.width > UIScreen.main.bounds.size.height {
            //portrait Orientation
        }
        else
        {
            //landscape Orientation
        }
    
    0 讨论(0)
  • 2020-12-09 11:08

    I haven't been been able to get it to work in an iMessage App Extension. Apple seems to have silently disabled it as far as I can tell. https://forums.developer.apple.com/thread/53981

    0 讨论(0)
提交回复
热议问题