InAppPurchase 从另一个 nsobject

发布于 2025-01-07 09:38:08 字数 2291 浏览 1 评论 0原文

我建立了一个应用程序内购买并且运行良好。
然后我创建了一个新的 Objective C 类,并将应用内购买的所有编码放入新类中。
好的,现在我想从我的主视图控制器调用应用程序内购买。这怎么可能?感谢

viewcontroller.m

- (void)viewDidLoad
{
    InAppPurchaseManager *IAPmanager = [[InAppPurchaseManager alloc]init];
    if([SKPaymentQueue canMakePayments])
        {
            //sets payment observer to class.
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

            //initializes products array.
            products = [[NSMutableArray alloc]init];
            [IAPmanager loadStore];

        }
            else
            {
                NSLog(@"failed");
            }

}  

InAppPurchaseManager.m

-(void)loadStore
{
    //restarts any purchases if they were interupted last time the was open
    [[SKPaymentQueue defaultQueue]addTransactionObserver:self];
    //get the product description
    ViewController *viewcontroller = [[ViewController alloc]init];
    [viewcontroller requestProductData];
}
//request product
-(void)requestProductData
{
    NSSet *productIdentifiers = [NSSet setWithObjects:@"com.company.myApp.App1",nil ];
    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];
    NSLog(@"product request start");

}  

它在此之前停止运行

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    myproducts = response.products;
    for(proUpgradeProduct in myproducts)
    {
        //debug log.
        NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
        NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
        NSLog(@"Product price: %@" , proUpgradeProduct.price);
        NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
    }
    // invalid purchase
    for (NSString *invalidProductId in response.invalidProductIdentifiers)
    {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];

    NSLog(@"receive respond");
}

i built an in app purchase and it work fine.
then i created a new objective c class and put all the coding for in app purchase in the new class.
ok, now i want to call the in app purchase from my main view controller. how is it possible? thanks

viewcontroller.m

- (void)viewDidLoad
{
    InAppPurchaseManager *IAPmanager = [[InAppPurchaseManager alloc]init];
    if([SKPaymentQueue canMakePayments])
        {
            //sets payment observer to class.
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

            //initializes products array.
            products = [[NSMutableArray alloc]init];
            [IAPmanager loadStore];

        }
            else
            {
                NSLog(@"failed");
            }

}  

InAppPurchaseManager.m

-(void)loadStore
{
    //restarts any purchases if they were interupted last time the was open
    [[SKPaymentQueue defaultQueue]addTransactionObserver:self];
    //get the product description
    ViewController *viewcontroller = [[ViewController alloc]init];
    [viewcontroller requestProductData];
}
//request product
-(void)requestProductData
{
    NSSet *productIdentifiers = [NSSet setWithObjects:@"com.company.myApp.App1",nil ];
    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];
    NSLog(@"product request start");

}  

it stopped running before this

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    myproducts = response.products;
    for(proUpgradeProduct in myproducts)
    {
        //debug log.
        NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
        NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
        NSLog(@"Product price: %@" , proUpgradeProduct.price);
        NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
    }
    // invalid purchase
    for (NSString *invalidProductId in response.invalidProductIdentifiers)
    {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];

    NSLog(@"receive respond");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-01-14 09:38:08

从您想要的任何类中调用此方法

- (IBAction) buy:(id)sender
{

InAppPurchaseManager *inapp = [[InAppPurchaseManager alloc] init];

[应用内加载存储];

if ([inapp canMakePurchases]) {
    [inapp purchaseProUpgrade];

}
}

Call This method from any class you want-

-(IBAction) purchase:(id)sender
{

InAppPurchaseManager *inapp = [[InAppPurchaseManager alloc] init];

[inapp loadStore];

if ([inapp canMakePurchases]) {
    [inapp purchaseProUpgrade];

}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文