在将 Stripe API 与我的 Swift 2.0 项目集成时,我试图摆脱所有已弃用的对象。我已经替换了显而易见的代码,但我需要帮助的 3 行是:
ABMultiValueRef addressValues = ABRecordCopyValue(payment.billingAddress, kABPersonAddressProperty);
和
if (ABMultiValueGetCount(addressValues) > 0) {
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressValues, 0);
}
警告是(如下):
'ABMultiValueRef' 已弃用:首先在 iOS 9.0 中弃用 - 使用 CNLabeledValue 的 NSArray
'ABRecordCopyValue' 已弃用:首先在 iOS 9.0 中弃用 - 使用 CN 对象的属性
'ABMultiValueGetCount' 已弃用:首先在 iOS 9.0 中弃用 - 使用带有标签值属性的 NSArray.count
'ABMultiValueCopyValueAtIndex' 已弃用:首先在 iOS 9.0 中弃用 - 将 [[NSArray objectAtIndex:] value] 与标签值属性一起使用
完整代码如下:
#import <AddressBook/AddressBook.h>
#import "STPAPIClient+ApplePay.h"
#import "PKPayment+Stripe.h"
#import "STPAPIClient+Private.h"
@implementation STPAPIClient (ApplePay)
- (void)createTokenWithPayment:(PKPayment *)payment completion:(STPTokenCompletionBlock)completion {
[self createTokenWithData:[self.class formEncodedDataForPayment:payment] completion:completion];
}
+ (NSData *)formEncodedDataForPayment:(PKPayment *)payment {
NSCAssert(payment != nil, @"Cannot create a token with a nil payment.");
NSMutableCharacterSet *set = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
[set removeCharactersInString:@"+="];
NSString *paymentString =
[[[NSString alloc] initWithData:payment.token.paymentData encoding:NSUTF8StringEncoding] stringByAddingPercentEncodingWithAllowedCharacters:set];
__block NSString *payloadString = [@"pk_token=" stringByAppendingString:paymentString];
if (payment.billingContact) {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
NSString *firstName = CNContactGivenNameKey;
NSString *lastName = CNContactFamilyNameKey;
if (firstName.length && lastName.length) {
params[@"name"] = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
ABMultiValueRef addressValues = ABRecordCopyValue(payment.billingAddress, kABPersonAddressProperty);
if (addressValues != NULL) {
if (ABMultiValueGetCount(addressValues) > 0) {
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressValues, 0);
NSString *line1 = CFDictionaryGetValue(dict, CNPostalAddressStreetKey);
if (line1) {
params[@"address_line1"] = line1;
}
NSString *city = CFDictionaryGetValue(dict, CNPostalAddressCityKey);
if (city) {
params[@"address_city"] = city;
}
NSString *state = CFDictionaryGetValue(dict, CNPostalAddressStateKey);
if (state) {
params[@"address_state"] = state;
}
NSString *zip = CFDictionaryGetValue(dict, CNPostalAddressPostalCodeKey);
if (zip) {
params[@"address_zip"] = zip;
}
NSString *country = CFDictionaryGetValue(dict, CNPostalAddressCountryKey);
if (country) {
params[@"address_country"] = country;
}
CFRelease(dict);
[params enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, __unused BOOL *stop) {
NSString *param = [NSString stringWithFormat:@"&card[%@]=%@", key, [obj stringByAddingPercentEncodingWithAllowedCharacters:set]];
payloadString = [payloadString stringByAppendingString:param];
}];
}
CFRelease(addressValues);
}
}
if (payment.token.paymentMethod) {
NSString *param = [NSString stringWithFormat:@"&pk_token_instrument_name=%@", payment.token.paymentMethod];
payloadString = [payloadString stringByAppendingString:param];
}
if (payment.token.paymentMethod) {
NSString *param = [NSString stringWithFormat:@"&pk_token_payment_network=%@", payment.token.paymentMethod];
payloadString = [payloadString stringByAppendingString:param];
}
if (payment.token.transactionIdentifier) {
NSString *transactionIdentifier = payment.token.transactionIdentifier;
if ([payment stp_isSimulated]) {
transactionIdentifier = [PKPayment stp_testTransactionIdentifier];
}
NSString *param = [NSString stringWithFormat:@"&pk_token_transaction_id=%@", transactionIdentifier];
payloadString = [payloadString stringByAppendingString:param];
}
return [payloadString dataUsingEncoding:NSUTF8StringEncoding];
}
@end
void linkSTPAPIClientApplePayCategory(void){}