如何在仅适用于iOS的应用上运行代码一次时间(即使应用程序将被删除并重新安装)?
例如,我们需要为所有新用户赠送礼物。但如果您使用它,则必须使用应用内购买再次购买。
答案 0 :(得分:3)
您可以在钥匙串中存储标志。即使应用程序被删除后,该设备仍然存在。您还可以使用iCloud键值设置,该设置可用于控制同一帐户中的多个设备。
这是一个添加信息(在这种情况下,用户是否购买了东西)到钥匙串并将其取回的示例。
static NSString *kIAPKeyChainDescription = @"MyAppPurchaseKey";
static NSString *kPurchaseTag = @"YES";
+ (void)recordTransaction:(NSString *)productId
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSData *tag = [kPurchaseTag dataUsingEncoding:NSUTF8StringEncoding];
[dict setObject: (id) kSecClassGenericPassword forKey: (id) kSecClass];
[dict setObject: kIAPKeyChainDescription forKey:(id) kSecAttrDescription];
[dict setObject: productId forKey: (id) kSecAttrService];
[dict setObject: tag forKey: (id) kSecValueData];
OSStatus result = SecItemAdd ((CFDictionaryRef) dict, NULL);
if (result != 0)
NSLog(@"SecItemAdd error code is %ld",result);
}
+ (BOOL)isPurchased:(NSString *)productId
{
NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject: (id) kSecClassGenericPassword forKey: (id) kSecClass];
[query setObject: productId forKey: (id) kSecAttrService];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
NSData* upgradeItemData = nil;
OSStatus result = SecItemCopyMatching ( (CFDictionaryRef) query, (CFTypeRef*) &upgradeItemData );
if (result != errSecSuccess && result != errSecItemNotFound){
NSLog(@"SecItemCopyMatching errcode is %ld",result);
return NO;
}
if ( !upgradeItemData || result == errSecItemNotFound)
{
return NO;
}
else
{
NSString* s = [[[NSString alloc]
initWithData: upgradeItemData
encoding: NSUTF8StringEncoding] autorelease];
[upgradeItemData release];
return [s boolValue];
}
}
答案 1 :(得分:0)
你必须为它做一个网络服务我不认为任何其他方式。保存设备令牌。