Delegate methods for Store Kit in Objective-C

Creating QR in Objective-C Delegate methods for Store Kit

Listing 18.8 Delegate methods for Store Kit
Painting QR Code In Objective-C
Using Barcode drawer for iPhone Control to generate, create QR Code 2d barcode image in iPhone applications.
www.OnBarcode.com
Data Matrix ECC200 Generation In Objective-C
Using Barcode creator for iPhone Control to generate, create Data Matrix image in iPhone applications.
www.OnBarcode.com
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { for (SKPaymentTransaction *transaction in transactions) { switch (transaction.transactionState) {
Draw QR Code ISO/IEC18004 In Objective-C
Using Barcode maker for iPhone Control to generate, create Denso QR Bar Code image in iPhone applications.
www.OnBarcode.com
Barcode Encoder In Objective-C
Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Download from Wow! eBook <www.wowebook.com>
UCC - 12 Printer In Objective-C
Using Barcode generator for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.
www.OnBarcode.com
EAN13 Drawer In Objective-C
Using Barcode generation for iPhone Control to generate, create EAN 13 image in iPhone applications.
www.OnBarcode.com
Creating a simple store interface
Generating Barcode In Objective-C
Using Barcode drawer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Making GS1 - 12 In Objective-C
Using Barcode maker for iPhone Control to generate, create Universal Product Code version E image in iPhone applications.
www.OnBarcode.com
case SKPaymentTransactionStatePurchased: [self completeTransaction:transaction]; break; case SKPaymentTransactionStateFailed: [self failedTransaction:transaction]; break; case SKPaymentTransactionStateRestored: [self restoreTransaction:transaction]; default: break; } } }
Decoding QR Code ISO/IEC18004 In VB.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Draw Denso QR Bar Code In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create QR image in VS .NET applications.
www.OnBarcode.com
Store Kit calls this method after the user has tried to purchase an item, and it s used as a controller based on the status of the purchase. A purchase has three potential states:
PDF417 Generation In None
Using Barcode creation for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
Create GTIN - 12 In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create UPC Code image in .NET framework applications.
www.OnBarcode.com
SKPaymentTransactionStatePurchased
Generating Barcode In None
Using Barcode printer for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Generating Code128 In None
Using Barcode maker for Font Control to generate, create Code-128 image in Font applications.
www.OnBarcode.com
Occurs when the transaction was successful. At this point, you should deliver the content to the user and record the transaction history. SKPaymentTransactionStateFailed The transaction may fail for a number of reasons, including insufficient funds or a network error. If this is the case, you need to notify the user that their purchase wasn t completed. SKPaymentTransactionStateRestored Occurs when the user has already purchased an item. If this is the state of the transaction, you should deliver the content to the user as if it was a new transaction. Now that you ve implemented this driver method, you need to implement each of the response methods that it calls. Add the code from listing 18.9 to your RootViewController.m file.
Code 39 Drawer In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Code 3/9 image in VS .NET applications.
www.OnBarcode.com
Code 128 Code Set A Generation In None
Using Barcode generation for Microsoft Word Control to generate, create Code128 image in Word applications.
www.OnBarcode.com
Listing 18.9 Store Kit response methods
Barcode Drawer In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Barcode Recognizer In C#
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 18.11 A purchase confirmation as shown on the iPhone
Paint Barcode In .NET Framework
Using Barcode generator for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Paint Matrix Barcode In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create 2D Barcode image in .NET applications.
www.OnBarcode.com
- (void) completeTransaction: (SKPaymentTransaction *)transaction { Completes [self.navigationController popViewControllerAnimated:YES]; transaction [self recordTransaction: transaction]; and [self provideContent: transaction.payment.productIdentifier]; delivers [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; content } - (void) restoreTransaction: (SKPaymentTransaction *)transactionB
Download from Wow! eBook <www.wowebook.com>
In-app purchasing using Store Kit Restores transaction
[self completeTransaction:transaction]; } - (void) failedTransaction: (SKPaymentTransaction *)transaction { if (transaction.error.code != SKErrorPaymentCancelled) { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error in purchase" message:transaction.error.description delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; } [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; }
Notifies user that transaction failed
As we stated before, the actions for completing and restoring transactions are usually similar. For the example, you have restoreTransaction call completeTransaction to save lines of code. When a purchase is successful, Store Kit notifies the user via a UIAlertView. Figure 18.12 shows what this view looks like in the application. The process you should follow when completing a transaction is usually the same in every application. You call a series of methods to record the history, provide the content to the user, and finalize the transaction. Listing 18.10 details each of these methods. To record the transactions, you keep a plist file on disk containing the product identifiers of every product purchased by the user. Although you don t do much with the recorded transaction information in this application, it s good practice to track it.
Listing 18.10 Recording transactions and delivering content
- (void) recordTransaction:(SKPaymentTransaction * )transaction { if([self.transactionHistory containsObject: transaction.payment.productIdentifier]) return; NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString * path = [documentsDirectory stringByAppendingPathComponent:@"history.plist"]; [self.transactionHistory addObject:transaction.payment.productIdentifier]; [self.transactionHistory writeToFile:path atomically:YES]; }
Saves history to disk
- (void) provideContent:(NSString * )productIdentifier { UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:[NSString stringWithFormat:@"%@.jpeg",productIdentifier]],self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
Saves image to user s camera roll
Download from Wow! eBook <www.wowebook.com>
Summary
} - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Purchase Complete" message:@"The wallpaper has been saved to your camera roll." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; }
Notifies user
The code first checks to see if the user has already purchased the given item. If so, you don t need to double-record it. After that, you resolve the path to the file history.plist in the application s documents directory and write the history to it B. When you re creating a store in an application, the provideContent method varies depending on what type of content you re selling. In the case of the wallpaper sale application, you need to save the purchased wallpaper from the application s directory to the user s camera roll. You can do this using the UIImageWriteToSavedPhotosAlbum method C. After this method completes, it calls the selector you passed in, which notifies the user that the wallpaper was saved to their device D. The user now has full access to that wallpaper via their camera roll. The last method in #3 shows what to do if an error occurs. It s important to notify the user that their payment wasn t processed. Otherwise, you ll have unhappy customers thinking they paid for something and it wasn t delivered.
Copyright © OnBarcode.com . All rights reserved.