Cocos2d, iOS 6.1, GameCenter Crash When Showing Leader-board - iphone

I am trying to show a Leader-board in my Cocos2d Game. The code below works correctly on iOS 5.0. However on 6.1 it crashes with the error message below. Thanks in advance for any suggestions.
* Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View > is associated with . Clear this association before associating this view with .'
- (void)showLeaderboard:(id)sender
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
leaderboardViewController = [[UIViewController alloc] init];
leaderboardController.category = self.currentLeaderBoard;
leaderboardController.leaderboardDelegate = self;
[leaderboardViewController setView:[[CCDirector sharedDirector] openGLView]];
[leaderboardViewController presentViewController:leaderboardController animated:YES completion:nil];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[leaderboardViewController dismissViewControllerAnimated:YES completion:nil];
[viewController release];
}

Try to stop openGL animation and resume back.
-(void)showLeaderboard
{
[[CCDirector sharedDirector] stopAnimation];
GKLeaderboardViewController *leaderboardViewController = [[[GKLeaderboardViewController alloc] init] autorelease];
leaderboardViewController.leaderboardDelegate = self;
AppController *app=(AppController*)[UIApplication sharedApplication].delegate;
[app.navController presentModalViewController:leaderboardViewController animated:YES];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
AppController *app=(AppController*)[UIApplication sharedApplication].delegate;
[app.navController dismissModalViewControllerAnimated:YES];
[[CCDirector sharedDirector] startAnimation];
}

Related

IOS 7 + For force controller Orientation to Portrait

I have used below code for IO6 and below to force rotate:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIApplication* application = [UIApplication sharedApplication];
if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
{
UIViewController *c = [[UIViewController alloc]init];
[c.view setBackgroundColor:[UIColor redColor]];
[self.navigationController presentViewController:c animated:NO completion:^{
[self.navigationController dismissViewControllerAnimated:NO completion:^{
}];
}];
}
}
But it is not working correctly on IOS7, it rotate view controller but again set blank view on screen...
Can you help me to solve this issue in IOS 7...
Change this line:
[self.navigationController dismissViewControllerAnimated:NO completion:^{
}];
To this:
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];

Viewcontroller not appearing since iOS7

Edit - I solved this myself - see the notes at the bottom
When using iOS7 on Xcode 5, I am using an option to take an image from a camera, or from the photo library, once the image is chosen (or a new picture taken) the view should flip over to the next screen.
This does not happen on the iPhone running iOS7, it works fine on the iPad, but the method is slightly different, but it does appear to be iPhone only problem on iOS7.
here is the code used, for example, on the choose image from library function;
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
[self presentModalViewController: cameraUI animated: YES];
}
}
Also, the code once picker is finished;
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
//Disable buttons
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self disableButtons];
//Get image
self.originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
//Dismiss
if(_popover)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[_popover dismissPopoverAnimated:YES];
_popover = nil;
}
}
else
[picker dismissModalViewControllerAnimated: YES];
//Next
[self performSelector: #selector(nextScreen) withObject:nil afterDelay:0.5];
}
I fixed this by switching out;
[picker dismissModalViewControllerAnimated: YES];
With
[picker dismissViewControllerAnimated:NO completion:nil];
First you need to make sure that logic reaches the correct place it's intended to, try setting a breakpoint or NSLog before the iPhone's specific line, try this (You also missed curled braces, added them here) :
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
NSLog(#"Checkpoint !");
[self presentModalViewController: cameraUI animated: YES];
}
} }

How do I get a photo from the phone gallery?

