ios – 在App中购买无法在设备上运行

ios – 在App中购买无法在设备上运行,第1张

概述我尝试在我的应用程序上进行应用程序购买.但是当我尝试在设备运行它时,我没有结果. 我的代码如下: 这是获取产品信息并购买产品所需的对象. .H #import <Foundation/Foundation.h>#import "StoreKit/StoreKit.h"#define kProductsLoadedNotification @"ProductsLoaded" 我尝试在我的应用程序上进行应用程序购买.但是当我尝试在设备中运行它时,我没有结果.

我的代码如下:

这是获取产品信息并购买产品所需的对象.

.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

#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

单身对象

.H

#import <Foundation/Foundation.h>#import "StoreManager.h"@interface StoreWrapper : StoreManager+ (StoreWrapper *)sharedInstance;@end

.M

#import "StoreWrapper.h"@implementation StoreWrapperstatic 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

在我的视图控制器中,我呼叫下一个:

- (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];}

为此,我发送了一个请求从服务器获取产品信息的产品ID

接下来如果产品加载我的类得到关于此的通知(并调用以下方法).

- (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];            }    }

对于按钮我添加选择器buyProduct(如上所示)

- (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];    }

对于iPhone模拟器这个工作很棒,但在设备上我没有任何按钮,因为response.products没有下载.

调用所有方法,但委托方法* – (voID)productsRequest:(SKProductsRequest *)请求dIDReceiveResponse:(SKProductsResponse)响应不返回response.products.

感谢您的时间和感谢您的回答!

解决方法 为了解决这个问题,我重置了我的设备并重新运行了应用程序.

这对我有帮助.

总结

以上是内存溢出为你收集整理的ios – 在App中购买无法在设备上运行全部内容,希望文章能够帮你解决ios – 在App中购买无法在设备上运行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1068397.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存