saturngod / IAPHelper

No more maintenance for this repo. Please use the https://github.com/bizz84/SwiftyStoreKit
MIT License
1.55k stars 280 forks source link

Can't buy: Fail Cannot connect to iTunes Store. Transaction error: Cannot connect to iTunes Store 0 ? #14

Closed gclsoft closed 10 years ago

gclsoft commented 10 years ago
2014-06-11 14:29:11.897 app[14096:60b] Transaction error: Cannot connect to iTunes Store 0
2014-06-11 14:29:11.898 app[14096:60b] Fail Cannot connect to iTunes Store

In viewDidLoad:

    if(![IAPShare sharedHelper].iap) {
        NSSet* dataSet = [[NSSet alloc] initWithObjects:SUB_PRODUCT_REMOVEAD,SUB_PRODUCT_BUY200, nil];

        [IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:dataSet];
    }

    [IAPShare sharedHelper].iap.production = NO;

Buy:

//IAP
- (IBAction)pressBuy:(id)sender{
    [[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse* response)
     {
         if(response > 0 ) {
             SKProduct* product =[[IAPShare sharedHelper].iap.products objectAtIndex:0];

             [[IAPShare sharedHelper].iap buyProduct:product
                                        onCompletion:^(SKPaymentTransaction* trans){

                                            if(trans.error)
                                            {
                                                NSLog(@"Fail %@",[trans.error localizedDescription]);
                                            }
                                            else if(trans.transactionState == SKPaymentTransactionStatePurchased) {

                                                [[IAPShare sharedHelper].iap checkReceipt:trans.transactionReceipt AndSharedSecret:@"your sharesecret" onCompletion:^(NSString *response, NSError *error) {

                                                    //Convert JSON String to NSDictionary
                                                    NSDictionary* rec = [IAPShare toJSON:response];

                                                    if([rec[@"status"] integerValue]==0)
                                                    {
                                                        NSString *productIdentifier = trans.payment.productIdentifier;
                                                        [[IAPShare sharedHelper].iap provideContent:productIdentifier];
                                                        NSLog(@"SUCCESS %@",response);
                                                        NSLog(@"Pruchases %@",[IAPShare sharedHelper].iap.purchasedProducts);
                                                    }
                                                    else {
                                                        NSLog(@"Fail");
                                                    }
                                                }];
                                            }
                                            else if(trans.transactionState == SKPaymentTransactionStateFailed) {
                                                NSLog(@"Fail");
                                            }
                                        }];//end of buy product
         }
     }];
}

Why can't buy when I press buy button? Is there any IAPHelp project example to download?

saturngod commented 10 years ago

What username are you using for testing ?

You need to use test account from iTunes Connect.

Logout the current account from Phone Setting > iTunes & App Store > Click on App ID > Singout

After singout , please try again.

gclsoft commented 10 years ago

Thanks so much! OK now! Can I ask one more question? SKProduct* product =[[IAPShare sharedHelper].iap.products objectAtIndex:0]; I'm not sure the index 0 or 1 which one is I want, can I just give a product ID to get the product?

saturngod commented 10 years ago

You can use like that

NSString *productIdentifier = @"myproduct.name.identifier";
 NSArray *result = [[IAPShare sharedHelper].iap.products filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"productIdentifier == %@", productIdentifier]];
if(result.count > 0 )
{
SKProduct  *product = result[0];
}
gclsoft commented 10 years ago

Thanks! It's great! Works well!