Game Center Calling Leaderboard function not responding? - objective-c

I am calling the leader board like this:
-(void)viewscores:(SPEvent*)event
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview: self.rootViewController];
[self presentModalViewController: leaderboardController animated: YES];
}
}
When I Click the leader board button, I receive an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIViewController presentModalViewController:animated:]: unrecognized selector sent to class 0x3e2fc7f8'
Is this normal?

You should probably call the function in a viewController. In one of my app's the code looks like this:
-(IBAction)showLeaderBoard {
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
if (leaderBoardCont) {
leaderBoardCont.category=#"1S";
leaderBoardCont.timeScope=GKLeaderboardTimeScopeWeek;
leaderBoardCont.leaderboardDelegate=self;
[self presentModalViewController:leaderBoardCont animated:YES];
}
}
and then you should also implement the delegate method:
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
viewController = nil;
}
Here 1S is the identifier for the leader board you created in iTunes Connect. Hope this helps.
Edit: since you are still having problems, check these tutorials out. They cover everything about leader boards and achievements.
1st part
2nd part

You're sending the presentModalViewController message to an object that doesn't recognize it. So the class you wrote in your "Game.m" file doesn't inherit from UIViewController. Dunno what framework you're using, but you'll have to init a UIViewController instance to show the GameCenter view.

Related

IBOutlet always nil

