非消耗品 (first_view と second_view) を使用して支払いを完了した後、2 つの新しいビューを開く 2 つのボタンがあります。コードは 1 つの製品で正常に動作しますが、複数の製品に使用する方法がわかりません。これは初めての経験です。アプリ内購入コーディング付き。このコードを使用している以下のproductsRequestメソッドを更新する必要があると思います:
implementation ViewController
@synthesize firstBTN, secondBTN,;
#define kStoredData @"first_view"
-(BOOL)IAPItemPurchased {
NSError *error = nil;
NSString *password = [SFHFKeychainUtils getPasswordForUsername:@"username" andServiceName:kStoredData error:&error];
if ([password isEqualToString:@"password"]) return YES; else return NO;
}
-(IBAction)firstBTNPressed:(id)sender {
if ([self IAPItemPurchased]) {
firstViewController *controller = [[firstViewController alloc] initWithNibName:@"firstView" bundle:nil];
[self presentModalViewController:controller animated:YES];
[controller release];
} else {
// not purchased so show a view to prompt for purchase
askToPurchase = [[UIAlertView alloc]
initWithTitle:@"Feature Locked"
message:@"Purchase Feature?"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Yes", @"No", nil];
askToPurchase.delegate = self;
[askToPurchase show];
[askToPurchase release];
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Complete"
message:@"You have unlocked Feature 2!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
[tmp release];
NSError *error = nil;
[SFHFKeychainUtils storeUsername:@"username" andPassword:@"password" forServiceName:kStoredData updateExisting:YES error:&error];
[firstBTN setBackgroundImage:nil forState:UIControlStateNormal];
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default:
break;
}
}
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response
{
SKProduct *validProduct = nil;
int count = [response.products count];
if (count>0) {
validProduct = [response.products objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"first_view"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Not Available"
message:@"No products to purchase"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
[tmp release];
}
}
-(void)requestDidFinish:(SKRequest *)request
{
[request release];
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
}
#pragma mark AlertView Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex {
if (alertView==askToPurchase) {
if (buttonIndex==0) {
if ([SKPaymentQueue canMakePayments]) {
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"first_view"]];
request.delegate = self;
[request start];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Prohibited"
message:@"Parental Control is enabled, cannot make a purchase!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
[tmp release];
}
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self IAPItemPurchased]) {
[firstBTN setBackgroundImage:nil forState:UIControlStateNormal];
} else {
[firstBTN setBackgroundImage:[UIImage imageNamed:@"Locked.png"] forState:UIControlStateNormal];
}
}
2番目のビューを開く2番目のボタンで同じことをするのを手伝ってくれませんか。前もって感謝します。