如何使用NSUserDefaults而不是我自己的服务器添加耗材In App Purchase? [英] How do I add consumable In App Purchases using NSUserDefaults and not my own server?

查看:112
本文介绍了如何使用NSUserDefaults而不是我自己的服务器添加耗材In App Purchase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新,所以如果可能的话,我需要简单的答案。我的第一个应用程序刚刚完成(但它甚至没有提交到商店),我想添加In App Purchases。我的应用程序是一个游戏,用户以一些虚拟货币硬币开始。我想为他们添加一个IAP,以便能够以三个到五个不同的价格点购买更多的硬币。



我看过很多教程,似乎信息混杂了。任何人都可以帮助我如何在这里继续,以一种对于一个非常新的开发人员而且与我的特定案例相关的方式。我想免费添加IAP,但如果有一个非常值得的简单付费解决方案,那可能没问题。



我正在使用Xcode 4.3.2。我想使用NSUserDefaults而不是我自己的服务器,我有NSUserDefaults的代码。至少它存储了硬币并且有一个值的键。



此外,由于IOS 6已经用完了,我需要一个全新的版本吗? Xcode?

感谢您的帮助,请原谅我写好问题的经验不足:)

解决方案

由于没有人回答这个问题,我会......如果只是为了帮助后来查看此内容的人。



请承认,我还没有用过-app还在购买..但我是一位经验丰富的obj-c程序员&我相信我在查看苹果文档之后理解了这个概念......这里是一个非常简单的消费品应用内购买示例(*注意,如果它们是非消耗品或其他方式,您应该在此处包含更多内容):



首先,在项目中加入StoreKitobjective-c框架。



我创建了这个名为 InAppPurchaser - 请包括此课程..



InAppPurchaser.h:

  #import< Foundation / Foundation.h> 
#import< StoreKit / StoreKit.h>

@protocol InAppPurchaserDelegate< NSObject>
- (void)InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID;
- (void)InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)事务productID:(NSString *)productID错误:(NSError *)错误;
@end

@interface InAppPurchaser:NSObject< SKPaymentTransactionObserver>

@property(非原子,保留)id< InAppPurchaserDelegate>代表;

#pragma mark Instantiation ...
- (id)init;
+(InAppPurchaser *)买家;
+(InAppPurchaser *)purchaserWithDelegate:(id< InAppPurchaserDelegate>)委托;

#pragma mark Utilities ...
- (void)purchaseProductWithProductIdentifier:(NSString *)productID数量:(NSInteger)数量;
- (void)restoreTransactions;

@end

InAppPurchaser.m:

  #importInAppPurchaser.h

@implementation InAppPurchaser

@synthesize delegate;

- (id)init {

if(self = [super init]){

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

} //结束条件...

return(self);

} //结束方法...

+(InAppPurchaser *)买方{

return([[InAppPurchaser alloc] init]);

} //结束方法...

+(InAppPurchaser *)purchaserWithDelegate:(id< InAppPurchaserDelegate>)委托{

InAppPurchaser *买方= [InAppPurchaser买方];
purchaser.delegate = delegate;

退货(买方);

} //结束方法...

#pragma mark操作...

- (void)purchaseProductWithProductIdentifier:(NSString *)productID数量:(NSInteger)数量{

SKMutablePayment * payment = [[SKMutablePayment alloc] init];
payment.productIdentifier = productID;
payment.quantity = quantity;

[[SKPaymentQueue defaultQueue] addPayment:payment];

} //结束方法...

#pragma mark SKPaymentTransactionObserver ...

- (void)paymentQueue:(SKPaymentQueue *)队列updatedTransactions:(NSArray *)交易{

for(SKPaymentTransaction *交易中的交易){

switch(transaction.transactionState){

case SKPaymentTransactionStatePurchased :
[self completeTransaction:transaction];
休息;

案例SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
休息;

case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
休息;

默认值:
// ...
break;

} //结束开关语句...

} //结束循环...

} //结束方法...

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

if(delegate!= nil&& [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionSuccessfully:productID :)] )
[委托InAppPurchaserHasCompletedTransactionSuccessfully:transaction productID:transaction.payment.productIdentifier];

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

} //结束方法...

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

