SDK

Implement In-App Purchase

With the Notificare's Monetize add-on you can easily sell virtual goods in the App Store. Our library will provide you all the mechanisms to quickly develop an engaging in-app store experience where you can sell digital products.

Please note that be able to complete a transaction while using sandbox servers, you will need to create test users with access to your app in Apple’s Developer Portal. Your can find more information about test users here.

In your AppDelegate.m or AppDelegate.swift add the following delegate that will be trigger as soon as your products are ready to be consumed:

-(void)notificarePushLib:(NotificarePushLib *)library didLoadStore:(nonnull NSArray<NotificareProduct *> *)products{
    for (NotificareProduct *prod in products) {
        NSLog(@"%@", [prod productName]);
    }
}
func notificarePushLib(_ library: NotificarePushLib, didLoadStore products: [NotificareProduct]) {
    for prod: NotificareProduct in products {
        print("\(prod.productName)")
    }
}

If there is some issue with your products or your account, the following delegate is triggered instead. If it is a network issue, Notificare will automatically retry and only if errors are fatal it will trigger this delegate:

-(void)notificarePushLib:(NotificarePushLib *)library didFailToLoadStore:(nonnull NSError *)error{
    //Handle error
}
func notificarePushLib(_ library: NotificarePushLib) throws {
    //Handle error
}

If your store is loaded successfully, at any time you can request the list of products with the following method:

[[NotificarePushLib shared] fetchProducts:^(id  _Nullable response, NSError * _Nullable error) {
    if (!error) {
        //Handle array of NotificareProduct objects
    }
}];
NotificarePushLib.shared().fetchProducts({(_ response: Any?, _ error: Error?) -> Void in
    if error == nil {
        //Handle array of NotificareProduct objects
    }
})

You can also retrieve a list of the purchased products, this can be used to allow or not products to be bought again (depending on their type) or automatically give access to the content they unlock. To do that, simply invoke the following method:

[[NotificarePushLib shared] fetchPurchasedProducts:^(id  _Nullable response, NSError * _Nullable error) {
    if (!error) {
        //Handle array of NotificareProduct objects
    }
}];
NotificarePushLib.shared().fetchPurchasedProducts({(_ response: Any?, _ error: Error?) -> Void in
    if error == nil {
        //Handle array of NotificareProduct objects
    }
})

To get a specific product using its identifier, invoke the following method:

[[NotificarePushLib shared] fetchProduct:@"PRODUCT_SKU" completionHandler:^(id  _Nullable response, NSError * _Nullable error) {
    if (!error) {
        //response will be a NotificareProduct object
    }
}];
NotificarePushLib.shared().fetchProduct("PRODUCT_SKU", completionHandler: {(_ response: Any?, _ error: Error?) -> Void in
    if error == nil {
        //response will be a NotificareProduct object
    }
})

Finally to allow a user to buy a product, simply invoke the method below. Invoking it will trigger some of the delegates listed below in this page.

[[NotificarePushLib shared] buyProduct:(NotificareProduct*)prod];
NotificarePushLib.shared().buy((prod as? NotificareProduct))

Handle Downloads

You can eventually use the methods below to handle your Non-Consumable's hosted content (if applicable). The accepted parameter should be an array of SKDownload objects.

To pause one or more downloads, invoke the following method:

[[NotificarePushLib shared] pauseDownloads:downloads];
NotificarePushLib.shared().pauseDownloads(downloads)

To resume downloading one or more downloads, invoke the following method:

[[NotificarePushLib shared] resumeDownloads:downloads];
NotificarePushLib.shared().resumeDownloads(downloads)

To cancel one or more downloads, invoke the following method:

[[NotificarePushLib shared] cancelDownloads:downloads];
NotificarePushLib.shared().cancelDownloads(downloads)

Finally when a download is completed, you can access the hosted content by invoking the following methods:

NSString * dir = [[NotificarePushLib shared] contentPathForProduct:[product identifier]];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:&error];
var dir: String = NotificarePushLib.shared().contentPath(forProduct: product.identifier())
var error: Error?
var directoryContents = try? FileManager.default.contentsOfDirectory(atPath: dir)

In-App Purchase Delegates

The method above will start the purchase flow handled by iOS and the App Store. You can then use the following delegates to understand the state of the purchase or the hosted content associated to the product. Use should use them to update UI or show errors.

- (void)notificarePushLib:(NotificarePushLib *)library didFailProductTransaction:(SKPaymentTransaction *)transaction withError:(NSError *)error{
    //Triggered when purchase did not finish or failed
}

- (void)notificarePushLib:(NotificarePushLib *)library didCompleteProductTransaction:(SKPaymentTransaction *)transaction{
    //Triggered when purchase is completed
}

- (void)notificarePushLib:(NotificarePushLib *)library didRestoreProductTransaction:(SKPaymentTransaction *)transaction{
    //Triggered when your try to buy a product that is already bought
}

- (void)notificarePushLib:(NotificarePushLib *)library didStartDownloadContent:(SKPaymentTransaction *)transaction{
    //Triggered when it starts downloading hosted content
}

- (void)notificarePushLib:(NotificarePushLib *)library didPauseDownloadContent:(SKDownload *)download{
    //Triggered when a download is paused
}

- (void)notificarePushLib:(NotificarePushLib *)library didCancelDownloadContent:(SKDownload *)download{
    //Triggered when a download is cancelled
}

- (void)notificarePushLib:(NotificarePushLib *)library didReceiveProgressDownloadContent:(SKDownload *)download{
    //Triggered while a download is in progress
    NSLog(@"didReceiveProgressDownloadContent %@ %f %f", download, [download progress], [download timeRemaining]);
}

- (void)notificarePushLib:(NotificarePushLib *)library didFailDownloadContent:(SKDownload *)download{
    //Triggered when a download fails
}

- (void)notificarePushLib:(NotificarePushLib *)library didFinishDownloadContent:(SKDownload *)download{
    //Triggered when a download is completed
}
func notificarePushLib(_ library: NotificarePushLib, didFailProductTransaction transaction: SKPaymentTransaction) throws {
    //Triggered when purchase did not finish or failed
}

func notificarePushLib(_ library: NotificarePushLib, didCompleteProductTransaction transaction: SKPaymentTransaction) {
    //Triggered when purchase is completed
}

func notificarePushLib(_ library: NotificarePushLib, didRestoreProductTransaction transaction: SKPaymentTransaction) {
    //Triggered when your try to buy a product that is already bought
}

func notificarePushLib(_ library: NotificarePushLib, didStartDownloadContent transaction: SKPaymentTransaction) {
    //Triggered when it starts downloading hosted content
}

func notificarePushLib(_ library: NotificarePushLib, didPauseDownloadContent download: SKDownload) {
    //Triggered when a download is paused
}

func notificarePushLib(_ library: NotificarePushLib, didCancelDownloadContent download: SKDownload) {
    //Triggered when a download is cancelled
}

func notificarePushLib(_ library: NotificarePushLib, didReceiveProgressDownloadContent download: SKDownload) {
    //Triggered while a download is in progress
    print("didReceiveProgressDownloadContent \(download) \(download.progress) \(download.timeRemaining)")
}

func notificarePushLib(_ library: NotificarePushLib, didFailDownloadContent download: SKDownload) {
    //Triggered when a download fails
}

func notificarePushLib(_ library: NotificarePushLib, didFinishDownloadContent download: SKDownload) {
    //Triggered when a download is completed
}

This is all you need to display, sell and handle hosted content for your in-app products.