iOS
iOS

Reputation: 213

which class is used to implement the GKLocalPlayerListener protocols

i am trying to implement the GKChallengeListener in my code and i have used GameCenterManager class from apple. the player is authenticated fine and leaderboard and challenges are also. But i want to notify my application when the remote player completed the challenge. for this i have used GKChallengeListener protocols. but they are not calling up when i am sending or receiving the challenges reference from. but i dnt understood which class will call the delgate to self https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKEventListener_Ref/Reference/Reference.html#//apple_ref/occ/intf/GKChallengeListener

In ViewController.h

@interface ViewController : UIViewController <UIActionSheetDelegate, GameCenterManagerDelegate,GKGameCenterControllerDelegate,GKChallengeListener> {
    GameCenterManager *gameCenterManager;
    NSInteger  currentScore;
    NSString* currentLeaderBoard;
    IBOutlet UILabel *currentScoreLabel;
}
@property (nonatomic, retain) GameCenterManager *gameCenterManager;
@property (nonatomic, assign) NSInteger currentScore;
@property (nonatomic, retain) NSString* currentLeaderBoard;
@property (nonatomic, retain) UILabel *currentScoreLabel;
- (IBAction) showLeaderboard;
- (IBAction) increaseScore;

@end

In ViewController.m

@implementation ViewController

@synthesize gameCenterManager;
@synthesize currentScore;
@synthesize currentLeaderBoard;
@synthesize currentScoreLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentLeaderBoard = kLeaderboardID;
    self.currentScore = 0;
    if ([GameCenterManager isGameCenterAvailable]) {
        self.gameCenterManager = [[GameCenterManager alloc] init];
        [self.gameCenterManager setDelegate:self];
        [self.gameCenterManager authenticateLocalUser];
    } else {
        // The current device does not support Game Center.
    }
}

-(void)player:(GKPlayer *)player issuedChallengeWasCompleted:(GKChallenge *)challenge byFriend:(GKPlayer *)friendPlayer{
    NSLog(@"issued challenge was completed by friend");

}
-(void)player:(GKPlayer *)player didCompleteChallenge:(GKChallenge *)challenge issuedByFriend:(GKPlayer *)friendPlayer{
    NSLog(@"player did complete challenge");
}
-(void)player:(GKPlayer *)player didReceiveChallenge:(GKChallenge *)challenge{
    NSLog(@"player did recieve challenge");
}
-(void)player:(GKPlayer *)player wantsToPlayChallenge:(GKChallenge *)challenge{
    NSLog(@"player wants to play challenge ");
}

Upvotes: 1

Views: 712

Answers (2)

Steven W. Disbrow
Steven W. Disbrow

Reputation: 316

Struggled with this myself for a bit today.

The trick is to register the object that implements the GKLocalPlayerListener protocol as the listener for the localPlayer. Er... Well, that sentence seems to have looped back on itself but this might help.

/* this happens inside my authenticateLocalPlayer method */
if ([GKLocalPlayer localPlayer].authenticated) {
    [[GKLocalPlayer localPlayer] registerListener:self];
    // More stuff here
    }

And then later on in the same object you implement the protocol methods.

Edit: Oh! And you aren't supposed to implement GKChallengeListener. You should only implement GKLocalPlayerListener. (I was going to post a link to that, but danged if I can find it now.)

Hope that helps.

Upvotes: 2

Vivek Sehrawat
Vivek Sehrawat

Reputation: 6570

This is depreciated in iOS 7.0 but still you can use this as an alternative

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentLeaderBoard = kLeaderboardID;
    self.currentScore = 0;
    if ([GameCenterManager isGameCenterAvailable]) {

        self.gameCenterManager = [[GameCenterManager alloc] init];
        [self.gameCenterManager setDelegate:self];
        [self.gameCenterManager authenticateLocalUser];
    } else {
        // The current device does not support Game Center.
    }
GKChallengeEventHandler
    *gk
    =[GKChallengeEventHandler challengeEventHandler].delegate=self;
}

- (void)localPlayerDidCompleteChallenge:(GKChallenge *)challenge
{
    NSLog(@"localPlayerDidCompleteChallenge");
}
- (void)localPlayerDidReceiveChallenge:(GKChallenge *)challenge
{
    NSLog(@"localPlayerDidReceiveChallenge");
}
- (void)localPlayerDidSelectChallenge:(GKChallenge *)challenge{
    NSLog(@"localPlayerDidSelectChallenge");
}
- (void)remotePlayerDidCompleteChallenge:(GKChallenge *)challenge{
    NSLog(@"remotePlayerDidCompleteChallenge");
}
- (BOOL)shouldShowBannerForLocallyCompletedChallenge:(GKChallenge *)challenge
{
    return YES;
}
- (BOOL)shouldShowBannerForLocallyReceivedChallenge:(GKChallenge *)challenge
{
    return YES;
}
- (BOOL)shouldShowBannerForRemotelyCompletedChallenge:(GKChallenge *)challenge
{
    return YES;
}

Upvotes: 3

Related Questions