關於iOS12版手機內購充值無回調的一則代碼級錯誤

在維護老項目時,發現iOS12版手機上無法充值成功,經過測試發現,僅有iOS12版及以上版本手機上無法充值成功,

卡在了向蘋果發送產品ID後等待回調時,無回調!

因爲iOS11版沒有問題,排除充值流程上的問題了,經過和能夠在iOS12版上正常充值的項目比對發現:

重點在下面的代碼上!


C++ TO Object-c 的 PopCapacitymanage.mm 文件上 關於 單例類的靜態方法

PopCapacityManage* PopCapacityManage::sharedManage()
{
    if (!_sharedManage) {
        _sharedManage = new PopCapacityManage; 
    }
    return _sharedManage;
}

void PopCapacityManage::IOSPAYView(char*iospayID){
    NSString*payIDStr=[NSString stringWithUTF8String:iospayID];
    NSLog(@"%@",payIDStr);
    [[IOSPAYViewController sharePayViewC] buy:payIDStr];//將 產品 傳遞給 支付類
}


下面是 內購充值支付類:IOSPAYViewController.mm文件

static IOSPAYViewController *sharePayView = nil;

+(IOSPAYViewController *)sharePayViewC{
    @synchronized(self){
        if(sharePayView == nil){
            sharePayView = [[[self alloc] init] autorelease];
        }
    }
    return sharePayView;
}
-(void)buy:(NSString*)type
{
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    buyType = type;
    if ([SKPaymentQueue canMakePayments]) {
        [self RequestProductData];//        CCLOG(@"允許程序內付費購買");
        
        _TS_alerView =  [[UIAlertView alloc] initWithTitle:nil
                                                   message:@"兌換正在處理,請勿關閉……"
                                                  delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(關閉)",nil) otherButtonTitles:nil];
        
        [_TS_alerView show];
        [_TS_alerView release];
    }
    else
    {   // CCLOG(@"不允許程序內付費購買");
        UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert"
                                                            message:@"沒允許應用程序內購買"
                                                           delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(關閉)",nil) otherButtonTitles:nil];
        
        [alerView show];
        [alerView release];
        
    }
}


-(void)RequestProductData
{
    NSArray* product = [[NSArray alloc] initWithObjects:buyType,nil];
    NSSet* nsset = [NSSet setWithArray:product];
    SKProductsRequest* request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
    request.delegate=self;
    [request start];
    [product release];
}
//<SKProductsRequestDelegate> 請求協議
//收到的產品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

}

對的,就是上面這個方法

productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

沒有被回調!


看一下這兩個類的 單例創建方法,是有問題的!

下面是修復後的代碼

PopCapacityManage.mm文件

static PopCapacityManage* _sharedManage;
PopCapacityManage* PopCapacityManage::sharedManage()
{
    if (!_sharedManage) {
        _sharedManage = new (std::nothrow) PopCapacityManage();
    }
    return _sharedManage;
}
void PopCapacityManage::IOSPAYView(const char* iospayID){
    NSString* payIDStr = [NSString stringWithUTF8String:iospayID];
    [[IOSPAYViewController getInstance] buy:payIDStr];
}


IOSPAYViewController.mm

static IOSPAYViewController* gIapTools = nil;
+(id) getInstance
{
    @synchronized ([IOSPAYViewController class])
    {
        if (!gIapTools)
        {
            gIapTools = [[IOSPAYViewController alloc] initIAP];
        }
        return gIapTools;
    }
}

-(id)initIAP
{
    if (self = [super init])
    {
        [[SKPaymentQueue defaultQueue]addTransactionObserver:self];
        productID = nil;
    }
    return self;
}

-(void)releaseIAP
{
    if (gIapTools)
    {
        [gIapTools release];
        gIapTools = nil;
    }
}


-(void)buy:(NSString*)type
{
    //[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    productID = type;
    
    if ([SKPaymentQueue canMakePayments]) {
        [self requestProductData];// CCLOG(@"允許程序內付費購買");
        _TS_alerView =  [[UIAlertView alloc] initWithTitle:nil
                                                   message:@"兌換正在處理,請勿關閉……"
                                                  delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(關閉)",nil) otherButtonTitles:nil];
        
        [_TS_alerView show];
        [_TS_alerView release];
    }
    else
    {   // CCLOG(@"不允許程序內付費購買");
        UIAlertView* alerView = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                            message:@"沒允許應用程序內購買"
                                                           delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(關閉)",nil) otherButtonTitles:nil];
        
        [alerView show];
        [alerView release];
    }
}

-(void)requestProductData
{
    NSArray* product = [[NSArray alloc] initWithObjects:productID, nil];
    NSSet* set = [NSSet setWithArray:product];
    SKProductsRequest* request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
    //SKProductsRequest* request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:productID]];
    request.delegate = self;
    [request start];
    [product release];
}
-(void)requestDidFinish:(SKRequest *)request
{
    NSLog(@"--反饋信息結束------");
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"--彈出錯誤信息------");
}

//<SKProductsRequestDelegate> 請求協議
//收到的產品信息
//#pragma mark- SKProductsRequest delegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSLog(@"-----------收到產品反饋信息--------------");
    NSArray *myProduct = response.products;
    NSLog(@"產品Product ID:%@",response.invalidProductIdentifiers);
    NSLog(@"產品付費數量: %lu", [myProduct count]);
    // populate UI
    for(SKProduct *product in myProduct){
        NSLog(@"product info");
        NSLog(@"SKProduct 描述信息%@", [product description]);
        NSLog(@"產品標題 %@" , product.localizedTitle);
        NSLog(@"產品描述信息: %@" , product.localizedDescription);
        NSLog(@"價格: %@" , product.price);
        NSLog(@"Product id: %@" , product.productIdentifier);
    }
    SKPayment* payment = nil;//SKProductsRequest
    payment = [SKPayment paymentWithProductIdentifier:productID];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [request autorelease];
    
}


主要是看 單例的創建 部分, 就代碼在 iOS8到iOS11 可以使用,但不推薦! 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章