簡體   English   中英

SKProductRequest委托未被調用

[英]SKProductRequest delegate not being called

我知道這肯定是一個相當普遍的問題,但是我從字面上搜索了互聯網並嘗試了數十種解決方案,到目前為止都無濟於事...:P我對這件事不屑一顧

因此,我正在iOS上實施InApp購買,並且遇到了最奇怪的問題。 我對SKProduct檢索進行了所有設置和早期測試,沒有大驚小怪,但是隨后整個事情停止了工作,我不知道為什么。

我遵循了一些教程,因此此代碼必須是熟悉的。 首先是InAppPurchaseManager.h:

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

#define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification"


@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate>
{

} 

-(void) requestWalterLevelPackData;


+(SKProduct *) WalterLevelPack;
+(NSArray *) productCatalog;


@property (strong,nonatomic) SKProductsRequest *request;


@end

到目前為止,沒有什么太奇怪了。 我將SKProductRequest放在屬性上以保留值,並設置兩個“類變量”以供外部訪問值。

轉到InAppPurchaseManager.mm文件:

#import "InAppPurchaseManager.h"

SKProduct* _walterLevelPack=nil;
NSArray* _productCatalog=nil;

@implementation InAppPurchaseManager

@synthesize request;

+(NSArray *) productCatalog
{
    return _productCatalog;
}

+(SKProduct *) WalterLevelPack
{
    return _walterLevelPack;
}


-(void)requestWalterLevelPackData
{

    NSSet *productIdentifiers = [NSSet setWithObject:@"MR_WALTER_LEVEL_PACK01" ];

    self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    self.request.delegate = self;
    [self.request start];

}


#pragma -
#pragma SKProductsRequestDelegate methods


- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    printf("got an error");
    NSLog(@"request failed: %@,  %@", request, error);

   [request release];

}

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    printf("delegate called");


    [_productCatalog release];

    _productCatalog = [response.products retain];

    [_walterLevelPack release];

    _walterLevelPack=[[_productCatalog firstObject] retain];

   //_walterLevelPack=[[self.products count] == 1 ? [[self.products firstObject] retain] : nil retain];


        NSLog(@"Product title: %@" , _walterLevelPack.localizedTitle);
        NSLog(@"Product description: %@" , _walterLevelPack.localizedDescription);
        NSLog(@"Product price: %@" , _walterLevelPack.price);
        NSLog(@"Product id: %@" , _walterLevelPack.productIdentifier);


    for (NSString *invalidProductId in response.invalidProductIdentifiers)
    {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }

    [request release];

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];


}


- (void)dealloc
{

    request.delegate = nil;
    [request cancel];
    request = nil;
    [super dealloc];
}


@end

SKProduct* StoreItem=nil;
NSArray* productCatalog=nil;


extern "C"
{

    NSString * RequestLevelPackData()
    {

        InAppPurchaseManager* purchaseManager=[[InAppPurchaseManager alloc] init];

        [purchaseManager requestWalterLevelPackData];


        productCatalog=[InAppPurchaseManager productCatalog];


        if(productCatalog==nil)
        {
            printf("empty!");
        }

        StoreItem=[InAppPurchaseManager WalterLevelPack];


        printf("%s", [StoreItem.localizedTitle UTF8String]);
        printf("%s", [StoreItem.localizedDescription UTF8String]);
        // printf("%s", [StoreItem.price UTF8String]);
        printf("%s", [StoreItem.productIdentifier UTF8String]);


        NSString *ProductData=@"Empty";


        return ProductData;
    }
}

在這里,我初始化了“類變量”,設置了productRequest委托並開始了整個工作。 在“ Ext C”部分,我創建InAppPurchaseManager類的實例,然后調用“ requestWalterLevelPackData()”函數。 之后,我嘗試訪問“類變量”以查看它們是否具有值。

預期的行為將是:

-NSLog在productRequest委托內打印和打印f以顯示在控制台上-Class變量將具有值。

我所擁有的是:

-控制台上沒有打印-類變量為空

經過一些痛苦的斷點檢查后,我得出的結論是沒有調用productsRequest委托方法。 我放置在方法內部的斷點沒有達到,變量值也沒有設置。 發生的所有事情都是在self.request和@synthetize的動作之間在RequestWalterLevelPack()內部大量跳躍。

現在最時髦的部分是該代碼實際上可以正常工作。 我已經打印出productRequest內的所有Nlog,斷點只會碰到一點,然后……就沒有更多了。 我所做的唯一顯着變化是來回更改名稱,添加“類變量”和一些測試。

我在不注意的情況下剎車了嗎? 有人可以幫我嗎?

我正在運行Xcode 4.2,目標iOS版本是5.0,開發設備是iphone 3GS。

Store Kit可能很難進行故障排除。 如果您沒有更改代碼,我很想考慮環境。 配給配置文件是否存在問題? 您要在設備上測試而不是模擬器上嗎? 您使用的是測試商店/沙箱帳戶(不是無意中使用您的正確帳戶登錄的。)現在,我寫這篇文章,我確實想起了我擁有這種“沉默”的情況,並且使用新創建的測試帳戶是解決問題的關鍵它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM