我正在尝试在我的app中使用cocos2d x c ++集成In App Purchases。我正在使用easyNdk Helper进行应用内购买。我的应用内购买适用于我的Objective C应用。但对于cocos2d x,它会引发以下行的错误
if ([[RageIAPHelper sharedInstance] productPurchased:productP.productIdentifier])
实际上,CPP文件的值完全以参数的形式出现,并且在NSLog中正确显示它们的值,但是它总是将对象显示为nil甚至objetcs在NSLog中打印它们的存储值
@try catch条件也不起作用
最后抛出以下错误
请帮我解决一下我必须做的事情? 感谢
我的.CPP代码是
NDKHelper::AddSelector("HelloWorldSelectors",
"SampleSelector",
callfuncND_selector(Main::cameFromObjC),
this);
CCDictionary* prms = CCDictionary::create();
prms->setObject(CCString::create("SampleSelector"), "to_be_called");
prms->setObject(CCString::create(result), "BirdNameKey");
SendMessageWithParams(string("SampleSelector"), prms);
和.mm代码是
- (void) SampleSelector:(NSObject *)prms
{
NSLog(@"purchase something called");
NSDictionary *parameters = [[NSDictionary alloc]init];// (NSDictionary*)prms;
parameters = (NSDictionary*)prms;
NSLog(@"Passed params are : %@", parameters);
// Fetching the name of the method to be called from Native to C++
// For a ease of use, i have passed the name of method from C++
NSString* CPPFunctionToBeCalled = (NSString*)[parameters objectForKey:@"to_be_called"];
//NSString *str = [NSString stringWithFormat:@"%@",[parameters valueForKey:@"BirdNameKey"]];
NSString *BirdName = [parameters valueForKey:@"BirdNameKey"];
NSString *str = [[NSString alloc]initWithFormat:@"%@",[parameters objectForKey:@"BirdNameKey"]];
NSUserDefaults *d2 = [NSUserDefaults standardUserDefaults];
NSLog(@"%@ , %@ , %@", str,BirdName,[d2 objectForKey:@"product"]); // output is ok for all
SKProduct * product = (SKProduct *) [ APPDELEGATE.productDictionary objectForKey:[d2 objectForKey:@"product"]];
[ APPDELEGATE.priceFormatter setLocale:product.priceLocale];
APPDELEGATE.currentProduct =product;
if ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier])
{
// check the product purchased or not but app crash at this if statement
}
[IOSNDKHelper SendMessage:CPPFunctionToBeCalled WithParameters:nil];
}
答案 0 :(得分:1)
我也有这个问题,我已经解决了。 在你的IAPhelper.mm
中仅执行此操作替换此行
_purchasedProductIdentifiers = [NSMutableSet set];
使用以下行
_purchasedProductIdentifiers = [[NSMutableSet alloc] init];
如下图所示
- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers {
if ((self = [super init])) {
// Store product identifiers
_productIdentifiers = productIdentifiers;
// Check for previously purchased products
// _purchasedProductIdentifiers = [NSMutableSet set];
_purchasedProductIdentifiers = [[NSMutableSet alloc] init];
for (NSString * productIdentifier in _productIdentifiers) {
BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
if (productPurchased) {
[_purchasedProductIdentifiers addObject:productIdentifier];
// NSLog(@"Previously purchased: %@", productIdentifier);
} else {
// NSLog(@"Not purchased: %@", productIdentifier);
}
}
// Add self as transaction observer
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
return self;
}
答案 1 :(得分:0)
我发现您的代码有两个(潜在的)问题,
首先
CCDictionary* prms = CCDictionary::create();
请注意,通过这种方式初始化数据对象并不能保证在以后尝试在代码中访问它时(特别是在其他函数中)不会释放它
所以,试试这个,
CCDictionary* prms = new CCDictionary();
prms->init ... (the initialization)
但请注意,通过这样做,现在您有责任在完成后删除此对象。另外,我不确定" setObject"的实现。方法,如果它保留了对象,那么我相信这一步没有必要,但我不确定你必须检查它!
其次
NSDictionary *parameters = [[NSDictionary alloc]init];// (NSDictionary*)prms;
parameters = (NSDictionary*)prms;
NSLog(@"Passed params are : %@", parameters);
我觉得那个演员有问题(或者我可能错了)
我建议你做这样的事情
(NSDictionary *)nsDictionaryFromCCDictionary:(cocos2d::CCDictionary *)ccDictionary {
if (ccDictionary == NULL) {
return NULL;
} else if (ccDictionary->allKeys() == NULL) {
return NULL;
} else if (ccDictionary->allKeys()->count() <= 0) {
return NULL;
}
cocos2d::CCLog("1");
NSMutableDictionary *nsDict = [NSMutableDictionary dictionaryWithCapacity:ccDictionary->allKeys()->count()];
cocos2d::CCLog("2");
for (int i = 0; i < ccDictionary->allKeys()->count(); i++) {
cocos2d::CCLog("3");
cocos2d::CCObject* obj = ccDictionary->objectForKey(((cocos2d::CCString *)ccDictionary->allKeys()->objectAtIndex(i))->getCString());
NSObject* nsObject;
if(isKindOfClass(obj, cocos2d::CCDictionary))
{
nsObject = @"Dictionary";
}
else if(isKindOfClass(obj, cocos2d::CCArray))
{
nsObject = @"Array";
}
else if (isKindOfClass(obj, cocos2d::CCString))
{
const char* cstring = ((cocos2d::CCString*)obj)->getCString();
nsObject = [[[NSString alloc] initWithBytes:cstring length:strlen(cstring) encoding:NSUTF8StringEncoding] autorelease];
}
else if (isKindOfClass(obj, cocos2d::CCInteger))
{
nsObject = [NSString stringWithFormat:@"%d", ((cocos2d::CCInteger*)obj)->getValue()];
}
else
{
nsObject = @"Unknown Object";
}
[nsDict setValue:nsObject forKey:[AnalyticXStringUtil nsstringFromCString:((cocos2d::CCString *)ccDictionary->allKeys()->objectAtIndex(i))->getCString()]];
}
return nsDict;
}
我已粘贴上述代码&gt;&gt;&gt; https://github.com/diwu/AnalyticX/blob/master/Add-To-Your-Own-Project/AnalyticXStringUtil.mm
尝试接收字典作为CCDictionary并检查此对象是否有效(因为mm可以有c ++和目标c代码,所以这不会成为问题)然后在创建NSDictionary之后,尝试打印它。我希望这次不会给你错误。