2012-02-08 8 views
5

Intento realizar la compra de la aplicación en mi aplicación. Pero no he resultado cuando trato de ejecutarlo en el dispositivo.En la compra de la aplicación no funciona en el dispositivo

Mi código es el siguiente:

Esta es objeto necesario para obtener información sobre productos y comprarlo.

.h

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

#define kProductsLoadedNotification   @"ProductsLoaded" 
#define kProductPurchasedNotification  @"ProductPurchased" 
#define kProductPurchaseFailedNotification @"ProductPurchaseFailed" 

@interface StoreManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver> 

@property (strong, nonatomic) NSMutableSet * _purchasedProducts; 
@property (strong, nonatomic) NSArray *products; 
@property (strong, nonatomic) NSSet *_productIdentifiers; 
@property (strong, nonatomic) SKProductsRequest *request; 

- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers; 
- (void)requestProducts; 
- (void)buyProduct:(SKProduct *)product; 

- (void)completeTransaction:(SKPaymentTransaction *)transaction; 
- (void)restoreTransaction:(SKPaymentTransaction *)transaction; 
- (void)failedTransaction:(SKPaymentTransaction *)transaction; 
- (void)provideContent:(NSString *)productIdentifier; 
- (void)recordTransaction:(SKPaymentTransaction *)transaction; 

@end 

.m

objeto
#import "StoreManager.h" 

@implementation StoreManager 
@synthesize _purchasedProducts; 
@synthesize products; 
@synthesize _productIdentifiers; 
@synthesize request; 

// Initializes the request object with the set of product identifiers. 
- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers{ 
    self = [super init]; 
    if(self){ 
     self._productIdentifiers = productIdentifiers;   
     NSMutableSet *purchased = [NSMutableSet set]; 
     for(NSString * productId in self._productIdentifiers){ 
      BOOL flag = [[NSUserDefaults standardUserDefaults] boolForKey:productId]; 
      if(flag){ 
       [purchased addObject:productId]; 
       NSLog(@"Previously purchased: %@", productId); 
      } 
      NSLog(@"Not purchased: %@", productId); 
     } 
     self._purchasedProducts = purchased; 
    } 
    return self; 
} 

// Request info for the product. 
- (void)requestProducts{ 
    self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:self._productIdentifiers]; 
    self.request.delegate = self; 
    [self.request start]; 
} 

// Request fail. 
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{ 
    NSLog(@"Fail request! Error: %@", error); 
} 

// Delegate method - did receive responce. 
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ 
    self.products = response.products; 
    self.request = nil; 
    [[NSNotificationCenter defaultCenter] postNotificationName:kProductsLoadedNotification object:self.products]; 
} 

// Buy product. 
- (void)buyProduct:(SKProduct *)product{  
    SKPayment *payment = [SKPayment paymentWithProduct:product]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 
} 

// Payment transactions 
- (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)recordTransaction:(SKPaymentTransaction *)transaction {  
    // TODO: Record the transaction on the server side...  
} 

- (void)completeTransaction:(SKPaymentTransaction *)transaction{ 
    [self recordTransaction: transaction]; 
    [self provideContent: transaction.payment.productIdentifier]; 
    NSLog(@"completeTransaction..."); 
} 

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

    NSLog(@"Toggling flag for: %@", productIdentifier); 

    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:productIdentifier]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    [self._purchasedProducts addObject:productIdentifier]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchasedNotification object:productIdentifier]; 

} 

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

    NSLog(@"restoreTransaction..."); 

    [self recordTransaction: transaction]; 
    [self provideContent: transaction.originalTransaction.payment.productIdentifier]; 
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

} 

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

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

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchaseFailedNotification object:transaction]; 

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

} 

@end 

Singleton

.h

#import <Foundation/Foundation.h> 
#import "StoreManager.h" 

@interface StoreWrapper : StoreManager 

+ (StoreWrapper *)sharedInstance; 

@end 

.m

#import "StoreWrapper.h" 

@implementation StoreWrapper 
static StoreWrapper *gInstance = NULL; 

