// // UIViewController+Logger.cpp // Unity-iPhone // // Created by Hl Zhang on 2023/3/21. // #import "UIViewController+Purchase.h" #import "Utilities.h" #import "StoreManager.h" #import "StoreObserver.h" #import "AppConfiguration.h" #import static Utilities *utility; @implementation UIViewController (Purchase) -(void)initPurchaseEnv { utility = [[Utilities alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleProductRequestNotification:) name:PCSProductRequestNotification object:[StoreManager sharedInstance]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePurchaseNotification:) name:PCSPurchaseNotification object:[StoreObserver sharedInstance]]; } /// Retrieves product information from the App Store. -(void)fetchProductInformation { // First, let's check whether the user is allowed to make purchases. Proceed if they are allowed. Display an alert, otherwise. if ([StoreObserver sharedInstance].isAuthorizedForPayments) { NSArray *identifiers = utility.identifiers; if (identifiers != nil) { if (identifiers.count > 0) { Section *section = [[Section alloc] initWithName:PCSProductsInvalidIdentifiers elements:identifiers]; // Refresh the UI with identifiers to be queried. // [self switchToViewController:ParentViewControllerSegmentProducts]; // [self.products reloadWithData:[NSMutableArray arrayWithObject:section]]; // Fetch the product information. [[StoreManager sharedInstance] startProductRequestWithIdentifiers:identifiers]; } else { // Warn the user that the resource file does not contain anything. [self alertWithTitle:PCSMessagesStatus message:[NSString stringWithFormat:@"%@.%@ %@", PCSProductIdsPlistName, PCSProductIdsPlistFileExtension, PCSMessagesEmptyResource]]; } } else { // Warn the user that the resource file could not be found. [self alertWithTitle:PCSMessagesStatus message:[NSString stringWithFormat:@"%@ %@.%@.", PCSMessagesResourceNotFound, PCSProductIdsPlistName, PCSProductIdsPlistFileExtension]]; } } else { // Warn the user that they are not allowed to make purchases. [self alertWithTitle:PCSMessagesStatus message:[NSString stringWithFormat:@"%@", PCSMessagesCannotMakePayments]]; } } /// Creates and displays an alert. -(void)alertWithTitle:(NSString *)title message:(NSString *)message { UIAlertController *alertController = [utility alertWithTitle:title message:message]; [self.navigationController presentViewController:alertController animated:YES completion:nil]; } #pragma mark - Handle PCSProductRequest Notification /// Updates the UI according to the product request notification result. -(void)handleProductRequestNotification:(NSNotification *)notification { StoreManager *productRequestNotification = (StoreManager*)notification.object; PCSProductRequestStatus status = (PCSProductRequestStatus)productRequestNotification.status; if (status == PCSStoreResponse) { // Switch to the Products view controller. // [self switchToViewController:ParentViewControllerSegmentProducts]; // [self.products reloadWithData:productRequestNotification.storeResponse]; // self.segmentedControl.selectedSegmentIndex = ParentViewControllerSegmentProducts; } else if (status == PCSRequestFailed) { [self alertWithTitle:PCSMessagesProductRequestStatus message:productRequestNotification.message]; } } #pragma mark - Handle PCSPurchase Notification /// Updates the UI according to the purchase request notification result. -(void)handlePurchaseNotification:(NSNotification *)notification { StoreObserver *purchasesNotification = (StoreObserver *)notification.object; PCSPurchaseStatus status = (PCSPurchaseStatus)purchasesNotification.status; switch (status) { case PCSNoRestorablePurchases: case PCSPurchaseFailed: case PCSRestoreFailed: [self alertWithTitle:PCSMessagesPurchaseStatus message:purchasesNotification.message]; break; // Switch to the Purchases view when receiving a successful restore notification. case PCSRestoreSucceeded: [self handleRestoredSucceededTransaction]; break; default: break; } } #pragma mark - Handle Restored Transactions /// Handles succesful restored transactions. Switches to the Purchases view. -(void)handleRestoredSucceededTransaction { utility.restoreWasCalled = YES; // Refresh the Purchases view with the restored purchases. // [self switchToViewController:ParentViewControllerSegmentPurchases]; // [self.purchases reloadWithData:self.utility.dataSourceForPurchasesUI]; // self.segmentedControl.selectedSegmentIndex = ParentViewControllerSegmentPurchases; } @end