您应用内ID的应用内购买权利

时间:2014-08-18 06:43:26

标签: ios xcode in-app-purchase

我真的很困惑,我不知道如何在我现有的应用中添加应用内购买。在“功能”中我选择应用内购买时,它只显示错误add the in app purchase entitlement to your app id。我在开发中心添加了应用ID,并在itunesconnect中添加了应用内购买。我的捆绑ID为sk.freetech.zatracenacestina.intro,我在应用内设置sk.freetech.zatracenacestina.intro.package,但我不知道在哪里可以填写它?

谢谢

4 个答案:

答案 0 :(得分:3)

你可以按照这些教程 1. http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial 2. http://www.tutorialspoint.com/ios/ios_in_app_purchase.htm 3. http://code4app.net/ios/In-App-Purchase/4fc85be56803fa4a49000000

创建一个类名称IAPHelper子类NSObject

#import <StoreKit/StoreKit.h>


UIKIT_EXTERN NSString *const IAPHelperProductPurchasedNotification;

typedef void (^RequestProductsCompletionHandler)(BOOL success, NSArray * products);
@protocol inAppPurchaseDelegate <NSObject>

@optional
-(void)transactionSucsess:(NSString *)transactionId;
-(void)transactionOnRestore:(NSString *)transactionId;
-(void)transactionOnFail;
@end
@interface IAPHelper : NSObject

@property(nonatomic,readwrite)id<inAppPurchaseDelegate> delegate;

- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers;
- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler;
- (void)buyProduct:(SKProduct *)product;
- (BOOL)productPurchased:(NSString *)productIdentifier;
- (void)restoreCompletedTransactions;



@end

在IAPHelper.m

import&#34; IAPHelper.h&#34;

#import <StoreKit/StoreKit.h>

NSString *const IAPHelperProductPurchasedNotification = @"IAPHelperProductPurchasedNotification";


@interface IAPHelper () <SKProductsRequestDelegate, SKPaymentTransactionObserver>
@end


@implementation IAPHelper {
    SKProductsRequest * _productsRequest;
    RequestProductsCompletionHandler _completionHandler;

    NSSet * _productIdentifiers;
    NSMutableSet * _purchasedProductIdentifiers;
}


- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers {

    if ((self = [super init])) {

        // Store product identifiers
        _productIdentifiers = productIdentifiers;

        // Check for previously purchased products
        _purchasedProductIdentifiers = [NSMutableSet set];
        for (NSString * productIdentifier in _productIdentifiers) {
            BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
            if (productPurchased) {
                [_purchasedProductIdentifiers addObject:productIdentifier];
                NSLog(@"Previously purchased: %@", productIdentifier);
            } else {
                NSLog(@"Not purchased: %@", productIdentifier);
            }
        }

        // Add self as transaction observer
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    }
    return self;

}

- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler {

    _completionHandler = [completionHandler copy];

    _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers];
    _productsRequest.delegate = self;
    [_productsRequest start];
}

- (BOOL)productPurchased:(NSString *)productIdentifier {
    return [_purchasedProductIdentifiers containsObject:productIdentifier];
}

- (void)buyProduct:(SKProduct *)product {

    NSLog(@"Buying %@...", product.productIdentifier);

    SKPayment * payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];

}

#pragma mark - SKProductsRequestDelegate

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

    NSLog(@"Loaded list of products...");
    _productsRequest = nil;

    NSArray * skProducts = response.products;
    for (SKProduct * skProduct in skProducts) {
        NSLog(@"Found product: %@ %@ %0.2f",
              skProduct.productIdentifier,
              skProduct.localizedTitle,
              skProduct.price.floatValue);
    }

    _completionHandler(YES, skProducts);
    _completionHandler = nil;

}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {

    NSLog(@"Failed to load list of products.");
    _productsRequest = nil;

    _completionHandler(NO, nil);
    _completionHandler = nil;

}

#pragma mark SKPaymentTransactionOBserver

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction * transaction in transactions) {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    };
}

- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    NSLog(@"completeTransaction...");

    [self provideContentForProductIdentifier:transaction.payment.productIdentifier];


    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];


    if(_delegate)
      [_delegate  transactionSucsess:transaction.payment.productIdentifier];
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    NSLog(@"restoreTransaction...");

    [self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];


    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];


    if(_delegate)
        [_delegate  transactionOnRestore:transaction.payment.productIdentifier];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {

    NSLog(@"failedTransaction...");
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
    }

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

    if(_delegate)
        [_delegate  transactionOnFail];
}

- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {

    [_purchasedProductIdentifiers addObject:productIdentifier];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

- (void)restoreCompletedTransactions {
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

@end

创建IAPHelper的类RageIAPHelper子类

#import "IAPHelper.h"

@interface RageIAPHelper : IAPHelper

+ (RageIAPHelper *)sharedInstance;



@end

在RageIAPHelper.m

#import "RageIAPHelper.h"

@implementation RageIAPHelper

+ (RageIAPHelper *)sharedInstance {
    static dispatch_once_t once;
    static RageIAPHelper * sharedInstance;
    dispatch_once(&once, ^{
        NSSet * productIdentifiers = [NSSet setWithObjects:
                                      @"your identifier",@"your identifier",
                                      nil];
        sharedInstance = [[self alloc] initWithProductIdentifiers:productIdentifiers];

    });
    return sharedInstance;
}




@end


[[RageIAPHelper  sharedInstance]  setDelegate:(id)self];

[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {


    if (success) {
        totalProduct_Arr =products;
        for (SKProduct *s in totalProduct_Arr) {
            NSLog(@"product Identifier %@",[s productIdentifier]);
            if ([[s productIdentifier]isEqualToString:FULLVERSION]) {
                [self.btnFullVersion setEnabled:YES];
            }
            else if ([[s productIdentifier]isEqualToString:REMOVE_ADS]) {
                [self.btnRemoveAd setEnabled:YES];
            }
        }
    }
    else{
        UIAlertView   *alertFail=[[UIAlertView  alloc]  initWithTitle:@"Product Not Found" message:@"Fails to load product.." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

        [alertFail  show];

    }
}];

并购买产品

                     [[RageIAPHelper sharedInstance] buyProduct:product];

答案 1 :(得分:0)

此链接可以帮助您。请一步一步地遵循此link

您也可以使用此代码。

  1. 添加StoreKit.framework,Accounts.framework,MobileCoreServices.framework,QuartzCore.framework,CFNetwork.framework,SystemConfiguration.framework。
  2. 在.h文件导入&#39; StoreKit.h&#39;  #import
  3. 在.h类上添加这些委托  
  4. 为SKProductsRequest声明属性 @property(非原子,强)SKProductsRequest * request;
  5. 现在在.m课程中复制此代码以进行应用内购买

    -(void)startpurching
    {
        NSArray *arrPackageproductIdentifier=[NSArray arrayWithObjects:@"Your Product Id",nil];
        NSString *strPrductIdentifier=[arrPackageproductIdentifier objectAtIndex:0];
    
        request= [[SKProductsRequest alloc]
              initWithProductIdentifiers: [NSSet setWithObject:strPrductIdentifier]];
        NSLog(@"%@",request);
        request.delegate = self;
        [request start];
    }
    
    
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
    {
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
        NSArray *myProduct = response.products;
    
        if ([myProduct count]==0) {
            UIAlertView *alt=[[UIAlertView alloc]initWithTitle:nil message:@"Cannot connect !! Please check your internet connection." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alt show];
        } else {
            NSLog(@"pro...%@",[[myProduct objectAtIndex:0] productIdentifier]);
    
            SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
            [[SKPaymentQueue defaultQueue] addPayment:newPayment];
    
        }
    }
    
    
    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
    {
        NSLog(@"Transaction....%@",transactions);
        for (SKPaymentTransaction *transaction in transactions) {
            switch (transaction.transactionState) {
                NSLog(@"%d",transaction.transactionState);
                case SKPaymentTransactionStatePurchased:
                    [self completeTransaction:transaction];
                    break;
                case SKPaymentTransactionStateFailed:
                    [self failedTransaction:transaction];
                    break;
                case SKPaymentTransactionStateRestored:
                    [self restoreTransaction:transaction];
                default:
                    break;
            }
        }
        NSLog(@"=======%@",transactions);
    }
    
    - (void) completeTransaction: (SKPaymentTransaction *)transaction
    {
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
        NSLog(@"Transaction Completed");
    
        NSUserDefaults *remvAddSttr = [NSUserDefaults standardUserDefaults];
        // saving an NSString
        NSString *chkRemVal=@"10";
        [remvAddSttr setObject:chkRemVal forKey:@"revAdd"];
    
        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"Transaction completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alrt show];
    }
    
    
    - (void) restoreTransaction: (SKPaymentTransaction *)transaction
    {
        NSLog(@"Transaction Restored");
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }
    
    - (void) failedTransaction: (SKPaymentTransaction *)transaction
    {
        if (transaction.error.code != SKErrorPaymentCancelled) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Unsuccessful"
                                                        message:@"Your purchase failed. Please try again."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
            [alert show];
        }
    
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }
    

    现在需要为非耗材产品设计“恢复”按钮。 对于“恢复”按钮,单击“复制此代码”。

    //*****************************************************************************
    //*****************************************************************************
    -(void)restoreBtnClicked
    {
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }
    
    - (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
    {
        if (error.code == SKErrorPaymentCancelled) {
             NSLog(@"error.code");
        }
    }
    
    - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
    {
        // NSMutableArray* purchasableObjects  = [[[NSMutableArray alloc] init] autorelease];
        for (SKPaymentTransaction *transaction in queue.transactions) {
            NSString *productID = transaction.payment.productIdentifier;
            NSLog(@"productID...%@",productID);
            if ([productID isEqualToString:@"Your Product Id"]) {
                UIAlertView *alt12=[[UIAlertView alloc]initWithTitle:nil message:@"Product Restored" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    
                [alt12 show];
            }
        }
        //Block Call to access the products
    }
    

答案 2 :(得分:0)

对我来说,由于会员中心没有注册设备,因此出现此错误。 以下是帮助我的案例。

  1. 在会员中心注册设备。
  2. 将您的开发团队设置为&#34;您的目标&#34; - &GT;一般 - &gt; &#34;团队及#34; 之后,这个错误消失了。

答案 3 :(得分:0)

我知道这真的很老了,但我遇到了同样的问题(我正在关注Ray W.教程https://www.raywenderlich.com/122144/in-app-purchase-tutorial)... 当我添加我的签名团队(目标&gt;一般&gt;签名&gt;团队)时,它已解决。 教程中没有提到这一点(Ray W上没有什么内容 - 它们通常很棒)