我在做应用程序采购时,很像的例子。这包括我首先从服务器获取我的产品信息。在我的例子中,我使用。
我遇到的问题是[[SKPaymentQueue defaultQueue] addTransactionObserver:self];没有工作,这意味着- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions没有被调用。
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];在_productIDCompletionHandler块中被调用。
我已经将其缩小到以下代码。只要我在[[SKPaymentQueue defaultQueue] addTransactionObserver:self];之前调用continueWithExecutor,观察者就能工作。如果我在addTransactionObserver块内或之后调用BFTask,则不会调用updatedTransactions方法。
-(void)getProductidsWithCompletionHandler:(AWSAccessProductIDsCompletionHandler)completionHandler {
_productIDCompletionHandler = [completionHandler copy];
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
// Construct the NSURL for the download location.
NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"aws_downloadedproductidfile"];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];
// Construct the download request.
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = [self bucketStringForType:WB_com_test];
downloadRequest.key = [NSString stringWithFormat:@"%@%@",COM_PRODUCTIDS_FILEPATH,COM_PRODUCTIDS_FILENAME];
downloadRequest.downloadingFileURL = downloadingFileURL;
[[transferManager download:downloadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock:^id(BFTask *task) {
if (task.error){
NSLog(@"Error: %@", task.error);
_productIDCompletionHandler(NO,nil);
_productIDCompletionHandler = nil;
}
if (task.result) {
NSLog(@"Success: ");
AWSS3TransferManagerDownloadOutput *output = task.result;
NSURL *url = (NSURL *)output.body;
NSArray *products = [NSArray arrayWithContentsOfURL:url];
DDLogVerbose(@"Success Result Count: %lu", (unsigned long)[products count]);
if (products)
{
_productIDCompletionHandler( YES, products);
} else
{
_productIDCompletionHandler( NO, nil);
}
_productIDCompletionHandler = nil;
}
return nil;
}];}
当上面的_productIDCompletionHandler处理程序返回时,它位于主线程上,所以这不是问题,但是线程不知何故是“脏的”。
编辑更多信息添加了
我的应用程序内购买类调用我的AWS类来获得一个包含我所有产品的文件。我需要建立我的产品清单从什么是存储在AWS之前,我让苹果观察员开始。奇怪的是,正如我前面指出的,如果我在调用getProductidsWithCompletionHandler之前设置了观察者,那么它就能工作。在目前的情况下,什么都不会发生。
if (!awsAccess) {
awsAccess = [AWSAccess new];
}
[awsAccess getProductidsWithCompletionHandler:^(BOOL success, NSArray *awsProductIDs) {
if (success)
{
DDLogVerbose(@"Got ProductIDs from AWS...");
for (NSDictionary *productInfoDict in awsProductIDs)
{
IAPProductInfo *info =[[IAPProductInfo alloc] initFromDict:productInfoDict];
[self addInfo:info forProductIdentifier:info.productIdentifier];
}
DDLogVerbose(@"Adding the Apple transaction observer in case there are old purchases.");
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
} else {
DDLogVerbose(@"Error getting ProductIDs from AWS...");
}
}];当用户通过应用程序启动购买时,应用程序中的购买类(包含上述代码和观察者)和观察者被调用,但是始终只有一个观察者,因为我在应用程序中的购买类是单例的。
基于注释的更新2
H实现SKPaymentTransactionObserver委托。我已经创建了一个只有3个函数的新测试类。这个类只在应用程序委托中使用。
我已经在我尝试过观察员的地方发表了评论,无论它是否有效。
我查过“自我”,这不是零。我还检查了主1号线程上是否调用了观察者,这就是AWS代码中的BFExecutor。
#import "TestIAPurchase.h"
#import "AWSAccess.h"
@implementation TestIAPurchase
-(void)checkForFailedPurchases {
DDLogVerbose(@"checking for failed purchases by getting all products and waiting to see if Apple replies.");
//Tried it here and it works immediately
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self loadProductsWithCompletionHandler:^(BOOL success, NSError *error) {
DDLogVerbose(@"checking for failed purchases Request for products has returned from AWS...");
if (!success) {
DDLogVerbose(@"checking for failed purchases Request For Products Failed");
} else
{
DDLogVerbose(@"checking for failed purchases Request For Products was a success...");
//Tried it here and it does not work if I go and get the AWS information
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
}];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
DDLogVerbose(@"paymentQueue - updatedTransaction...");
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
DDLogVerbose(@"SKPaymentTransactionStatePurchased");
break;
case SKPaymentTransactionStateFailed:
DDLogVerbose(@"SKPaymentTransactionStateFailed");
break;
case SKPaymentTransactionStateRestored:
DDLogVerbose(@"SKPaymentTransactionStateRestored");
break;
case SKPaymentTransactionStateDeferred:
DDLogVerbose(@"SKPaymentTransactionStateDeferred");
break;
case SKPaymentTransactionStatePurchasing:
DDLogVerbose(@"SKPaymentTransactionStatePurchasing");
break;
default:
break;
}
};
}
- (void)loadProductsWithCompletionHandler:(void (^)(BOOL success, NSError * error))productCompletionHandler
{
DDLogVerbose(@"loadProductsWithCompletionHandler - by getting them from AWS.");
AWSAccess * awsAccess = [AWSAccess new];
//Tried it here and it works immediately
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[awsAccess getProductidsWithCompletionHandler:^(BOOL success, NSArray *awsProductIDs) {
if (success)
{
DDLogVerbose(@"Adding the Apple transaction observer in case there are old purchases.");
//Trying it here and it does not work
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
if (productCompletionHandler) {
productCompletionHandler(TRUE, nil);
}
} else {
DDLogVerbose(@"Error getting ProductIDs from AWS...");
if (productCompletionHandler) {
productCompletionHandler(FALSE, nil);
}
}
}];
}
@end发布于 2015-08-09 13:45:56
看来我发现了问题。这与AWS无关,只是碰巧AWS与我的测试有关。
基本上,当我在应用程序委托中使用上面的代码时,Facebook的代码也是在[FBSDKAppEvents activateApp];的应用委托中启动的。Facebook也会听取同一个观察者的意见。如果我使用AWS代码,Facebook就有足够的时间成为观察者,并收到苹果的回复。然后,我也注册为一个观察员,没有得到苹果的回应,我认为是因为苹果只是发送了一个。
https://stackoverflow.com/questions/31774503
复制相似问题