youtube full screen video not calling viewWillAppear or viewWillRotate - objective-c

So I have a UIWebView in which sometimes it has a youtube embedded player in it. When I play it and go to full screen and rotate and then dismiss the video, it doesn't call the viewWillAppear or the willRotateTo.... why is this?? I need to make some view adjustments when the device is rotated, however when the player is presented, for some reason none of these methods are being called. And yes I have shouldAutoRotateToInterfaceOrientation set correctly. Any idea?

You can use following for your UIwebView problem, viewWillAppear or willRotateTo.. never calls on UIWebView. You can detect the end of full screen mode by observing the #"UIMoviePlayerControllerDidExitFullscreenNotification" mode:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidExitFullscreen:)
name:#"UIMoviePlayerControllerDidExitFullscreenNotification"
object:nil];
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)moviePlayerDidExitFullScreen:(NSNotification *)notification
{
// This is where you do whatever you want.
}

Your controller must be a UIViewController or the viewWillAppear delegate won't be called.

Related

Pause game on orientation change, objective-c / Sprite Kit

I want my game to pause when device orientation is initiated. I have this method in my viewcontroller, which works fine:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"I am starting to rotate, should pause game..");
}
But how to listen for device rotation from within my SKScene, where the actual game is playing. Hope I have made myself clear enough. Thanks for any help!
You can use NSNotification. Keeping with your code, add the following line in your ViewController init:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"RotateNotification"
object:self];
Then in your SKScene init add this line:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"RotateNotification"
object:nil];
Also add the called method to SKScene:
-(void) receiveNotification:(NSNotification *)notification
{
NSLog (#"Received notification");
// do what you have to do here...
}

Setting UIScrollView's Position from another ViewController

I am trying to set the position of a UIScrollView by using contentOffset as such:
- (void) navigateToTableViewPosition:(CGPoint)contentOffset {
NSLog(#"Position set method gets called...");
NSLog(#"%#", NSStringFromCGPoint(contentOffset));
[mainScrollView setContentOffset:contentOffset animated:YES];
}
I call this method from another view controller before I dismiss it, and everything checks out. I pass the argument correctly, and the method gets called (checked it with NSLog), but the scroll view does not move...
What is funny is that when I call this method from the view controller, in which it is located, it works fine. Only when I call it from another view controller, it stops working.
Just for future reference, here is the calling method:
MainViewController *mainView = [[MainViewController alloc] init];
[mainView navigateToTableViewPosition:contentOffset];
Content offset is a CGPoint I set beforehand. It doesn't matter here; besides, it gets passed correctly anyways.
Try this, You have to send notification from other viewcontroller when you want to change ..
[[NSNotificationCenter defaultCenter] postNotificationName:#"changepostion" object:NSStringFromCGPoint(CGPointMake(contentOffset.x, contentOffset.y))];
in mainviewcontroller
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(navigateToTableViewPosition:) name:#"changepostion" object:nil];
}
- (void) navigateToTableViewPosition:(NSNotification *)notification
{
contentOffset =CGPointFromString([notification object]);
NSLog(#"Position set method gets called...");
NSLog(#"%#", NSStringFromCGPoint(contentOffset));
[mainScrollView setContentOffset:contentOffset animated:YES];
}
You can't set the properties of a view which is not visible. If you are using iOS5+ you can implement the offset setting in the completion in the view dismiss completion block.
Use delegate for backward messaging in view controllers.
Refer Basic Delegate Example link for more reference.
Your are making new instance of viewcontroller which will call method but will have no effect.

PageViewController with MPMovieViewController

i'm developing an iOS app for the iPad and used the PageView template. I added some buttons which play some video files. Everything works so far, but the problem is that the touch gestures get called for both views.
My view architecture looks like this
I create a MPMovieViewcontroller, set fullscreen mode and add the view to my pageview:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
mediaView = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaURL];
mediaView.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
mediaView.moviePlayer.fullscreen = YES;
mediaView.moviePlayer.view.exclusiveTouch = YES;
[mediaView shouldAutorotateToInterfaceOrientation:YES];
[mediaView setWantsFullScreenLayout:YES];
[mediaView.moviePlayer prepareToPlay];
[mediaView.moviePlayer play];
[self.view addSubview:mediaView.view];
the problem is that if I try to control the volume slider, that gesture turn the pages of the superview of my MPMovieViewController. How can I avoid this?
I ran into the same problem and I ended up removing the UIPageViewController gestures from the main view and then readded them when I was done. In my case, I'm showing a toolbar on the screen when someone single taps on the page view controller and then it fades out going back to the page view controller. To allow taps on the toolbar, I did the following:
// Remove the page controller gestures from the view
for (UIGestureRecognizer *gesture in self.gestureRecognizers) {
[self.view removeGestureRecognizer:gesture];
}
Where self is my extended UIPageViewController and I'm doing this in the method that shows something on the screen. It's going to be a little different in your case, but effectively this should work for you!

How to close a modal view when the application enter in background on ios

I have a modal view created in a method (there is no reference in the mainview) and I want to do a dismissModalViewControllerAnimated automatically when my app enter in background. How can I do that ?
In the mainview's viewDidLoad, add observer to be notified when app goes to background.
- (void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(goToBackground)
name:UIApplicationWillResignActiveNotification object:nil];
}
Define the function goToBackground(). It will be called when the app goes to background
- (void) goToBackground
{
[self dismissModalViewControllerAnimated: NO]; // no need to animate
}
Don't forget to remove the observer
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You can use a notification. Post a notification from the ApplicationDelegate's method applicationDidEnterBackground:. YOu can call the dismiss method from the modal controller, so add it as observer to the notification center.

Dismiss modal view controller on application exit

I have a view controller (view A) presenting a modal view (B) when the user pushed a button and the view B has itself a button to present view C. My problem is that if the user exits the application when the view B or C is shown, the same view will appear next time the application is launched. Is there a way to dismiss the views B and C on exit or to show view A when the application starts?
Thanks for your help
I assume by close you mean when the application enters the background.
In your app delegate you can via the applicationDidEnterBackground: method dismiss your controller.
Best way would probably be to add an observer in your view controller class:
- (void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appClosing) name:#"appClosing" object:nil];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appClosing" object:nil];
[super dealloc];
}
- (void) appClosing
{
[self dismissModalViewControllerAnimated:YES];
}
And post the notification in your app delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"appClosing" object:nil];
}