3
我已使用以下代碼進行應用內購買。如何在ios5中進行應用內購買?
- (void)viewDidLoad {
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"Can Buy Product");
SKProductsRequest *productRequest=[[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithObject:@"com.mycompany.myproduct.productpack"]];
productRequest.delegate=self;
[productRequest start];
}
else {
NSLog(@"Product Can't be purchased");
}
}
-(IBAction)purchasePack1 {
SKPayment *payment=[SKPayment paymentWithProductIdentifier:@"com.mycompany.myproduct.productpack"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
validProduct=nil;
int count=[response.products count];
if (count>0) {
NSLog(@"Product Avail");
validProduct=[response.products objectAtIndex:0];
}
else {
NSLog(@"No Product avail");
[purchaseBtn setHidden:TRUE];
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"Purchasing");
[activityIndicatorObj setHidden:FALSE];
[activityIndicatorObj startAnimating];
break;
case SKPaymentTransactionStatePurchased:
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Congrats" message:@"Thanks For Purchasing " delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alt show];
[alt release];
NSLog(@"Purchased");
[activityIndicatorObj stopAnimating];
[activityIndicatorObj setHidden:TRUE];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code!=SKErrorPaymentCancelled) {
NSLog(@"Cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
UIAlertView *alt1=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Some Error Encountered!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alt1 show];
[alt1 release];
NSLog(@"Failed");
[activityIndicatorObj stopAnimating];
[activityIndicatorObj setHidden:TRUE];
break;
case SKPaymentTransactionStateRestored:
NSLog(@"Restored");
[activityIndicatorObj stopAnimating];
[activityIndicatorObj setHidden:TRUE];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default:
break;
}
}
}
當我點擊購買按鈕我收到一個警告消息說「你已經購買了這個產品,還沒有下載的,你想下載?」 當我點擊是,我沒有得到任何應用程序的響應,我相信該產品沒有下載。任何人都可以告訴我這個問題的建議。提前致謝。
當您加載視圖時是否打印「Product avail」文字? –
paymentWithProductIdentifier:自iOS 5起棄用。您應該使用paymentWithProduct:代替。 –