我在使用 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;
}
}
}
最佳答案
是的。您需要实现 SKRequest 的成功和失败委托(delegate)方法。请参阅文档。 http://developer.apple.com/library/ios/#documentation/StoreKit/Reference/SKRequestDelegate/Reference/Reference.html#//apple_ref/doc/uid/TP40008416
- (void)requestDidFinish:(SKRequest *)request
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
关于iphone - 用户取消应用内购买。如何结束交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4505059/