I have a huge problem as am trying to open image picker but don't know why my app crashes at this line
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
I have used this this code in many application and code is working fine but in my current apps this code giving exception of preferred interface orientation.
The code which is used :
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (cameraAvailable == TRUE) {
if (buttonIndex == 0) {
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypeCamera;
//[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
}
else if(buttonIndex == 1){
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
}
}
if (buttonIndex == 0) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//imgpkrController.modalPresentationStyle=UIModalPresentationFullScreen;
//[self presentModalViewController:imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
//[self presentViewController:imgpkrController animated:YES completion:nil];
}
else{
NSLog(#"ipad photo");
imgpkrController.delegate = self;
popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
[popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
else{
if (buttonIndex == 0) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
}else{
NSLog(#"ipad photo");
imgpkrController.delegate = self;
popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
[popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
}
}
Code crashes on this line
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
with exception:
2013-07-16 15:11:02.143 HMW[1335:c07] *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
*** First throw call stack:
(0x2ec5012 0x2a7ce7e 0x2ec4deb 0x17a54da 0x19a444c 0x17a24f3 0x17a2777 0x2a663 0x1a952c1 0x2a90705 0x16bb920 0x16bb8b8 0x177c671 0x177cbcf 0x177bd38 0x16eb33f 0x16eb552 0x16c93aa 0x16bacf8 0x38a7df9 0x38a7ad0 0x2e3abf5 0x2e3a962 0x2e6bbb6 0x2e6af44 0x2e6ae1b 0x38a67e3 0x38a6668 0x16b865c 0x2802 0x2735)
libc++abi.dylib: terminate called throwing an exception
Not sure what's causing your error message as it seems to be related to the device orientation but I managed to get this working with the following code:
- (IBAction)attachPhotosClicked:(id)sender
{
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[_popoverController presentPopoverFromRect:self.attachButton.frame inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionLeft)
animated:YES];
return;
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose a source"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Camera", #"Photo Library", nil];
[actionSheet showFromRect:self.attachButton.frame inView:self.view animated:YES];
}
}
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
You will find this issue in iOS 6.0. have a look at the following link,
UIPopoverController orientation crash in iOS 6
It should be fixed in later versions.
I think your problem is due to an iOs version problem. The interfaceOrientation protocol is changed in iOs 6.
I think this link could help you.
preferredInterfaceOrientationForPresentation must return a supported interface orientation?

Exception in iPad, UIImagePickerController must be presented via UIPopoverController

I have created an application for capture image from camera. This is my code
-(IBAction) showCameraUI {
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
And implemented this delegate method for get the captured image
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *yourImageView = image;
}
Implemented this method if user cancel the controller
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
But it shows this exception. Does anyone have any idea why it is showing such exception after executing last line of function showCameraUI.
UIStatusBarStyleBlackTranslucent is not available on this device. 2013-02-07
10:06:06.976 CaptureImage[460:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be
presented via UIPopoverController'
Regarding the exception, the error message is very clear. "On iPad, UIImagePickerController must be presented via UIPopoverController" For iPad, you should present it in a UIPopoverController instead of using [self presentModalViewController:picker animated:YES];. This should fix the issue.
For eg:-
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popover = popover;
} else {
[self presentModalViewController:picker animated:YES];
}
Edit: As mentioned by #rmaddy, camera can be presented modally. The above is applicable when sourceType is UIImagePickerControllerSourceTypePhotoLibrary.
#Arun
i am also face the same problem add global property in header file.
I hope the below code is useful for you
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
[imgPicker setDelegate:self];
[imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imgPicker setAllowsEditing:YES];
[imgPicker setModalPresentationStyle:UIModalPresentationCurrentContext];
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
popOver.delegate = self;
self.popoverImageViewController = popOver;
[self.popoverImageViewController presentPopoverFromRect:CGRectMake(0, 0, 160, 40) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
In that header file create global property like this
#property (strong) UIPopoverController *popoverImageViewController;
For Swift with iOS11:
videoEditor.modalPresentationStyle = .popover
self.present(picker, animated: true)
let popover = picker.popoverPresentationController
popover?.sourceView = self.view
// then config popover frame and arrow style here

How to fix the UIModalTransitionstyle issue on ios6?

I am using the following code for UIModalTransitionStyle view in my application when i click the button
InfoViewController *infoViewController = [[InfoViewController alloc]initWithNibName:#"InfoViewController" bundle:nil];
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.presentedViewController presentViewController:infoViewController animated:YES completion:NULL];
its worked perfecly in ios5 but i update the application ios6 it not worked. How can I handle this issue
This code works, tested in iOS 6.0 simulator.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"http://videoURL"]];
self.moviePlayerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:self.moviePlayerController animated:YES completion:nil];
}
Since the URL is invalid, the MPMoviePlayerViewController will just endlessly flip back and forth, but the point is that it works.