iOS Question Game Center Achievements

natesobol

Member
Licensed User
Hi all,

I have my leaderboard working through the gamecenter library and I am wondering if there's a way I can upload my app's data to game center achievements as well in B4i?

I've attached my snippet of code for submitting just my leaderboard score, and wondering if there's something similar for achievements, along with a screenshot of my achievement setup in app connect and what it looks like on my app.

Thanks for any help!

Submit Leaderboard Score:
Dim score As GKScore
score.Initialize("coinsLeaderboardID")
score.Value = kvscache.Get("1")
Wait For (Game.SubmitScore(score)) GameCenter_ScoreSubmitted (Success As Boolean)

gamecenter.jpgIMG-6740.PNG
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not fully tested, but lets start with this:
B4X:
Private Sub SubmitAchievement(Identifier As String, PercentComplete As Double)
    Dim ach As NativeObject
    ach = ach.Initialize("GKAchievement").RunMethod("alloc", Null).RunMethod("initWithIdentifier:", Array(Identifier))
    ach.SetField("percentComplete", PercentComplete)
    Wait For (Game.As(NativeObject).RunMethod("SubmitAchievement:", Array(ach))) GameCenter_AchievementSubmitted (Success As Boolean)
    Log(Success)
    Log(LastException)
End Sub

#if OBJC
@end
@interface B4IGameCenter (achievement)
@end
@implementation B4IGameCenter (achievement)
- (NSObject *)SubmitAchievement:(GKAchievement *)ach {
    [GKAchievement reportAchievements:@[ach] withCompletionHandler:^(NSError *e) {
        if (e != nil)
            [B4I shared].lastError = e;
        [B4IObjectWrapper raiseEventFromDifferentThreadWithExplicitSender:self :ach :@"_achievementsubmitted:" :@[@(e == nil)]];
    }];
    return ach;
}
#End If

PercentComplete should be between 0 to 100.
 
Upvote 0
Top