2014-06-07 121 views
0

我正在嘗試使用cocos2d x C++在應用程序中集成應用程序購買。我正在使用easyNdk助手進行應用內購買。我的應用程序購買完美適用於我的Objective C應用程序。但對於cocos2d的X是以下行Cocos2dx InApp爲ios購買

if ([[RageIAPHelper sharedInstance] productPurchased:productP.productIdentifier]) 

其實值從CPP進來的參數形式完美的文件,並正確顯示其在NSLog的值拋出錯誤,但它總是顯示對象作爲零甚至objetcs打印其在NSLog的

儲值還@try捕捉條件不工作

終於拋出了以下錯誤enter image description here

請幫助我,我有什麼關係? 感謝

我.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

你發送給這個MM文件這個值,是字符*?另外,你有沒有在MM文件中記錄這個值?另外,您使用的這個對象的剩餘部分是什麼? –

+0

@ Al-mo嗨,親愛的,我將參數從.cpp文件傳遞給.mm文件,然後將其轉換爲NSDictionary。我的項目中沒有使用任何retain關鍵字,因爲它是ARC項目。恐怕我現在不應該在那裏我必須設置retaincount :(,我不知道你是什麼意思的「你有沒有在MM文件中記錄這個值」我也嘗試使用CCUserDefault設置值,然後試圖通過NSUserDefault來解決問題,但同樣的問題存在:( –

+0

在發送之前,你能檢查什麼是你的字典的RETAINCOUNT,並且當你收到這本字典時,你接受它作爲NSDICTIONARY(參數)還是你使用從CPP文件發送的Dictory創建了一個NSDictionary對象? –

回答

1

我也有這個問題,我解決了它。 在您的IAPhelper中。毫米

只有具備以下行

_purchasedProductIdentifiers = [[NSMutableSet alloc] init]; 

做替換該行

_purchasedProductIdentifiers = [NSMutableSet set]; 

如下圖所示

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

} 
+0

它的工作。謝謝 –

+0

歡迎親愛的:) – user3610913

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; 

}

我粘貼了以上代碼>>>https://github.com/diwu/AnalyticX/blob/master/Add-To-Your-Own-Project/AnalyticXStringUtil.mm

嘗試接收字典作爲CCDictionary,並檢查此對象是否有效(因爲mm可以有C++和目標c代碼,所以這不會是一個問題)然後創建NSDictionary後,嘗試打印它。我希望這次不會給你錯誤。

+0

親愛的Al-mo我嘗試了您的建議技術,但徒勞無功,即使我提供HardCoded In App pur追逐的關鍵,但仍然是拋出相同的錯誤我認爲這可能是一些編譯器問題 –

+0

我很想調試它......因爲它似乎很奇怪! –

+0

你可以給我任何樣品ios在應用程序購買代碼運行在cocos2d-x –