菜鸟教程小白 发表于 2022-12-13 09:51:30

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: @&#34;YES&#34;]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = URLForResource:@&#34;Uniques&#34; withExtension:@&#34;js&#34;] encoding:NSUTF8StringEncoding error:nil];
    ;    }
NSLog(@&#34;Shows uniques?: %@&#34;, [ objectForKey:SHOW_UNIQUES_KEY]);

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

} else {
    NSString *jsString = URLForResource:@&#34;PortalLevel&#34; withExtension:@&#34;js&#34;] encoding:NSUTF8StringEncoding error:nil];
    ;    }
NSLog(@&#34;Shows portal levels?: %@&#34;, [ objectForKey:SHOW_PORTAL_LEVEL_KEY]);

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

} else {
    NSString *jsString = URLForResource:@&#34;PortalNames&#34; withExtension:@&#34;js&#34;] encoding:NSUTF8StringEncoding error:nil];
    ;    }
NSLog(@&#34;Shows portal names?: %@&#34;, [ objectForKey:SHOW_PORTALS_KEY]);

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

} else {
    NSString *jsString = URLForResource:@&#34;bookmarks&#34; withExtension:@&#34;js&#34;] encoding:NSUTF8StringEncoding error:nil];
    ;    }
NSLog(@&#34;Shows bookmarks?: %@&#34;, [ objectForKey:SHOW_BOOKMARKS_KEY]);

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

} else {
    _PokeABowlAd.hidden = YES;
}
NSLog(@&#34;Shows ads?: %@&#34;, [ 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 @&#34;IAP 1 Used&#34;
...
#define IAP_LAST_USED @&#34;IAP X Used&#34;
</code></pre><br>和<br><pre><code>// In applicationDidFinishLaunchingWithOptions, should be the first method listed

[ setObject: @&#34;No&#34; forKey: IAP_FIRST_USED];
...
[ setObject: @&#34;No&#34; 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: @&#34;YES&#34;]){ // NEW CODE


   // NEW CODE BEGINS HERE

   if ([[ objectForKey:IAP_FIRST_USED] isEqualToString: @&#34;No&#34;]){
   [ setObject: @&#34;Yes&#34; forKey: IAP_FIRST_USED];
   [ synchronize];
   // Code to show bookmarks



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

   }
} else {
    NSString *jsString = URLForResource:@&#34;PortalLevel&#34; withExtension:@&#34;js&#34;] 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]
查看完整版本: ios - 应用内购买后,viewWillAppear会多次加载购买的商品