UIDeviceOrientationFaceDown in iphone - iphone

I want to detect UIDeviceOrientationFaceDown. How can i do this???
Actually i want to perform some action when my iphone face is down on flat surface.
how is it possible??? plz help me to do this.
i have this code, but don't know where and how apply this code
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(#"will receive orientation notifications: %#", getOrientationUpdates?#"YES":#"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
if there is any tutorial then please suggest me that
Thanks in Advance and most welcome to your precious help and suggestions.

You can accomplish this by detecting the ProximityState of iPhone. Using the [UIDevice currentDevice] singleton, setting the proximityMonitoringEnabled to YES. you can access the proximity information through the proximityState property.
[[UIDevice currentDevice]proximityState];
iPhone has a sensor turns off the screen when you put it on your ear during a call, AFAIK that is an infrared sensor. and you can access it.
EDIT:
You can also accomplish it using the below code. UIDeviceOrientationFaceDown
The device is held parallel to the ground with the screen facing downwards. (regardless if it touched any object) if you want to know if the iPhone touched an object, detect the proximity state of device.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(detectOrientation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)detectOrientation;{
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationPortrait:
{
NSLog(#"portrait");
}
break;
case UIDeviceOrientationPortraitUpsideDown:
{
NSLog(#"portraitUpSideDown");
}
break;
case UIDeviceOrientationLandscapeLeft:
{
NSLog(#"landscapeLeft");
}
break;
case UIDeviceOrientationLandscapeRight:
{
NSLog(#"landscapeRight");
}
break;
case UIDeviceOrientationFaceDown:
{
NSLog(#"facedown!!");
}
break;
default:
break;
}
}
}
EDIT: to answer the question in comment. add this line in your viewDidLoad
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil];
then write a method
-(void)handleProximityChangeNotification{
if([[UIDevice currentDevice]proximityState]){
NSLog(#"...");
}
}

You can use the z value from accelerometer sensor (-1 <= z <= 1). If your device is facing down, the z value will be in 0 < z <=1 (0 when it's facing parallel to the ground, 1 when it's facing perpendicular to the ground). To get this data you can use CMMotionManager
#import <CoreMotion/CoreMotion.h>
CMMotionManager *cm=[[CMMotionManager alloc] init];
cm.deviceMotionUpdateInterval=0.2f;
[cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *data, NSError *error) {
if(data.gravity.z>=0.3f)//
{
//DO something
}
}];
Don't forget to add CoreMotion framework to your project.

Related

iOS7 issue on Playing mov file in MPMoviePlayerViewController

I was able to play .mov file using following coding in ios 6.1.3 and below but in iOS7 it closed automatically.
MPMoviePlayerViewController *mp1 = [[ MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
if (mp1)
{
self.moviePlayer= mp1;
[mp1 release];
self.moviePlayer.view.userInteractionEnabled=YES;
self.moviePlayer.moviePlayer.repeatMode = MPMovieRepeatModeOne;
self.moviePlayer.view.frame = CGRectMake(0, 0, 320, 460);
[self.moviePlayer.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
self.moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleDefault;
//Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerDidExitFullscreenNotification object:self.moviePlayer.moviePlayer];
//setup device rotation notification observer
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[self.masterVC presentMoviePlayerViewControllerAnimated:moviePlayer];
[self.moviePlayer.moviePlayer prepareToPlay];
[self.moviePlayer.moviePlayer play];
[self.moviePlayer.moviePlayer setFullscreen:TRUE];
}
Using above coding I can able to play .mp4 file in iOS7 and all. Does apple restricted mov files?
I may be having the same issue as you. If you look at your logs, do you get the following error:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
The only other idea I have is to use a new class such as AVPlayer or even a third party class to play my media. I don't see any settings that are incorrect in your code nor my code. It is iOS 7 related though.

How to know what kind of device was plugged in?

I'm using the next code to know when a device is plugged in:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//code...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(accesoryChanged:) name:EAAccessoryDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(accesoryChanged:) name:EAAccessoryDidDisconnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(accesoryChanged:) name:UIScreenDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(accesoryChanged:) name:UIScreenDidDisconnectNotification object:nil];
EAAccessoryManager *accessoryManager = [EAAccessoryManager sharedAccessoryManager];
[accessoryManager registerForLocalNotifications];
}
- (void)accesoryChanged:(NSNotification*)note;
{
if(note.name == EAAccessoryDidConnectNotification)
{
EAAccessory* accessory = [note.userInfo objectForKey:EAAccessoryKey];
//code...
}
else if(note.name == EAAccessoryDidDisconnectNotification)
{
EAAccessory* accessory = [note.userInfo objectForKey:EAAccessoryKey];
//code...
}
}
And with:
accessory.name
I can get the device's name, but I couldn't find a way to know what kind of device is (i.e: a controller or a HDMI adapter).
Is there any way to get this information?
Thanks in advance.
The EAAccessory object has properties for the manufacturer, modelNumber, and serialNumber. You can also look at the array of protocolStrings to get an idea of the device's capabilities.
When deciding whether to connect to an accessory, you should use the accessory’s declared protocols to make your determination. The protocols associated with an accessory indicate the types of data the accessory is capable of processing. You may use other properties to help you decide whether or not to connect to an accessory but the list of protocols should be the key factor you consider.

iPhone/iPad: animated splash screen?

I want to create an animated logo that serves as the splash screen for my iphone/ipad app.
I'm thinking of showing the default.png, which then transitions to an .mp4 (where the first frame of the .mp4 matches the default.png), plays a 3 second movie, fades out, and loads my application.
Does anyone have any experience with this? And is my idea (using .mp4) the best way to achieve this? Also, is Apple "cool" with this?
Thanks in advance.
Yes you can absolutely do this and yes Apple is cool with it.
You could use MPMoviePlayerController, place it under a UIImageView with the launch image and when the movie is loaded and ready to go remove the UIImageView and play the movie.
But given the sometimes finicky nature of MPMoviePlayerController you need to time things carefully. Here's a snippet you can use as a starting point:
-(void)setupMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:)
name:MPMoviePlayerLoadStateDidChangeNotification object:self.playerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showMovie:)
name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.playerView];
[self.playerView setContentURL:[[NSBundle mainBundle] URLForResource:#"movie" withExtension:#"mov"]];
[self.playerView prepareToPlay];
}
-(void)playMovie:(NSNotification *)notification {
if (self.playerView.loadState == MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:notification.object];
[self.playerView play];
}
}
-(void)showMovie:(NSNotification *)notification {
if (self.playerView.playbackState == 1) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:notification.object];
// should update this to new UIView anim methods
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.splashScreen.alpha = 0;
[UIView commitAnimations];
}
}
I think that a tutorial like this one can be helpful: http://www.youtube.com/watch?v=wCsumlHiEc0&feature=channel_video_title
Respond to the UIApplicationDidFinishLaunchingNotification. I agree with #jhocking that you should consider whether such a wait is the best UX, but if it is, it's a pretty straightforward task.

