Tuesday 15 April 2014

In-App-Purchase in iOS

Step 1 :iTunes Connect (Login Ur Account) -> Manage Subscriptions -> App(Which One) -> Manage the InAppPurchase -> Select Type -> Create a Product id (ex:com.ashuss.AppPremium) -> Approve.

Step 2 : In Project Header --> Tab the Capabilities --> InApp Purchase to Enable it.



Step 3: Add a StoreKit.framwork into Your Project

Step 4: Add the In-App-Purchase Code into Your Project

-(void)viewWillAppear:(BOOL)animated 
           (or)
-(void)viewDidLoad
{
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}

-(IBAction)Btn_Click:(id)sender
{
    BOOL productpurchased = [[NSUserDefaults standardUserDefaults] boolForKey:@"com.ashuss.AppPremium"];
    if (productpurchased)
    {
        //iPhone_HSMO *vmo = [[iPhone_HSMO alloc]init];
        //[self.navigationController pushViewController:vmo animated:YES];
    }
    else
    {
        objAlert = [[UIAlertView alloc]initWithTitle:@"App Premium" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Restore",@"Buy", nil];
        [objAlert show];
        [objAlert setTag:1];
    }
}

-(void)alertView:(UIAlertView *)objAlert didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([objAlert tag]==1)
{
if (buttonIndex==0)
{
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}
else if (buttonIndex==1)
{
SKProductsRequest *request= [[SKProductsRequest alloc]initWithProductIdentifiers: [NSSet setWithObject: @"com.ashuss.AppPremium"]];
            request.delegate = self;
            [request start];
}
}
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
    NSArray *myProduct = response.products;
    NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);
    
    //Since only one product, we do not need to choose from the array. Proceed directly to payment.
    
    SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
    [[SKPaymentQueue defaultQueue] addPayment:newPayment];
    
    [request autorelease];
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"Transaction Completed");
    // You can create a method to record the transaction.
    //[self recordTransaction: transaction];
    
    // You should make the update to your app based on what was purchased and inform user.
    [self provideContent: transaction.payment.productIdentifier];
    
    // Finally, remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"Transaction Restored");
    // You can create a method to record the transaction.
    // [self recordTransaction: transaction];
    
    // You should make the update to your app based on what was purchased and inform user.
    [self provideContent: transaction.payment.productIdentifier];
    
    // Finally, remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

-(void)provideContent: (NSString *)productIdentifier
{
    if ([productIdentifier isEqualToString:@"com.ashuss.AppPremium"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
        //[[NSUserDefaults standardUserDefaults] setDouble:1.11 forKey:aa];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"InAppPurchase" object:productIdentifier userInfo:nil];
        
        iPhone_HSMO *vmo = [[iPhone_HSMO alloc]init];
        [self.navigationController pushViewController:vmo animated:YES];
    }
    else
    {
        NSLog(@"Failed");
    }
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    //[activityIndicator stopAnimating];
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // Display an error here.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Unsuccessful"
                                                        message:@"Your purchase failed. Please try again."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    
    // Finally, remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


No comments: