• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 应用内购买后,viewWillAppear会多次加载购买的商品

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:51:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在应用内购买方面遇到了麻烦。离开MasterViewController(列出并购买IAP物品的地方)后,我来到ViewController,它在viewDidLoad中检查购买的物品,但是我也有viewWillAppear检查购买的物品并在加载ViewController时立即解锁那些购买的物品。

问题是,每次重新加载或刷新ViewController时,都将其离开,然后再返回,则viewWillAppear会检查购买的物品并再次解锁物品,在我的情况下,购买的物品来自javascript文件,而这恰好会加载JS多次,使具有相同IAP项的应用一遍又一遍。

例如,如果您购买了“书签”,则每次重新加载ViewController时,都会一次又一次地获得另一个“书签”工具。

如何获得IAP仅加载一次的信息?

我的viewWillAppear看起来像这样:

- (void)viewWillAppearBOOL)animated {
// Check To See If The Uniques IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_UNIQUES_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource"Uniques" withExtension"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows uniques?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_UNIQUES_KEY]);

// Check To See If The PL Numbers IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource"ortalLevel" withExtension"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows portal levels?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY]);

// Check To See If The PN IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTALS_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource"ortalNames" withExtension"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows portal names?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTALS_KEY]);

// Check To See If The Bookmarks IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_BOOKMARKS_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource"bookmarks" withExtension"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows bookmarks?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_BOOKMARKS_KEY]);

// Check To See If The Poke A Bowl Ad Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_ADS_KEY] isEqualToString: @"No"]){ // NEW CODE
    // Code to show ads
    _PokeABowlAd.hidden = NO;

} else {
    _PokeABowlAd.hidden = YES;
}
NSLog(@"Shows ads?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_ADS_KEY]);
}



Best Answer-推荐答案


我认为您应该做的是使每个人最多被调用一次。您应该使用NSUserDefaults完成此操作。做这样的事情:

将其放在您的AppDelegate.m中:

//One new key for each IAP - put at the top of the AppDelegate.m and VC.m
#define IAP_FIRST_USED @"IAP 1 Used"
...
#define IAP_LAST_USED @"IAP X Used"


// In applicationDidFinishLaunchingWithOptions, should be the first method listed

[[NSUserDefaults standardUserDefaults] setObject: @"No" forKey: IAP_FIRST_USED];
...
[[NSUserDefaults standardUserDefaults] setObject: @"No" forKey: IAP_LAST_USED];
[[NSUserDefaults standardUserDefaults] synchronize];

然后在VC.m中将viewWillAppear修改为如下所示(这是第一个IAP应该看起来像的示例):
// Check To See If The PL Numbers IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: @"YES"]){ // NEW CODE


   // NEW CODE BEGINS HERE

   if ([[[NSUserDefaults standardUserDefaults] objectForKey:IAP_FIRST_USED] isEqualToString: @"No"]){
   [[NSUserDefaults standardUserDefaults] setObject: @"Yes" forKey: IAP_FIRST_USED];
   [[NSUserDefaults standardUserDefaults] synchronize];  
   // Code to show bookmarks



   // End of code to show bookmarks
   // NEW CODE ENDS HERE

   }
} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource"ortalLevel" withExtension"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    
}

还记得在VC.m中包含define语句

关于ios - 应用内购买后,viewWillAppear会多次加载购买的商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998793/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap