Instance Property

applicationUsername

A string that associates the payment transaction with a user on your own service.

Declaration

@property(nonatomic, copy, readwrite) NSString *applicationUsername;

Discussion

You can use this property to detect some forms of fraudulent activity, typically multiple transactions from different iTunes Store accounts. For example, if you have an online game where each user creates an account to save gameplay data, it's unusual for many different iTunes Store accounts to make purchases on behalf of the same user account on your system. The App Store can't automatically detect that the transactions are related. Setting this property associates the purchases with each other.

Create a string that uniquely identifies the user's account on your service. For example, you can use a one-way hash of the user’s account name. Don't provide personally identifiable information or any data whose disclosure would otherwise be detrimental to the user.

The following code demonstrates one way to generate this hashed data:

// Custom method to calculate the SHA-256 hash using Common Crypto.

-(NSString *)hashedValueForAccountName:(NSString *)userAccountName
{
    const int HASH_SIZE = 32;
    unsigned char hashedChars[HASH_SIZE];
    const char *accountName = [userAccountName UTF8String];
    size_t accountNameLen = strlen(accountName);

    // Confirm that the user name is short enough
    // to be recast when calling the hash function.

    if (accountNameLen > UINT32_MAX) {
        NSLog(@"Account name too long to hash: %@", userAccountName);
        return nil;

    }

    CC_SHA256(accountName, (CC_LONG)accountNameLen, hashedChars);

    // Convert the array of bytes into a string showing its hex representation.
    NSMutableString *userAccountHash = [[NSMutableString alloc] init];
    for (int i = 0; i < HASH_SIZE; i++) {
        // Add a dash every four bytes, for readability.
        if (i != 0 && i%4 == 0) {
            [userAccountHash appendString:@"-"];
        }
        [userAccountHash appendFormat:@"%02x", hashedChars[i]];
    }
    return userAccountHash;
}

Then, set this property before you submit the payment transaction to the payment queue:

SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
payment.applicationUsername = [self hashedValueForAccountName:@"userNameOnYourServer"];

[[SKPaymentQueue defaultQueue] addPayment:payment];

See Also

Getting and Setting Attributes

productIdentifier

A string that identifies a product that can be purchased from within your app.

quantity

The number of items the user wants to purchase.

requestData

Reserved for future use.