问题
Why it's so hard to find out how to do this is beyond me - so maybe I am trying to do this the wrong way - who knows - well hopefully one of you, lol.
I have a button that says 'Purchase' (for a non-consumable). If the purchase is successful then update NSUserDefaults purchased = YES and the button is removed. All good up to here.
Now when the user deletes the app and wants to reinstall or install on a new device then what I would like to show, in these cases, in place of the 'Purchase' button is a 'Restore' button. 'Restore' button would be clicked and restoreCompletedTransactions would be called. Seems logical to me - user doesn't want to see a 'Purchase' button - they have purchased already.
Problem is NSUserDefaults purchased = YES will not be available on re-install or new install. So my question is how do I determine if the app has been purchased before so I can either display a 'Purchase' button or a 'Restore' button. Is there a way to determine if this in-app purchase has already been made by the user?
Thanks in advance, Byron.
回答1:
There is no good way to transfer state from one app install to another app install in the future. You might be able to abuse the keychain for this, but you shouldn't.
Just show "Purchase" all the times. On iOS that's the standard way of handling this situation. People are used to see a purchase button even if they have purchased before, they will understand what that means. Of course you still need a restore button somewhere.
Depending on the kind of store you use you have two options to place the restore button.
If you have a store like interface, e.g. a tableView which lists all your products, put a "Restore Purchase" cell at the end of your tableView. Here is the interface of PCalc Lite as an example:
If you don't have a store like interface, put a button in the settings section of your app. That's how Disco Zoo does it:
回答2:
You could use a backend server that the app interrogates to determine if the user has purchased by way of signing into an account on the device. Apple will require you to have some sort of checking facility for the reasons you pointed out...users should not have to re purchase on new device or other devices they might own. You must make the account sign up optional to the user, again Apple will require this.
回答3:
I know it's an old question and you probably solved it, but I had a similar problem in terms of designing a user friendly interface for the purchase mechanism and found a solution like this:
-(IBAction) purchase
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:<# A brief explanation #>
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Restore",@"Purchase", nil];
alert.delegate = self;
alert.tag = _IAPTAG;
[alert show];
}
then in the alert view delegate function:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == _IAPTAG)
{
if(buttonIndex == 0)
{
//cancel
}
else if(buttonIndex == 1)
{
//restore
}
else if(buttonIndex == 2)
{
//purchase
}
}
}
So the restore option will restore and the purchase will purchase, eventually. If the user is buying for the first time, then s/he won't be worried about buying twice anyway. If the user bought before and is installing the app on another device, then s/he will know that the purchase button will give the option to restore.
来源:https://stackoverflow.com/questions/14667456/in-app-purchase-how-to-determine-if-a-purchase-has-already-been-made