I've already looked at all the other entries for this question. I have been stuck for a while on a problem I have been unable to solve, any suggestion for an avenue of approach would be sincerely appreciated.
I have a moderately large app that allocs a window controller then loads a window using an XIB containing a single window which contains an AVPlayerView that was placed in the window. The AVPlayerView is wired to an IBOutlet called playerView. The window loads when it is supposed to and then an AVPlayer is created with a URL and is to be assigned to the player of the AVPlayerView, however I always find that the IBOutlet playerView is always nil at the point of trying to make the assignment. I cannot figure out why the IBOutlet is nil. Another anomaly is that the AVPlayerWindow will not become key when so ordered even though there are no other windows on the screen. I know for a fact that I have the outlet wired up in the XIB. Any thoughts?
There are 11,000 lines of code in the app. Here is the tiny portion that attempts to attach the player to the AVPlayerView.
fileURL = [self prepareStringAndBecomeAURL:filePath isDirectory:NO needFileURL:NO];
// thePlayer = [self prepareToPlay:fileURL];
thePlayer = [AVPlayer playerWithURL:fileURL];
if (thePlayer == nil) {
[self popUpErrorMessageWithoutCode:#"Unable to create a video player."];
} else {
playerView.player = thePlayer;
if (thePlayer != nil) {
[theMainWindow orderOut:self];
[self showMoviesWindow:self];
[self performSelector:#selector(continueHandleMovieSelected) withObject:nil afterDelay:1]; // wait for window to appear
}
}
}
- (void)continueHandleMovieSelected
{
[playerView.player play];
}
No, no exceptions.
Window controller and nib are created in another module, an AppController:
- (IBAction)showMoviePlayerWindow:(id)sender
{
if (![self myMoviePlayerWindowController]) {
myMoviePlayerWindowController = [[MoviePlayerWindowController alloc] initWithWindowNibName:#"MoviePlayer"];
[self set_myMoviePlayerWindowController:myMoviePlayerWindowController];
}
[[[self myMoviePlayerWindowController] window] center];
[[[self myMoviePlayerWindowController] window] makeKeyAndOrderFront:self];
[[self myMoviePlayerWindowController] showWindow:self];
}

Displaying a subView in didReceiveRemoteNotification?

What Im trying to do is displaying my subView in didReceiveRemoteNotification when the app receives a notification and is running. How can I do this?
My code right now in didReceiveRemoteNotification:
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
AcceptAlertViewCreator *acceptAlertViewCreator = [[AcceptAlertViewCreator alloc] init];
//Here I try to get the current viewController running...
UIViewController *viewController = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
//This line gives me the "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView view]: unrecognized selector sent to instance 0x17816b7c0" error
[viewController.view addSubview:[acceptAlertViewCreator createAlertViewWithViewController:viewController andText:[[userInfo objectForKey:#"aps"] objectForKey:#"message"]]];
}
else
{
//other things
}
My AcceptAlertViewCreator returns a UIView and gets a viewController for the viewController to be displayed in and NSString for message. My AcceptAlertViewCreator also has a UIViewAnimation when it is played.
My AcceptAlertViewCreator works great when added to a normal ViewController subView.
Anyone knows how I can accomplish this? Id doesn't have to be adding a subView. It could be a workaround in some ways, or please give me some pointers. Thanks
Instead of getting the viewController from sharedApplication just get the rootViewController of you window
You didReceiveRemoteNotification method should look like this:
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
AcceptAlertViewCreator *acceptAlertViewCreator = [[AcceptAlertViewCreator alloc] init];
[self.window.rootViewController.view addSubview:[acceptAlertViewCreator createAlertViewWithViewController:self.window.rootViewController andText:[NSString stringWithFormat:#"%#", [[userInfo objectForKey:#"aps"] objectForKey:#"message"]]]];
}
else
{
//other things
}
So you are having your AppDelegate display a window when an alert comes in.
I would not add it as a subview, I would just present the view controller instead. I assume you want some action to be taken when this notice comes in.
You could also use NSNotificationCenter to send a notification to your app when the push notification comes in. You trigger the NSNotification from didReceiveRemoteNotification and your app views would need to listen for this notifications. The downside is each view would need to listen for this notification.

UIActivity custom activityViewController crash on iPad

I have a custom UIActivity that I use in order to create a Contact to the device's AddressBook. In this UIActivity, I create an ABNewPersonViewController, put it inside a UINavigationController and return it in UIActivity's
- (UIViewController *)activityViewController
The problem is that on the iPad I get a crash due to a reference to a released UINavigationController. Messages are of type:
*** -[UINavigationController _viewControllerForSupportedInterfaceOrientations]: message sent to deallocated instance 0xa6f1660
Also after my controller is dismissed, the iPad does not autorotate the view when the interface changes orientation.
The code I use is the following:
In UIActivity
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
// Prepare the AB View Controller
ABRecordRef aContact = ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(aContact, kABPersonKindProperty, kABPersonKindOrganization, &error);
ABRecordSetValue(aContact, kABPersonOrganizationProperty, #"Apple Inc.", &error);
ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phone, #"+1 2345 784513", kABWorkLabel, NULL);
ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &error);
CFRelease(phone);
self.newContactVC.title = #"New company";
self.newContactVC.displayedPerson = aContact;
[self.navigation setViewControllers:[NSArray arrayWithObject:self.newContactVC]];
CFRelease(aContact);
}
- (UIViewController *)activityViewController
{
return self.navigation;
}
// Dismisses the new-person view controller.
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self activityDidFinish:YES];
}
In the ViewController that calls this UIActivity
- (IBAction)showActionsSheet:(id)sender {
AddToAddressBookActivity *abActivity = [[AddToAddressBookActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:nil
applicationActivities:[NSArray arrayWithObject:abActivity]];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
if (!self.popover || ![self.popover isPopoverVisible]) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:activityVC];
[self.popover setDelegate:self];
self.popover.passthroughViews = nil;
[self.popover presentPopoverFromRect:self.showASBtn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
Any help would be greatly appreciated.
Link to a demo project:
http://ge.tt/23MeOYq/v/0?c
I had a similar problem, except the crash occurred on rotation after the activity view controller was dismissed. In my case, I had the custom view controller in a storyboard setup to present as "fullscreen", and on changing it to display as "form sheet" the crash went away. I don't know why that made a difference though.
Regarding your autorotation not working after the custom view controller dismisses, try adding a completion handler to your activity view controller to nil your popover view controller ivar. I noticed that if I didn't do this the custom activity and the custom view controller don't get dealloc'd, and autorotation doesn't happen. e.g.:
avc.completionHandler = ^(NSString* activityType, BOOL completed){
if ( UIUserInterfaceIdiomPad == [UIDevice currentDevice].userInterfaceIdiom )
self.popover = nil;
};
Also, check out the post at: "activityviewcontroller not dismissing", which I found helpful.

Is completion block syntax for MFMailComposeViewController mailComposeDelegate possible?

I would like to use the MFMailComposeViewController mailComposeDelegateproperty with completion block syntax, but not sure if that is possible. Something similar to how the TWTweetComposeViewController completionHandler property works:
TWTweetComposeViewController __weak *twee = tweeter;
tweeter.completionHandler = ^(TWTweetComposeViewControllerResult result) {
// code here
};
The code I have creates the MFMailComposeViewController, but then has to save a reference my viewController parameter, since this "share via email" functionality is not in a UIViewcontroller, but a custom MYSharing class.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
// more code here
_viewController = viewController;
picker.mailComposeDelegate = self;
[viewController presentModalViewController:picker animated:YES];
The mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate uses that _viewController to dismiss the modal.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[_viewController dismissModalViewControllerAnimated:YES];
}
Not understanding blocks very well, is there some syntax that look like this?
picker.mailComposeDelegate = ^(???)(??? ??? ???) {
[viewController dismissModalViewControllerAnimated:YES];
}
I did find MFMailComposeViewController(BlocksKit) (which has source on github), but even with that, the syntax conversion trips me up.
No completion handler for MFMailComposeViewController now.
if you want to reference the view controller who presented the MFMailComposeViewController, code like below, use presentingViewController property.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller.presentingViewController dismissModalViewControllerAnimated:YES];
}
Why don't you just call [self dismissModalViewControllerAnimated:YES]; from the MFMailComposeViewControllerDelegate method?

My modal dialog crashes (Cocoa)

My code below crashes if I have the code in windowWillClose: that releases
my MyWindowController, otherwise it works fine.
I test it on Mac OS 10.6.8.
I am using XCode 3.1.3.
What have I done wrong?
It seems like the window is not disposed of before I release MyWindowController,
because it crashes in a NSTableView method.
My button handler calls [NSApp stopModalWithCode:0];
MyDialog()
{
MyWindowController* controller = [[MyWindowController alloc] init];
[controller showWindow:controller];
NSWindow* window = [controller window];
[NSApp runModalForWindow:window];
[window close];
}
In my MyWindowController:
- (void)windowWillClose:(NSNotification*)notification
{
[self autorelease];
}
You are releasing 'self' in windowWillClose - that seems wrong.
Surely anything like that should be done in dealloc?
-(void)dealloc
{
[super dealloc];
}
Also, you might be better autoreleasing controller when it is initially alloc'd?