iPhone - in-App purchase consumable correct approach

前端 未结 3 1599
走了就别回头了
走了就别回头了 2020-12-07 08:57

I have this new app I am creating that will use consumable in-app purchases.

My question is this: how does that work? I mean, imagine the user buys a consumable stuf

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 09:51

    I wanted to share a somewhat unorthodox solution I found to this problem that has the HUGE advantage of not requiring a server. This method allows users to restore their consumable items if the app is deleted and reinstalled, but does not allow them to move the items to a new device (unless all their app data is copied over).

    Data stored in the keychain persists when an app is deleted and reinstalled. The keychain is intended for storing usernames and passwords, but you can also store information about consumable purchases in there. I used the KeychainItemWrapper class, available here: https://developer.apple.com/library/content/samplecode/GenericKeychain/Introduction/Intro.html

    Here is some sample code where I store and retrieve the number of paid hints that a user has remaining:

    //Storing the consumable hint item count
    int hintsLeft = 100;
    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Hints" accessGroup:nil];
    NSString *hintsString = [NSString stringWithFormat:@"%i",hintsLeft];
    [wrapper setObject:hintsString forKey:(id)kSecValueData];
    [wrapper release];
    
    //Retrieving it
    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Hints" accessGroup:nil];
    NSString *numHints = [wrapper objectForKey:(id)kSecValueData];
    [wrapper release];
    int retrievedHints = [numHints intValue];
    

    Notes:

    • the key (id)kSecValueData can't be an arbitrary string, there is a set list of constants that you can use as the key.

    • You will need to add the security framework

提交回复
热议问题