- (id)init{  
    NSSet *productId = [NSSet setWithObjects:@"com.company.sb.pack1", @"com.company.sb.pack5", nil]; 
    self = [super initWithProductIdentifiers:productId]; 
    if(self){ 

    } 
    return self; 
} 

+ (StoreWrapper *)sharedInstance{ 
    if(gInstance == NULL){ 
     gInstance = [[self alloc] init]; 
    } 
    return gInstance; 
} 

@end 

En mi controlador de vista Tiene la palabra:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productsLoaded:) name:kProductsLoadedNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:kProductPurchasedNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(productPurchaseFailed:) name:kProductPurchaseFailedNotification object: nil];  
    Reachability *reach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [reach currentReachabilityStatus];  
    if (netStatus == NotReachable) {   
     NSLog(@"No internet connection!");   
    } else {   
     NSLog(@"Internet connection available!"); 
     // Request product 
     [[StoreWrapper sharedInstance] requestProducts];   
     [activIndicator startAnimating]; 
    }  
    [super viewWillAppear:animated]; 
} 

para este punto puedo enviar una solicitud de información del producto Descargue desde un servidor con mi Identificación del producto

siguiente si el producto cargado mi clase recibe una notificación sobre esto (y llame al siguiente método).

- (void)productsLoaded:(NSNotification *)notification{  
    [activIndicator stopAnimating]; 
    [activIndicator setHidden:YES];  
    NSArray *arr = [notification object];  
    for(int i = 0; i < [arr count]; i++){ 
     SKProduct *product = [[StoreWrapper sharedInstance].products objectAtIndex:i]; 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [btn setFrame:CGRectMake(0, i*55, 100, 50)]; 
     [btn setTitle:product.localizedTitle forState:UIControlStateNormal]; 
     [btn setTag:i]; 
     [btn addTarget:self action:@selector(buyProduct:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.view addSubview:btn];   
    }  
} 

Para añadir el botón i buyProduct de selección (como se muestra arriba)

- (void)buyProduct:(id)sender{ 
    NSLog(@"sender tag %d", [sender tag]); 
    int tag = [sender tag]; 
    SKProduct *product = [[StoreWrapper sharedInstance].products objectAtIndex:tag];  
    [[StoreWrapper sharedInstance] buyProduct:product];  
} 

para el iPhone Simulador de este trabajo maravilloso, pero el dispositivo de E don tienen ningún botón, porque no response.products descarga.

Todo método son los llamados, pero el método delegado * - (void) productsRequest: (SKProductsRequest *) Solicitud de didReceiveResponse: (SKProductsResponse ) respuesta no vuelve response.products.

Gracias por su tiempo y gracias por responder!

+0

Funciona en un simulador? La compra de InApp siempre debe probarse en un dispositivo. E incluso allí, debe cerrar la sesión de su cuenta de iTunes y usar una cuenta de prueba (especialmente creada para probar en AppStore) dentro de la aplicación. –

+0

sí, esto es correcto, pero para este momento necesito crear el botón solo después de obtener la respuesta del servidor y tengo productos. Si recibo productos, envié una notificación con este objeto y cuando recibí un mensaje, creé un botón con un conteo de productos de respuesta. Este es solo un ejemplo de prueba. Pero funciona solo en el simulador. –

+0

Quiero decir que, según el dispositivo o el simulador, el método de delegado (vacío) productsRequest: (SKProductsRequest *) solicita la respuesta de respuesta de respuesta (SKProductsResponse) o devuelve productos o no los devuelve. Este es mi problema. –

Respuesta

5

Para resolver este problema, reinicié mi dispositivo y volví a ejecutar la aplicación.

Esto ayuda para mí.

+0

Después del restablecimiento completo, el método de delegado productsRequest funciona correctamente. –

+0

después de una larga búsqueda, no funciona la solución, pero esto funciona bien .. Muchas gracias – Adel

+0

¿Cómo "restablecer el método delegado productsRequest"? – Jacky

Cuestiones relacionadas