if(delegate!= nil) && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionSuccessfully:productID :)])
[委托InAppPurchaserHasCompletedTransactionSuccessfully:transaction productID:transaction.payment.productIdentifier];

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

} //结束方法...

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

if(delegate!= nil) && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionUnsuccessfully:productID:error :)])
[委托InAppPurchaserHasCompletedTransactionUnsuccessfully:transaction productID:transaction.payment.productIdentifier error:transaction.error];

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

} //结束方法...

- (无效)restoreTransactions {

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

} //结束方法...


@end

示例用法:



MyClassViewController.h:

  #import< UIKit / UIKit.h> 
#importInAppPurchaser.h

@interface MyClassViewController:UIViewController< InAppPurchaserDelegate>

@end

MyClassViewController.m:

  #importMyClassViewController.h

@interface MyClassViewController()

@end

@implementation MyClassViewController

- (void)viewDidLoad {

[super viewDidLoad];

} //结束方法...

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

} //结束方法...

#pragma mark采购...

- (无效)myPurchaseMethod {

InAppPurchaser * buyer = [InAppPurchaser buyerserWithDelegate:self];
[purchaseser purchaseProductWithProductIdentifier:@MyProduct数量:1];

} //结束方法...

- (void)InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)事务productID:(NSString *)productID错误:(NSError *)error {

//处理成功代码.. IE:[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@MyProduct];

} //结束方法...

- (void)InAppPurchaserHasCompletedTransaction成功:(SKPaymentTransaction *)交易productID:(NSString *)productID {

//处理失败代码...

} //结束方法...

@end

此代码未经过测试,但它应该可以运行..显然,它可以改进,但它应该让你开始。



最后,你也想在你的AppDelegate中使用它...就像你从ViewController那样调用它,除了不收取购买费用..只需调用我包含的restoreTransactions方法即可。这将恢复因应用或互联网连接失败或其他原因而发生的任何未完成的交易。



示例:



在AppDelegate.h中:

  @interface AppDelegate:UIResponder< UIApplicationDelegate,InAppPurchaserDelegate> AppDelegate.m中的

   - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions :( NSDictionary *)launchOptions {

InAppPurchaser * buyer = [InAppPurchaser purchaserWithDelegate:self];
[buyerser restoreTransactions];

返回(是);

} //结束方法...

- (void)InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)事务productID:(NSString *)productID错误:(NSError *)error {

//处理成功代码.. IE:[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@MyProduct];

} //结束方法...

- (void)InAppPurchaserHasCompletedTransaction成功:(SKPaymentTransaction *)交易productID:(NSString *)productID {

//处理失败代码...

} //结束方法...


I'm new so I need simple answers if at all possible. I have my first app just about done (but it's not even submitted to the store), and I want to add In App Purchases. My app is a game where the user starts with some virtual currency "coins". I want to add an IAP for them to be able to buy more coins at three to maybe five different price points.

I have seen a ton of tutorials and it seems the info is mixed. Can anyone help me out on how to proceed here, in a way that's realistic for a very new developer and relevant to my particular case. I'd like to add IAP for free but if there is a simple paid solution that's really worth it, that might be okay.

I am using Xcode 4.3.2. I would like to use NSUserDefaults and not my own server and I have code in place for the NSUserDefaults. At least it stores the "coins" and has a key for the value.

Additionally are things different now that IOS 6 is out, do I need a whole new version of Xcode?

Thanks for your help, please forgive my inexperience in writing good questions :)

解决方案

Since nobody answered this, I will..even if only to help people who view this later.

Please acknowledge, I have not used in-app purchases yet.. but I'm an experienced obj-c programmer & i believe i understand the concept after viewing the apple docs once... here is a very simple example for "consumable" in-app purchases (*Note, you should include more here if they are non-consumable or otherwise):

Firstly, include the "StoreKit" objective-c framework into your project.

I created this simple class called "InAppPurchaser" - Please include this class also..

"InAppPurchaser.h":

#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>

@protocol InAppPurchaserDelegate <NSObject>
- (void) InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID;
- (void) InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID error:(NSError *)error;
@end

@interface InAppPurchaser : NSObject <SKPaymentTransactionObserver>

@property (nonatomic, retain) id <InAppPurchaserDelegate> delegate;

