我有一個與iAP的小問題。我的代碼大部分工作。只是當用戶取消購買時,設備不會認爲它已經結束並保留。因此,如果你試圖再次購買,你會購買2件產品......這是不好的,因爲他們只需要一件。我必須缺少一些代碼。我將附上一個控制檯日誌,以便您可以看到發生了什麼。用戶取消在應用程序購買。如何結束交易
2010-12-21 15:47:29.364[204:307] Parental-controls are disabled (I launch the iAP)
2010-12-21 15:47:33.816[204:307] Win (There are products)
2010-12-21 15:47:37.043[204:307] Buying... (I pressed buy)
2010-12-21 15:47:40.661[204:307] FailedTransaction (Essentially I canceled)
2010-12-21 15:47:40.674[204:307] DefaultAction (which is to dismiss the modal view)
2010-12-21 15:47:42.738[204:307] Parental-controls are disabled (reopen the iAP view)
2010-12-21 15:47:44.440[204:307] Win
2010-12-21 15:47:46.939[204:307] Buying...
2010-12-21 15:47:46.945[204:307] Buying...
2010-12-21 15:47:48.414[204:307] FailedTransaction
2010-12-21 15:47:48.427[204:307] DefaultAction
2010-12-21 15:47:48.437[204:307] FailedTransaction
2010-12-21 15:47:48.449[204:307] DefaultAction
2010-12-21 15:47:50.622[204:307] Parental-controls are disabled
2010-12-21 15:47:51.314[204:307] Win
2010-12-21 15:47:51.883[204:307] Buying...(see how it compounds?)
2010-12-21 15:47:51.889[204:307] Buying...
2010-12-21 15:47:51.896[204:307] Buying...
2010-12-21 15:48:10.764[204:307] Posted5
2010-12-21 15:48:10.782[204:307] Yipeee!
2010-12-21 15:48:10.810[204:307] Posted5 (It gave the users 15 credits!)
2010-12-21 15:48:10.869[204:307] Yipeee!
2010-12-21 15:48:10.897 204:307] Posted5
2010-12-21 15:48:10.912[204:307] Yipeee!
這裏是我運行代碼:
#import "PurchaserViewController.h"
@implementation PurchaserViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"yayadaadul.com/checkcredits.php"]]];
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"Parental-controls are disabled");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.company.name.song"]];
productsRequest.delegate = self;
[productsRequest start];
} else {
NSLog(@"Parental-controls are enabled");
}
}
- (IBAction)purchase {
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.changedwe.worksinxcode.song"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction)stop {
[player stop];
}
- (IBAction)play {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@/Song.mp3", documentsDirectory];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:nil];
[player play];
}
- (void)downloadFromURL:(NSURL *)url {
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@/Song.mp3", documentsDirectory];
[data writeToFile:file atomically:YES];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
SKProduct *validProduct = nil;
int count = [response.products count];
if (count > 0) {
validProduct = [response.products objectAtIndex:0];
NSLog(@"Win");
} else if (!validProduct) {
NSLog(@"No products available");
}
}
-(IBAction)home {
[self dismissModalViewControllerAnimated:YES];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"Buying...");
break;
break;
case SKPaymentTransactionStatePurchased:
[self downloadFromURL:[NSURL URLWithString:@""]];
NSString *post =[NSString stringWithFormat:@"username=lol&cval=5"];
NSString *hostStr = @"http://mysite.com/subdom/ccredit.php?";
hostStr = [hostStr stringByAppendingString:post];
NSLog(@"Posted5");
NSLog(@"Yipeee!");
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://Action to Post.php?"]]];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(@"Restored");
break;
case SKPaymentTransactionStateFailed:
NSLog(@"FailedTransaction");
// [self finishTransaction:transaction wasSuccessful:NO];
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"An error encounterd");
}
//You need to clear their purchases here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(@"DefaultAction");
[self dismissModalViewControllerAnimated:YES];
break;
}
}
}
實際上,我發現的大多數教程都表明在那裏添加交易觀察員,可能是因爲他們正在考慮一次購買的簡單解決方案。但是,您可以在哪裏添加一次交易觀察員?因爲我讀過,如果你在不正確的時間添加它,你也可能會遇到問題。在我的代碼中,我添加了一個bool變量來檢查事務觀察者是否已添加到不再添加它,但我不認爲這是最好的解決方案,您對此有何看法? – CarmenA 2015-12-10 11:37:57