How to run code on app for iOS only one time (even the application will be removed and reinstalled)?
For example, we need to make a gift for all new users. But if you use it you have to buy it again using in-app purchase.
How to run code on app for iOS only one time (even the application will be removed and reinstalled)?
For example, we need to make a gift for all new users. But if you use it you have to buy it again using in-app purchase.
You can store a flag in the keychain. That persists on the device even after the app has been deleted. You could also use an iCloud key-value setting which could be used for control over multiple devices on the same account.
here's an example of adding info (in this case whether or not the user had purchased something) to the keychain and getting it back out.
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];
}
}
You will have to make a web service for it i dont think any other way out. Save device token.