#pragma mark Instantiation...
- (id) init;
+ (InAppPurchaser *) purchaser;
+ (InAppPurchaser *) purchaserWithDelegate:(id <InAppPurchaserDelegate>)delegate;

#pragma mark Utilities...
- (void) purchaseProductWithProductIdentifier:(NSString *)productID quantity:(NSInteger)quantity;
- (void) restoreTransactions;

@end

"InAppPurchaser.m":

#import "InAppPurchaser.h"

@implementation InAppPurchaser

@synthesize delegate;

- (id) init {

    if ( self = [super init] ) {

        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    } // ends condition...

    return( self );

} // ends method...

+ (InAppPurchaser *) purchaser {

    return( [[InAppPurchaser alloc] init] );

} // ends method...

+ (InAppPurchaser *) purchaserWithDelegate:(id <InAppPurchaserDelegate>)delegate {

    InAppPurchaser *purchaser = [InAppPurchaser purchaser];
    purchaser.delegate = delegate;

    return( purchaser );

} // ends method...

#pragma mark Actions...

- (void) purchaseProductWithProductIdentifier:(NSString *)productID quantity:(NSInteger)quantity {

    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = productID;
    payment.quantity = quantity;

    [[SKPaymentQueue defaultQueue] addPayment:payment];

} // ends method...

#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];
                break;

            default:
                // ...
                break;

        } // ends switch statement...

    } // ends loop...

} // ends method...

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

    if ( delegate != nil && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionSuccessfully:productID:)] )
        [delegate InAppPurchaserHasCompletedTransactionSuccessfully:transaction productID:transaction.payment.productIdentifier];

        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

} // ends method...

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

    if ( delegate != nil && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionSuccessfully:productID:)] )
        [delegate InAppPurchaserHasCompletedTransactionSuccessfully:transaction productID:transaction.payment.productIdentifier];

        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

} // ends method...

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

    if ( delegate != nil && [delegate respondsToSelector:@selector(InAppPurchaserHasCompletedTransactionUnsuccessfully:productID:error:)] )
        [delegate InAppPurchaserHasCompletedTransactionUnsuccessfully:transaction productID:transaction.payment.productIdentifier error:transaction.error];

        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

} // ends method...

- (void) restoreTransactions {

    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

} // ends method...


@end

Example usage:

"MyClassViewController.h":

#import <UIKit/UIKit.h>
#import "InAppPurchaser.h"

@interface MyClassViewController : UIViewController <InAppPurchaserDelegate>

@end

"MyClassViewController.m":

#import "MyClassViewController.h"

@interface MyClassViewController ()

@end

@implementation MyClassViewController

- (void)viewDidLoad {

    [super viewDidLoad];

} // ends method...

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

} // ends method...

#pragma mark Purchasing...

- (void) myPurchaseMethod {

    InAppPurchaser *purchaser = [InAppPurchaser purchaserWithDelegate:self];
    [purchaser purchaseProductWithProductIdentifier:@"MyProduct" quantity:1];

} // ends method...

- (void) InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID error:(NSError *)error {

    // handle success code.. IE: [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MyProduct"];

} // ends method...

- (void) InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID {

    // handle failure code...

} // ends method...

@end

This code was not tested, but it should work.. Obviously, it can be improved, but it should get you started.

And lastly, you would want to use this in your AppDelegate as well.. call it as you would from the ViewController, except do not charge a purchase.. simply call the "restoreTransactions" method that I included.. This will restore any incomplete transactions that occurred due to app or internet connectivity failure or whatever.

Example:

In "AppDelegate.h":

@interface AppDelegate : UIResponder <UIApplicationDelegate, InAppPurchaserDelegate>

in "AppDelegate.m":

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    InAppPurchaser *purchaser = [InAppPurchaser purchaserWithDelegate:self];
    [purchaser restoreTransactions];

    return( YES );

} // ends method...

- (void) InAppPurchaserHasCompletedTransactionUnsuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID error:(NSError *)error {

    // handle success code.. IE: [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MyProduct"];

} // ends method...

- (void) InAppPurchaserHasCompletedTransactionSuccessfully:(SKPaymentTransaction *)transaction productID:(NSString *)productID {

    // handle failure code...

} // ends method...

这篇关于如何使用NSUserDefaults而不是我自己的服务器添加耗材In App Purchase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