myData
myData

Reputation: 215

UIWebView video playback landscape mode works in iOS 7, not iOS 8

I have an app that I've enabled all viewing modes except portrait upside-down in my project. This works in iOS7 for video playback in portrait and landscape mode. Then, iOS 8 happened. In my app delegate:

var fullScreenVideoIsPlaying: Bool = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
    if fullScreenVideoIsPlaying == false || UIApplication.sharedApplication().statusBarOrientation.isLandscape {
        return Int(UIInterfaceOrientationMask.Portrait.toRaw())
    } else {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw())
    }
}

in my view controller, I am using 2 notifications:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeView:", name:UIWindowDidBecomeVisibleNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoPlayback:", name:UIWindowDidBecomeHiddenNotification, object: self.view.window)

Notification methods called:

func changeView(sender:AnyObject) {
    let string = "\(sender.object)"
    if string.rangeOfString("MPFullscreenWindow") != nil {
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        appDelegate.fullScreenVideoIsPlaying = false
    }
}

func videoPlayback(sender:AnyObject) {
    let string = "\(sender.object)"
    if string.rangeOfString("MPFullscreenWindow") != nil {
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        appDelegate.fullScreenVideoIsPlaying = true
    }
}

my webView...I am playing my video in a webview, not my choice and I cannot change this

func setupVideoWebView() {
    let request = NSURLRequest(URL: NSURL(string: "https://www.youtube.com/watch?v=3U7xtCBmieQ"))
    videoWebView.loadRequest(request)

    videoWebView.scrollView.scrollEnabled = false
    videoWebView.scrollView.bounces       = false
    videoWebView.delegate                 = self        

    self.view.addSubview(videoWebView)
}

This works in iOS 7, I knew it was risky b/c I was looking for a string that could change. Well, it did, for iOS 8. In iOS 8, I see this in my console after rotating app:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "=10)-[UIImageView:0x7fa964a8c9b0] (Names: '|':AVAudioOnlyIndicatorView:0x7fa964ac8550 )>", "",

it goes on for a while. It leads me to think that iOS doesn't like my Portrait/Landscape restrictions. Because I'm not using the MPMoviePlayerViewController directly, I cannot use help like this:. I've tried added contraints to

func webViewDidFinishLoad(webView: UIWebView)

but no luck. Is Autolayout my best bet? I keep seeing it as suggestion on other similar problems but cannot get it to work for this purpose. Any help is greatly appreciated.

Upvotes: 1

Views: 1325

Answers (1)

Asilisoft
Asilisoft

Reputation: 331

I have the same problem and I have just found a workaround to solve it.

  1. Create a property in app delegate of type UIWindow, I called it: "youTube".

  2. Add this code to UIViewController that includes the UIWebView:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(youTubeWindowOpened:)
                                             name:UIWindowDidBecomeVisibleNotification
                                           object:self.view.window
    ];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(youTubeWindowClosed:)
                                             name:UIWindowDidBecomeHiddenNotification
                                           object:self.view.window
    ];
    
  3. Then, write these new two methods:

    - (void)youTubeWindowOpened:(NSNotification *)sender
    {
       appDelegate.youTube = (UIWindow *)sender.object;
    }
    
    - (void)youTubeWindowClosed:(NSNotification *)sender
    {
       appDelegate.youTube = nil;
    }
    
  4. Finally, modify your "- application:supportedInterfaceOrientationsForWindow:" in AppDelegate:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        NSString *nom = NSStringFromClass([[[UIApplication sharedApplication] keyWindow] class]);
    
        // iOS 8
        if (youTube && youTube == window) {
            return UIInterfaceOrientationMaskLandscapeRight;
        }
    
        // iOS < 8
        else if ([nom isEqualToString:@"MPFullscreenWindow"]) {
            return UIInterfaceOrientationMaskLandscapeRight;
        }
        else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

I know it isn't a perfect solution, but I hope it helps!!

Upvotes: 3

Related Questions