Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[DEPRECATED] Various Codea Objective-C Libraries

edited January 2015 in Code Sharing Posts: 2,820

EDIT: Since the new Codea runtime in v2.1, these libraries no longer work. If you do make a fork of my library, please leave a link below and I'll add it up here.

I've been putting it off for a while: Here's all the Objective-C libraries I've made for StackIt.


- TestFlight
- GCAddOn (This is better garbage collection for Lua)
- GameCenterAddOn
- LocationAddOn
- SharingAddOn
- AppDelegate
Please get the code from the actual source code of my game, StackIt, here.


1. Place the AppDelegate files in ProjectRoot / [your project name]
2. In, put your project name in and your TestFlight key in
3. Place all other files in ProjectRoot / [your project name] / Addons
4. Tada!

Deleting files

1. Go to AppDelegate.h and delete the line that says #import "filename.h"
2. Delete the line that is @property (strong, nonatomic) fileclass *vartitle;
3. Go to and delete the lines that say this:

self.vartitle = [[classname alloc] init];
[self.viewController registerAddon:self.vartitle];

And then you're golden.

Other notes

- This is a live thread. I'm not done with these and I'll be posting updates.
- GameCenter and sharing only work on iOS 6, not 5. I'll be fixing that later.
- If you use this, all I ask is that you give me credit.
- I created my own GameCenter because it doesn't use deprecated APIs, you can publish to custom leaderboard names, and you can show GameCenter banners
- Make sure you enable GameCenter capabilities
- My sharing file can also save to the camera role
- Right now my sharing API only lets you share photos, but that'll change
- Any other questions/requests/changes? Want to know how to do something I did in StackIt? Let me know.




  • Posts: 2,051

    Thanks for sharing! These will definitely come in handy once i migrate my project to Xcode / Codea Runtime

  • Posts: 2,820

    @JakAttak - No problem.

  • Jmv38Jmv38 Mod
    Posts: 3,297

    @Zoyt same for me thanks! (although i think i wont start publishing app befor 6-9 months.. But then it will quite help me!

  • edited December 2013 Posts: 2,820

    I just finished the ability to compose HTML email in-app (with a popup), ability to view other apps inside your app, and ability to go to the review link.
    Now I'm working on alerts with multiple options and callbacks.... Probably one of the more suggested features in Codea. Trust me, it's not easy... Not to mention it's 2:30 AM here. :|

  • Posts: 2,820

    It's now 6:15 AM... I'm almost done, one last bug. I had to learn all sorts of crap about the Lua C stack (it's horrible.) So I won't be posting the libraries tonight. Probably in a while.

  • Posts: 2,820

    I finished the library. Now I just need to make it compatable with the current "alert" API and handle errors.

  • Posts: 2,820

    I'm looking into making a UI elements library. We'll see how that goes... If I do it.

  • Posts: 2,051

    Sounds interesting

  • edited July 2014 Posts: 277

    Hey there,

    Alright, I'm at the point that i transferred all my stuff to Xcode and am trying to add these great plugins to the mix. I have a few basic questions though :

    • I only need Game Center and SharingAddOn, should I comment the lines referring to other add ons in the appdelegate files?
    • How do I call Game Center, or sharing for that matter, from the codea code? is there some templates I might have missed?

    Thanks a lot, I'm excited to see how this works!

  • Posts: 2,051

    @Rodolphe, yes comment any code for the addons you dont include from the appdelegate files.

    To call Game Center look in the add-on .m files where the functions are registered as lua functions - those are the names of the functions you can call directly from Codea code.

  • Posts: 2,820

    Thanks @JakAttak. Also, @Rodolphe, you can look at my latest repository, my game StackIt, which uses all these libraries. You can see what I do there. Just search the all the Lua code for "gamecenter". That will who you everywhere I use the library.

  • Posts: 277

    Thanks @JakAttak and @Zoyt. I can't see StackIt in your repository, did you release the code already?

  • Posts: 2,820

    @Rodolphe - It's not in that repository (and that's not even a repository anyways), it's in another repository here:
    Hope that helps!

  • Posts: 277

    Thanks a lot Zoyt, I'll try and get game center to work in my game (credit is good, if I ever manage to make it work?)... Cheers!

  • Posts: 277

    Ok, I tried adding Game Center capabilities tonight. I:

    1) turned on GC capability in xcode,
    2) I added the m and h Game Center files in Addons directory
    3) I added the new AppDelegate files
    4) I inserted "authenticatePlayer()" at the beginning of setup(), "showGameCenter()" in the menu, when players press the GC button, and "reportScore(hscore,"Highest elevation")" when a game is over and the best score has been beaten

    I'm getting this message, do you think I missed something? GC.PNG


  • Posts: 127

    Hi @Rodolphe, this is ot, but there's a little typo. Developed, not developped ;)

  • Posts: 277

    Oh thanks for the typo!

    Sorry, what do you mean "this is ot"?

  • Posts: 127

    I meant that my comment was off topic
    with regards to the question you've originally made, sorry for not saying that in the first place @rodolphe

  • Posts: 277

    Oh I'm sorry, English is not my mother tongue, obviously, and I tend to stumble on this kind of things... :) thanks again for the heads up on the typo!

  • Posts: 2,820

    @Rodolphe - Lucky for you, easy fix. This simply means that your provisioning profile doesn't have GameCenter enable. So, to fix it, simply go to Xcode, then click on your project in the sidebar, make sure your app is selected as the target (which it probably is by default), then click "Capabilities" in the tab bar above the content. Finally, flip the switch on GameCenter. If that didn't make sense/doesn't work, LMK. I have another solution if this doesn't work.

  • edited July 2014 Posts: 277

    Thanks! actually, it's activated, what would be your other solution @Zoyt?

  • edited July 2014 Posts: 178

    @Rodolphe. I think I might be able to help. Are you calling the showGameCenter together with authencatePlayer? Because that might cause that issue.

    @Zoyt. Your add ons are really helpful, but I think I'm doing something wrong. The Game Center log in will show up in the simulator, but I can't log in and see the test leaderboards I have made.

    EDIT: Never mind. I just never updated the App ID. :)

  • edited July 2014 Posts: 277

    Thanks @YoloSwag, should I use only either showGameCenter or authencatePlayer ?

    But maybe I didn't update the app id either, where should I do this?

  • edited July 2014 Posts: 178

    @Rodolphe. I use authencatePlayer at the beginning, and thats it. When you want to show the leaderboards I then would assume you use showGameCenter, but i haven't tried this.

    And you can update your App ID under the certificates page in the member section on the Apple dev website. However, i am still having problems with the logging in part.

    EDIT (This contains the answer to your question): This was actually fixed by making a sandbox account. I reset my iPad, and it prompted me to make a new account.

    To get rid of that error, try putting authencatePlayer in setup. This will allow the banner to pop up if they're already signed in, or prompt them to log in if they're not already. Hope this helps! I'm learning as I go along.

    If you have a button that pulls up game center, add this function somewhere within the parameters of that code: showGameCenter(). That will pull up any leaderboards you have.

    Sorry for the long explanation.

  • edited July 2014 Posts: 277

    Still no luck... I'm wondering now, something really obvious I might have overlooked : should I create an app in itunes connect, then a leader board, in order to to be able to connect to it from my app while still in development?

  • Posts: 2,820

    @Rodolphe - Yes, that was one of the other things I was going to suggest. Try that. I think that's your issue.

  • Posts: 178

    @Zoyt. Does showGameCenter automatically call the leaderboard option set up in iTunes Connect, or does the leaderboard ID need to be added?

  • Posts: 2,051

    @Rodolphe, yes you do need to have created the app in itunes connect

  • edited July 2014 Posts: 2,820

    @YoloSwag - Not quite sure what you're asking, but you do have to call authenticatePlayer in the main function. Then, when the user wants to see the leaderboards, you call the showGameCenter function.
    Edit: Sorry, I misunderstood your question. You can't (at this time) pass anything into the function, so it just goes to the default leaderboard.

  • Posts: 178

    @Zoyt. When I'm reporting a score to Game Center, am I supposed to put a variable within the parenthesis of reportScore? Because when I do put my scoring variable the app crashes.

  • Posts: 277

    Thanks @JakAttak and @Zoyt, I'll try that... :)

  • edited July 2014 Posts: 2,820

    @YoloSwag - Make sure you're also including the GameCenter leaderboard along with the score. i.e.: reportScore(self.score,"highscore")
    You can find the leaderboard IDs on iTunesConnect.

  • Posts: 178

    @Zoyt, thanks. Now I can see the test scores!

  • Posts: 277

    @Zoyt, @JakAttak, first-timer concern here... I'm wondering if I understood you well as far as creating the app in iTunes connect in order to test Game Center capabilities.

    When creating the app, Apple asks me about all kind of material and information in order for them to review and publish the app. Off course, I'm not ready to get it reviewed, and much less published for that matter.

    How does that work? should I go ahead, and I will later have an option to tell them not to review the app yet?


  • Posts: 2,051

    @Rodolohe, yes it is a slightly annoying system, you have to provide some of the details such as a screenshots - but don't worry because it will not put it into review yet and you can change all that info later.

  • Posts: 277

    Ok, thanks a lot @JakAttak!

  • Posts: 277

    Alright, it seems to work in sandbox mode in the simulator! Thanks! Also, is it possible to send achievements to game center, @Zoyt?

  • Posts: 2,051

    @Rodolphe, you would have to add your own functions to add that functionality.

  • Posts: 277

    Ok, I'll pass it for now, Objective C is still a little intimidating to me :) I'll report back here if I manage anything...

  • edited July 2014 Posts: 2,051

    @Rodolphe, I just gave it a go and I think I have some code that does what you want.

    In GameCenterAddOn.h add these lines:

    static int reportAchievement(struct lua_State *state);
    static int showAchievements(struct lua_State *state);

    with the other similar lines underneath this one: @property (weak, nonatomic) CodeaViewController *codeaViewController;

    In GameCenterAddOn.m add these lines:

    lua_register(L, "reportAchievement", reportAchievement);
    lua_register(L, "showAchievements", showAchievements);

    with the other similar lines in this function: - (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L

    then add this block of code:

    #pragma mark - Report Achievement Progress
    static int reportAchievement(struct lua_State *state) {
        [gameCenterAddOnInstance reportAchievementAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] percentComplete:(int)lua_tointeger(state, 2)];
        return 1;
    - (void) reportAchievementAction: (NSString*) identifier percentComplete: (float) percent
        GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
        if (achievement)
            achievement.percentComplete = percent;
            [achievement reportAchievementWithCompletionHandler:^(NSError *error)
                 if (error != nil)
                     NSLog(@"Error in reporting achievements: %@", error);
    #pragma mark - Show GameCenter Achievements
    static int showAchievements(struct lua_State *state) {
        [gameCenterAddOnInstance showAchievementsAction];
         return 1;
    - (void) showAchievementsAction
        GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
        if (gameCenterController != nil)
            gameCenterController.gameCenterDelegate = self;
            gameCenterController.viewState = GKGameCenterViewControllerStateAchievements;
            [self.codeaViewController presentViewController: gameCenterController animated: YES  completion:nil];

    anywhere with the other functions (i placed it after the reportScore code)

    This will give you two new lua functions reportAchievement and showAchievements

    showAchievements should bring up the Game Center view showing the achievements.

    reportAchievement is called like so: reportAchievement(identifier, percentage) where identifier is the achievement identifier for the achievement you are updating, and percentage is the percentage complete (so for example if the achievement was kill 5 bosses, and you killed one boss percentage would be 20)

    I haven't tested this code (I don't have an app with achievements) so let me know if it works or doesn't.

  • Posts: 277

    Oh my... oh my... I can't believe it... I'll get back to you with results :)

  • Posts: 277

    it doesn't seem to work right now. I call reportAchievement as such:

    if h ~= nil and h[2] >= 150 and level < 1 then -- If the height of 150m is achieved and the first achievement hasn't been awarded yet
        saveLocalData("level", 1)
        level = readLocalData("level",0)
        reportAchievement("beinnbheigeir", 100) -- award achievement "Beinn Bheigeir"

    where the achievement I want to award is: d'écran 2014-07-31 15.22.08.png

  • Posts: 2,051

    @Rodolphe, I'll take a look. How long ago did you register the achievement?

  • Posts: 277

    A few hours, at the same time as the leaderborad, which works. Something else: calling showAchievements() works as expected !

  • Posts: 2,051

    Oops. I made a typo. it isn't passing the percentage, it's passing the identifier for both variables.. fixed above.

    replace this line [gameCenterAddOnInstance reportAchievementAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] percentComplete:(int)lua_tointeger(state, 1)];

    with this: [gameCenterAddOnInstance reportAchievementAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] percentComplete:(int)lua_tointeger(state, 2)];

    hopefully that fixes it for you.

  • Posts: 277

    it wooooorks!!! All of this is quite amazing, I must say... thanks so much

  • Posts: 2,051

    @Rodolphe, glad it works :)

  • Posts: 277

    Another question already... :D

    I get get an error "attempt to call global 'sendTweet' (a nil value)" when using:

    sendTweet("hello, hello")

    Is this what you, @Zoyt, means when you write "Right now my sharing API only lets you share photos, but that'll change"?

  • Posts: 2,051

    @Rodolphe, that means that the function sendTweet does not exist.

    I believe his addon supports the functions tweetPhoto and facebookPhoto only.

  • edited July 2014 Posts: 2,820

    Thanks @JakAttak for answering those questions. You're the new Obj-C guru here. ;)
    Anyways, @Rodolphe, are you sure you included the add on completely?
    Make sure you have @property (strong, nonatomic) SharingAddOn *sharingAddOn; in AppDelegate.h and self.sharingAddOn = [[SharingAddOn alloc] init]; [self.viewController registerAddon:self.sharingAddOn]; in
    Edit: Also, make sure you're using the one from my StackIt repository, not my snippets off GitHub.
    Edit #2: To attach an image to the Tweet/FaceBook post, just add a second argument with the image.

Sign In or Register to comment.