iPad 3.2 & [MPMoviePlayerController setFullScreen:] not showing up

I have an universal application that plays movies from the internet. It has to support 3.1.x as well as 4.x.
In order to get this to work, I have a branch in the code that detects pre-3.2 devices and utilizes MPMoviePlayerController as it is supposed to work there.
This is how I prepare the player to play the remote movie:
- (void)registerForMovieNotifications {
//for 3.2 devices and above
if ([moviePlayer respondsToSelector:#selector(loadState)]) {
LOG(#"moviePlayer responds to loadState, this is a 3.2+ device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didExitFullScreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
LOG(#"preparing moviePlayer...");
[moviePlayer prepareToPlay];
} else {
//for pre-3.2 devices
LOG(#"This is a 3.1.x device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
//handle when the movie finished
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)readyPlayer {
if (!moviePlayer) {
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
} else {
[moviePlayer setContentURL:movieURL];
}
[self registerForMovieNotifications];
}
Later on I get this notification, and it sets up the movie player's view, etc.
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
LOG(#"3.2/4.x - moviePlayerLoadStateChanged:");
//unless state is unknown, start playback
if ([moviePlayer loadState] != MPMovieLoadStateUnknown) {
//remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
//set the frame of the movie player to match
self.view.autoresizesSubviews = YES;
[[moviePlayer view] setFrame:self.view.bounds];
[[moviePlayer view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[moviePlayer view] setAutoresizesSubviews:YES];
//add movie player as a subview
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES];
//play the movie
[moviePlayer play];
}
}
And the movie plays. This works perfectly on iPhone 4.2, 4.3, iPad 4.2, 4.3, but it fails on iPad 3.2. The movie plays but all I get is a black screen.
If I remove the [moviePlayer setFullscreen:YES] call I get a visible playing movie in 3.2, however it isn't "fullscreen" and so it doesn't have the Done button and there's no way for me to dismiss the screen.
I'd love some help on what's going on here. Thanks!
I was able to come to an acceptable solution, but I still feel this might be a bug.
If I skip the call to setFullScreen and instead just manually set the controlStyle to MPMovieControlStyleFullScreen then it gives me a view that is mostly correct (the toolbar is about 40 pixels too low).
Then I can get the Done button, which triggers the moviePlayer:didFinishPlaying callback.
So it stinks I now have a smelly if 3.2 branch of logic in my code, but hopefully most people will be on 4.0 anyway.

Rotating landscape mode

what i did is displaying 10 images as grid and 3 images in portroide mode, and what i did is when ever i rotate the simulator to landscape then i have to display 4 images.It is also displayed using the code
if(self.interfaceorientation == UIIntefaceorientationPortrait) {
[self abc];
else {
[self abclandscape];
}
here abc and abclandscape are two functions it works fine but, it works from initial means form starting if i rotate to landscape mode or portrait mode it works fine .while in the middle if i rotate from landscape to portrait it does 't goes to [self abc] function. how can i solve this?
What you could do is either use the UIViewController delegates, or use the NSNoticationCenter.
I.E. add in your "viewDidLoad":
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
And add the function:
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
// Do one thing
}
else
{
// Do something else
}
}
#mahesh In shouldAutoRototate Method
use
if(self.interfaceorientation == UIIntefaceorientationPortrait||self.interfaceorientation == UIIntefaceorientationPortraitUp..)
{
}
else
{
}