簡體   English   中英

根據應用內購買顯示內容

[英]Displaying content based on in app purchase

我已經將應用購買代碼編碼到我的應用中,但是我想根據用戶是否購買了商品,使用其他視圖控制器來顯示內容

在我的ViewController中,我正在使用此簡單的IF語句,但它似乎沒有用。 我還有什么需要做的嗎?

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
 if(![userDefaults boolForKey:@"productIdentifier"])
    {

        label.text = @"Purchased";

    }

    else {

        label.text = @"Not Purchased";



    }

我還有一個名為IAPHelper.m的文件,其中對應用程序內購買進行了編碼,其中有一個用於NSUSerDefaults的方法,如下所示:

- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {


    [_purchasedProductIdentifiers addObject:productIdentifier];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];



}

編輯

如果有幫助,我的應用程序中有一個UITableView可以根據用戶是否購買了商品來顯示內容。 控制此的語句是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    SKProduct * product = (SKProduct *) [_products objectAtIndex:indexPath.row];
    cell.textLabel.text = product.localizedTitle;

    [_priceFormatter setLocale:product.priceLocale];
    cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price];

    if ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {

        labelPrice.text = @"Purchased";




    } else {



        labelPrice.text = @"Not Purchased";

    }

    return cell;
}

@"productIdentifier"是第一段代碼中的字符串文字,但是productIdentifier是第二段代碼中的變量。

好吧…… 編輯#3

它在您的表中工作的原因是因為您正在使用以下productPurchased訪問當前IAPHelper中的productPurchased方法:

[[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier];

同樣,使用:

// Index should equal 0 if there's only one product…
SKProduct * product = (SKProduct *) [_products objectAtIndex:index];

if([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {

    label.text = @"Purchased";

} else {

    label.text = @"Not Purchased";

}

如果要根據用戶是否購買了IAP轉到其他視圖,則以下代碼可用於基於Storyboard的應用程序。 (您從未指定,所以我假設這就是您所使用的)

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

//self.productIdentifier needs to be defined with the productIdentifier to test.
 if([userDefaults boolForKey:self.productIdentifier])// **Removed** the !. With a ! this would return that the user hasn't purchased it, when they did.
{

    //User has purchased
[self performSegueWithIdentifier:@"purchasedView" sender:self];//You must set up a segue in the storyboard file first. (Control drag from files owner (orange box) onto the desired view. Then click on the new arrow. Then on the attributes inspector, name your segue id to "purchasedView"

}

else {

    //User has not purchased IAP.
//Optionally stay on this view.
}

編輯新代碼

int indexNumberOfProduct = 1;//Change to whatever you need
SKProduct * product = (SKProduct *) [_products objectAtIndex:indexNumberOfProduct];


if ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {

    labelPrice.text = @"Purchased";




} else {



    labelPrice.text = @"Not Purchased";

}

如果可以幫助您,請將此答案標記為正確。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM