Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • In app purchase sample code for iOS apps

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 927
    Comment on it

    Import StoreKit.Framework into your xcode project first.

    Apple allows three types of purchases within the app and Apple terms them as consumables, non-consumables and subscriptions.

    Consumables are products that are consumed immediately. This means, he will be charged again when he attempts a purchase. Non-Consumables are features that are purchased exactly once for lifetime.

    Subscriptions behave like non-consumables during the subscribed period and like consumables after that. You as a developer have to ensure that anything that is subscribed by the user is available across all of his iTunes synced devices when they are purchased from one device. Hence, do not lock in-app purchases to UDIDs. This might even get your app rejected.

    Preparing your iTunes Connect

    To start in-app purchases, you need iTunes Connect account.

    Step 1: First is to create an App ID and enable in-app purchases for that. This App ID shouldnt have any wild card characters or else, the in-app purchases option will be grayed.

    Step 2: Create provisioning profiles (Development and Distribution) using this App ID.

    Step 3: You need to create product references in your iTunes account. Each individual in-app purchase should be uniquely identifiable something like, com.mycompany.myiproduct1 To create a new in-app purchase, open your itunes connect and choose Manage In-App purchases. Choose the app for which you want to setup in-app purchases and click next. The reference name is the name that appears during the in-app purchase prompt. The Product ID should be unique. Type in the other required detail in this page and click save.

    Step 4: The fourth and final step is to create test user accounts. After you program the app, you might want to test the app. You can use these accounts to login to the App Store. The purchases will be processed as if it were real but no financial transactions will take place.

    Code steps:-

    Step 1: Adding StoreKit.Framework.

    Step 2: Write a function for payment permits

    if ([SKPaymentQueue canMakePayments])
    {
    ... // Display a store to the user.
    }
    else
    {
    ... // Warn the user that purchases are disabled.
    }
    

    Step 3: Retrieving the product information and populating the UI.

    - (void) requestProductData
    {
    SKProductsRequest *request= [[SKProductsRequest alloc]
    initWithProductIdentifiers: [NSSet setWithObject: kMyFeatureIdentifier]];
    request.delegate = self;
    [request start];
    }
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
            (SKProductsResponse *)response
    {
    NSArray *myProduct = response.products;
    // populate UI
    }
    

    Step 4: Implementing the callback.

    - (void)paymentQueue:updatedTransactions(SKPaymentQueue *)queue
    updatedTransactions:(NSArray *)transactions
    {
    for (SKPaymentTransaction *transaction in transactions)
    {
    switch (transaction.transactionState)
    {
    case SKPaymentTransactionStatePurchased:
    // take action to purchase the feature
    [self provideContent: transaction.payment.productIdentifier];
    break;
    case SKPaymentTransactionStateFailed:
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
    // Optionally, display an error here.
    }
    // take action to display some error message
    break;
    case SKPaymentTransactionStateRestored:
    // take action to restore the app as if it was purchased
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    default:
    break;
    }
    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }
    }
    

    For more help please see the attached code file. or click here *Happy Coding :) *

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: