ios - 应用内购买后,viewWillAppear会多次加载购买的商品
<p>我在应用内购买方面遇到了麻烦。离开MasterViewController(列出并购买IAP物品的地方)后,我来到ViewController,它在viewDidLoad中检查购买的物品,但是我也有viewWillAppear检查购买的物品并在加载ViewController时立即解锁那些购买的物品。<br><br>问题是,每次重新加载或刷新ViewController时,都将其离开,然后再返回,则viewWillAppear会检查购买的物品并再次解锁物品,在我的情况下,购买的物品来自javascript文件,而这恰好会加载JS多次,使具有相同IAP项的应用一遍又一遍。<br><br>例如,如果您购买了“书签”,则每次重新加载ViewController时,都会一次又一次地获得另一个“书签”工具。<br><br>如何获得IAP仅加载一次的信息?<br><br>我的viewWillAppear看起来像这样:<br><pre><code>- (void)viewWillAppear:(BOOL)animated {
// Check To See If The Uniques IAP Has Been Paid For
if (![[ objectForKey:SHOW_UNIQUES_KEY] isEqualToString: @"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = URLForResource:@"Uniques" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
; }
NSLog(@"Shows uniques?: %@", [ objectForKey:SHOW_UNIQUES_KEY]);
// Check To See If The PL Numbers IAP Has Been Paid For
if (![[ objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: @"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = URLForResource:@"PortalLevel" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
; }
NSLog(@"Shows portal levels?: %@", [ objectForKey:SHOW_PORTAL_LEVEL_KEY]);
// Check To See If The PN IAP Has Been Paid For
if (![[ objectForKey:SHOW_PORTALS_KEY] isEqualToString: @"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = URLForResource:@"PortalNames" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
; }
NSLog(@"Shows portal names?: %@", [ objectForKey:SHOW_PORTALS_KEY]);
// Check To See If The Bookmarks IAP Has Been Paid For
if (![[ objectForKey:SHOW_BOOKMARKS_KEY] isEqualToString: @"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = URLForResource:@"bookmarks" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
; }
NSLog(@"Shows bookmarks?: %@", [ objectForKey:SHOW_BOOKMARKS_KEY]);
// Check To See If The Poke A Bowl Ad Has Been Paid For
if (![[ objectForKey:SHOW_ADS_KEY] isEqualToString: @"No"]){ // NEW CODE
// Code to show ads
_PokeABowlAd.hidden = NO;
} else {
_PokeABowlAd.hidden = YES;
}
NSLog(@"Shows ads?: %@", [ objectForKey:SHOW_ADS_KEY]);
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p>我认为您应该做的是使每个人最多被调用一次。您应该使用NSUserDefaults完成此操作。做这样的事情:<br><br>将其放在您的AppDelegate.m中:<br><pre><code>//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"
</code></pre><br>和<br><pre><code>// In applicationDidFinishLaunchingWithOptions, should be the first method listed
[ setObject: @"No" forKey: IAP_FIRST_USED];
...
[ setObject: @"No" forKey: IAP_LAST_USED];
[ synchronize];
</code></pre><br>然后在VC.m中将viewWillAppear修改为如下所示(这是第一个IAP应该看起来像的示例):<br><pre><code>// Check To See If The PL Numbers IAP Has Been Paid For
if (![[ objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: @"YES"]){ // NEW CODE
// NEW CODE BEGINS HERE
if ([[ objectForKey:IAP_FIRST_USED] isEqualToString: @"No"]){
[ setObject: @"Yes" forKey: IAP_FIRST_USED];
[ synchronize];
// Code to show bookmarks
// End of code to show bookmarks
// NEW CODE ENDS HERE
}
} else {
NSString *jsString = URLForResource:@"PortalLevel" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
;
}
</code></pre><br>还记得在VC.m中包含define语句</p>
<p style="font-size: 20px;">关于ios - 应用内购买后,viewWillAppear会多次加载购买的商品,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31998793/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31998793/
</a>
</p>
页:
[1]