1

如何仅在 iOS 应用程序上运行一次代码甚至应用程序将被删除并重新安装)?

例如,我们需要为所有新用户制作礼物。但是,如果您使用它,则必须使用应用内购买再次购买。

4

2 回答 2

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];
        }
    }
于 2013-02-22T14:59:22.143 回答
0

你将不得不为它创建一个网络服务,我认为没有其他出路。保存设备令牌。

于 2013-02-22T14:57:10.960 回答