0

I have an app that has consumable IAP. It's an IAP that users consumes it over the time, something like the IAP has 100 points, while users may use 10 today and 50 tomorrow, so the user would have 100-10-50=40 points left for later use. The app gives out initial points of 100 to let users to try it out. I used UserDefaults to record that and it works fine, EXCEPT when user delete the app --- when I tested two methods: 1, delete and reinstall, I got the initial 100 points again; 2, I reset iPhone language and got the initial 100 points again. This means users wouldn't go thru too much trouble to get the initial 100 points, which would basically stop my IAP from selling.

I currently don't have an external server to record/verify IAP. I don't want a bullet-proof mechanism to prevent hack. All I want is something that can survive delete/reinstall. After reading stackoverflow pages and I learned that 1, keychain survive deletion/reinstall, 2, it's usually used for password, 3, some developers use it to store IAP flags (bought the IAP or not), but 4, I don't know if it's okay to store a NSNumber (the 40 points left over in the example above). I wonder if keychain would be suitable for my case.

If keychain is okay to store NSNumber, anyone has some code to share? Thanks.

Updates with Lockbox: why I got Apple Mach-O-Linker Error: Objc-class-ref in ViewController.o? Something related to ARC? I saw Granoff (Lockbox developer) said ARC has been taken care of automatically. What I did was add Lockbox.h and .m file into my project and import to my app. Then app doesn't run because of the error. However when I tried Lockbox provided sample project, it works. Any insight? Thanks.

Lockbox error I got:

    Undefined symbols for architecture i386:
    "_OBJC_CLASS_$_Lockbox", referenced from:
    objc-class-ref in ViewController.o
    ld: symbol(s) not found for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

I added "Security" framework. After adding security framework, I copied both Lockbox.h and .m files into my app. In ViewController.m file where I plan to use lockbox methods, I added "#import "Lockbox.h"". I could run at this step, without error. I then added the following line: if ([Lockbox stringForKey:mylockboxstring] != nil) {} I got error shown above.

Tony Xu
  • 3,031
  • 4
  • 32
  • 43

1 Answers1

3

Yes.

Have a look at http://github.com/granoff/Lockbox. It's a class exactly for this, although you may have to either extend it to store NSNumbers or use one of the currently supported data types, like an NSString.

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61
  • I assume Apple gets no issue with this, right? I'll try this today and accept your answer if it works. Thanks! – Tony Xu Nov 12 '12 at 20:07
  • Nope, no issue. The Keychain APIs are public. I've used this in several apps, all on the App Store now. – Mark Granoff Nov 12 '12 at 20:28
  • why I got Apple Mach-O-Linker Error: Objc-class-ref in ViewController.o? Something related to ARC? I saw Granoff (Lockbox developer) said ARC has been taken care of automatically. What I did was add Lockbox.h and .m file into my project and import to my app. Then app doesn't run because of the error. However when I tried Lockbox provided sample project, it works. Any insight? Thanks. – Tony Xu Nov 12 '12 at 21:33
  • The LockBox sample project does not use ARC, fwiw, but the code should compile correctly in both ARC and non-ARC environments. Can you paste the exact error you're seeing as it relates to LockBox? – Mark Granoff Nov 12 '12 at 21:45
  • yes, I added this framework. For the error, please see updates in original post. Thanks. – Tony Xu Nov 12 '12 at 21:50
  • Did you copy the files Lockbox.m and Lockbox.h into your project? And if you did, in the code where you call the Lockbox class methods, you must `#import "Lockbox.h"`, of course. – Mark Granoff Nov 12 '12 at 21:52
  • Yes, I copied (not only link) both Lockbox.h and .m into my project. When I type #import L... I got automatic text prompt with Lockbox.h in my ViewController.m file when I called "Lockbox setString....". Actually, does this line of code problematic? `if ([Lockbox stringForKey:mylockboxstring] != nil) {}` because I got error with import Lockbox.h and this line. with no other implementation added yet. – Tony Xu Nov 12 '12 at 21:59
  • I think I fixed it. I added "Lockbox.m" into "compile sources", which points me to where errors are. Then I followed the fix suggestion given by Xcode --- something like add "_bridge" to do the cast. Then it runs. Seems all works now. Thank you Mark --- just found out you are the developer! Thanks again! I accepted your answer. – Tony Xu Nov 12 '12 at 22:24
  • @MarkGranoff I get the following error using ARC, Lockbox/Lockbox.m:73:22: error: implicit conversion of C pointer type 'CFTypeRef' (aka 'const void *') to Objective-C pointer type 'id' requires a bridged cast [dict setObject: accessibility forKey: (LOCKBOX_ID) kSecAttrAccessible]; any ideas? – nylund Feb 22 '13 at 12:48
  • @MarkGranoff adding the LOCKBOX_ID macro makes it compile but I do not know if it is the correct fix... – nylund Feb 22 '13 at 12:50
  • @nylund Make sure you have the most up to date sources. The sources on Github already contain that modification. And yes, that is the right fix here. – Mark Granoff Feb 22 '13 at 14:59