Send Action on rotation of device iOS - ios

here is what I'm trying to achieve, it would be to send an action on rotation of the device...
the point is i'm having a 360 player, I would like the video to start only once the device is on landscape. If it's in portrait i'd like to display a message a saying "rotate your device in landscape to start", and when the user rotate the device, the video play.
below is my code so far:
- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:#"HTY360PlayerVC" bundle:nil url:url];
[videoController VRMode:true];
if (![[self presentedViewController] isBeingDismissed]) {
[self presentViewController:videoController animated:YES completion:nil];
}
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
The problem of this code is than it trigger only on TouchUp Inside in the Sent Event . . . anyway to have the action starting on rotation ?
Thanks for all your help !
EDIT---
TRYIED THE FOLLOWING ASWELL:
- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:#"HTY360PlayerVC" bundle:nil url:url];
[videoController VRMode:true];
if (![[self presentedViewController] isBeingDismissed]) {
[self presentViewController:videoController animated:YES completion:nil];
}
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}

Have you tried
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

Try put your code inside:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
}
and write this methods in your UIViewController. This method is called when the device (interface really) is rotated; if you want do something before the rotation of interface, use willRotateFromInterfaceOrientation

Related

iBeacon to push another view! iOS App

What I am trying to do is make it so that when an iBeacon has been found (when the user is on the home screen), it sends them to another part of the app. Essentially it pushes another view of the app from the storyboard. For example, iBeacon1 pushes View1, iBeacon2 pushes View2, etc, and that's it. Like I said I know that it possible, but because iBeacons are quite new, there isn't much help available.
I would really appreciate it if you could take some time out of your day to help me with this issue that I cannot find the answer to.
Thank you for your time,
Josh
If you are using Storyboards, you could do something like this in your AppDelegate:
var launchedViewControllers = [Int]()
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let navController = self.window?.rootViewController?.navigationController {
for beacon in beacons {
if (beacon.minor == 1) {
if !launchedViewControllers.contains(beacon.minor.integerValue) {
launchedViewControllers.append(beacon.minor.integerValue)
let viewController1 = mainStoryboard.instantiateViewControllerWithIdentifier("viewController1")
navController.pushViewController(viewController1, animated: true)
}
}
if (beacon.minor == 2) {
if !launchedViewControllers.contains(beacon.minor.integerValue) {
launchedViewControllers.append(beacon.minor.integerValue)
let viewController2 = mainStoryboard.instantiateViewControllerWithIdentifier("viewController1")
navController.pushViewController(viewController2, animated: true)
}
}
}
}
}
You could also rewrite this to use segues instead of programmatically pushing ViewControllers using a NavigationController but both will work.
pragma mark -
pragma mark -Important Methods
-(void)showSecondController{
if ([_beacon.major isEqual: #3404] && [_beacon.minor isEqual: #10692]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SDOVideoDetailController"];
} else if ([_beacon.major isEqual: #20165] && [_beacon.minor isEqual: #53849]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LRCVideoDetailController"];
} else if ([_beacon.major isEqual: #53088] && [_beacon.minor isEqual: #30487]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CECSVideoDetailController"];
} else if ([_beacon.major isEqual: #55304] && [_beacon.minor isEqual: #59835]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IVideoDetailController"];
} else if ([_beacon.major isEqual: #16004] && [_beacon.minor isEqual: #46054]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CILVideoDetailController"];
} else if ([_beacon.major isEqual: #20214] && [_beacon.minor isEqual: #11696]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ITVideoDetailController"];
} else if ([_beacon.major isEqual: #21559] && [_beacon.minor isEqual: #17557]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PARKVideoDetailController"];
} else if ([_beacon.major isEqual: #59452] && [_beacon.minor isEqual: #21013]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SCVideoDetailController"];
} else if ([_beacon.major isEqual: #42093] && [_beacon.minor isEqual: #49773]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"BAVideoDetailController"];
}

Load another .xib when orientation in iOS

I used below code to implement that but it not work. Please give me some advices. Thanks much.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
orientationChanged() function:
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
adjustViewsForOrientation() function:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"load portrait view");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardViewController" owner:self options:nil];
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(#"load landscapeview");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardLandscapeView" owner:self options:nil];
}
}
When run it always show DashBoardViewController.xib. In lanscape, it still shows DashBoardViewController.xib. I don't know why.
This is because your not actually setting the view.
You need to do something like:
self.view = [[NSBundle mainBundle] loadNibNamed:#"DashBoardLandscapeView" owner:self options:nil] objectAtIndex:0];
This will load the view from the Xib file, and set the current view to that view.
Don't forget to add the objectAtIndex:0 as you see in the code.
Use willRotateToInterfaceOrientation: method instead of notifications
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
{
NSLog(#"load landscapeview");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardLandscapeView" owner:self options:nil];
//[self viewDidLoad];
}
else
{
NSLog(#"load portrait view");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardViewController" owner:self options:nil];
//[self viewDidLoad];
}
}
Maybe you have to uncoment [self viewDidLoad] if you have som logic there.

UIIMagePicker camera landscape orientation issue

I have a view controller and i have added UIImagePicker as subview. On changing device orientation from portrait to landscape, camera appears to be on half screen rather than full screen. the images in the camera is also rotated 90 degrees. I am supporting device orientation in my app. how to fix this camera orientation issue. Any help is appreciated.
//Code for presenting camera
- (id)init
{
self = [super init];
if (self) {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.allowsEditing = YES;
self.cameraPicker = picker;
cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );;
//cameraPicker.view.frame = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height );
[self.view addSubview:cameraPicker.view];
}
return self;
}
//Code for supporting device orientation
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self setFramesForControls:toInterfaceOrientation];
}
-(void)setFramesForControls :(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );
}
else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );
}
}
Try this its working fine
declare this in viewdidload method
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.allowsEditing = YES;
and on button click event do the following
[self presentModalViewController:picker animated:YES];
no matter to check device orientation;
To prevent your image this code will help you-
Define these macros on the top of your ViewController class-
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
Then manage your delegate method in this way-
#pragma mark- Delegate methods of UIImagePickerController Class
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString *imgType = [info objectForKey:UIImagePickerControllerReferenceURL];
//NSLog(#"%#",imgType);
imgType = [imgType lastPathComponent];
NSData *imageData;
if (SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
// code here
imageData = UIImagePNGRepresentation(pickedImage);
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
{
// code here
imageData = UIImageJPEGRepresentation(pickedImage, 0.5);
}
NSError * error = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&error];
if (error != nil)
{
NSLog(#"ERROR: %#", error);
return;
}
[picker dismissViewControllerAnimated:YES completion: nil];
}
It will capture the images according to your requirement.

Load different .xib for different orientation

In my application, i want to load different .xib for different orientation i.e. one for portrait , one for landscape
I searched it and found solutions too, but it isn't working for me. Anybody please help me out with this.
Here is my code:-
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}
- (void) orientationChanged:(NSNotification *)note {
UIDevice * device = note.object;
NSArray *array;
UIView *mainView;
if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {
array = [[NSBundle mainBundle] loadNibNamed:#"NewViewController" owner:self options:nil];
mainView = [array objectAtIndex:0];
NSLog(#"Portrait");
} else {
array = [[NSBundle mainBundle] loadNibNamed:#"Landscape" owner:self options:nil];
mainView = [array objectAtIndex:0];
NSLog(#"Landscape");
}
}
Thanks in advance!

Game Center authentication always results in sigabrt

This is driving me crazy. Every time I try to authenticate the local player with Game Center I get a thread-1 sigkill. It happens asynchronously after I set the localPlayer's authenticateHandler like so:
- (void)authenticateLocalPlayer
{
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
{
GKLocalPlayer __weak *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
{
if (viewController != nil)
{
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:viewController animated:YES completion:nil];
}
else if (localPlayer.isAuthenticated)
{
NSLog(#"Player authenticated");
}
else
{
NSLog(#"Player authentication failed");
}
};
}
}
Any ideas?
I've often seen the systemVersion tested with a string comparison instead of a number comparison. Try just printing the value of
[[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0
to verify there's nothing wrong with that expression. I know the following method works:
-(BOOL) os6 {
NSString *targetSystemVersion = #"6.0";
NSString *currentSystemVersion = [[UIDevice currentDevice] systemVersion];
if ([currentSystemVersion compare:targetSystemVersion options:NSNumericSearch] == NSOrderedAscending) {
return NO; //current system version is less than 6.0
} else {
return YES;
}
}
[GKLocalPlayer localPlayer] returns a singleton and __weak releases it at some time (I don't understand when that happens, but at some future time). localPlayer might be getting released, along with the block, before the block finishes executing. There's no need to release a singleton. Try removing __weak.
Try putting __weak before GKLocalPlayer.
It's the only difference I can see between your code and the code I use...

Resources