Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Aubrey-Falconer · Dec 16, 2012 at 03:51 AM · iospluginobjective-c

Passing NSDictionary from obj-C to C#

I have a Unity app which needs to read data from an iOS library, and I created an interface.mm file to connect that library to Unity.

I set up a GetNumber function in the interface file which returns an int. I can successfully read the int in Unity using [DllImport ("__Internal")] private static extern int GetNumber();

Now all I need to do is pass a NSDictionary instead of an int, but objective-c is incredibly foreign to me and I haven't yet figured out how to make things work.

My current interface code is:

 #include "KochavaUnitySupport.h"

 extern "C" {
     
     //Works!
     int GetNumber()
     {
         return 3;
     }
     
     //Need Help
     string GetPastboardAttribute(string pasteboardName)
     {
         UIPasteboard *pb = [UIPasteboard pasteboardWithName:pasteboardName create:NO];
         if (pb) return pb.string;
     }
     
     //Need serious help
     NSDictionary* GetKochavaInfo()
     {
         NSDictionary *kochavaInfo = [[KochavaUnitySupport sharedManager]returnKochavaInfo];
         return kochavaInfo;
     }
 }


MANY thanks to anyone that knows what I need to change to make the above code work, and what datatype I should be expecting to receive the NSDictionary in Unity (a Hashtable would be perfect).

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aubrey-Falconer · Dec 16, 2012 at 04:09 AM 0
Share

Received a tip from SuperPig on IRC that I should try implementing a visitor pattern - iterating through each element of the NSDictionary pointer and activating a c# callback function. Any other ideas, or tips on making this work?

avatar image Aubrey-Falconer · Dec 16, 2012 at 06:18 AM 0
Share

Ok, I now have this code working perfectly: http://pastebin.com/P2wCkiWe Unfortunately, I'd rather not rely on iOS 5's builtin JSON library, so I am now working on some code to build the JSON string manually through string concatenation. Any suggestions?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Aubrey-Falconer · Dec 16, 2012 at 06:57 AM

Done! This code should be extremely useful to anyone that happens to encounter similar situations in the future:

 #import "JSONKit.h"
 
 char* AutonomousStringCopy (const char* string)
 {
     if (string == NULL)
         return NULL;
     
     char* res = (char*)malloc(strlen(string) + 1);
     strcpy(res, string);
     return res;
 }
 
 extern "C" {
     char* GETDICTIONARY()
     {
         NSDictionary *myNSDict = //Get Dict Here
         
         //Works great for iOS 5+
         //NSError *error;
         //NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myNSDict options:0 error:&error];
         //NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

         //Much more compatible library
         NSString *jsonString = [myNSDict JSONString];
         
         const char* jsonChar = AutonomousStringCopy([jsonString UTF8String]);
         return AutonomousStringCopy(jsonChar);
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DonKanallie · Jul 23, 2013 at 10:28 AM 0
Share

Ins$$anonymous$$d of implementing your own AutonomousStringCopy, why don't you use `strdub`?

avatar image Tarlius · Jul 23, 2013 at 10:49 AM 0
Share

Is there a reason you didn't use NSString's method for generating CStrings?

avatar image
1

Answer by superpig · Dec 16, 2012 at 04:14 AM

It's not possible for Unity to work directly with an NSDictionary, as far as I know, so what you need to do is have your C DLL access it on your behalf. I'd use something like a visitor pattern for this:

 void GetKochavaInfo(void (*visitorFn)(const char* key, const char* value))
 {
    // Get your NSDictionary
    for(NSString* key in dictionary)
    {
        NSObject* value = [dictionary objectForKey:key];
        // do something with value to get a string out of it

        visitorFn([key UTF8String], [valueStr UTF8String]);
    }
 }

On the C# side, you'd then declare a delegate type to describe the function pointer:

 public delegate void DictionaryVisitorHandler(string key, string value);

and then the external function definition:

 public extern void GetKochavaInfo(DictionaryVisitorHandler handler);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aubrey-Falconer · Dec 16, 2012 at 06:19 AM 0
Share

Good idea. I am currently leaning towards JSON serialization, but i'll keep your solution in $$anonymous$$d

avatar image
0

Answer by david90 · Feb 18, 2014 at 12:14 PM

I use https://developers.facebook.com/docs/unity/reference/current/Json/ on C# side to deserialize JSON in unity.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Open Web page embedded in unity app using native UIVIEW 0 Answers

How to pass a Swift string into Unity iOS game? 0 Answers

iOS Objective C plugin linker error 2 Answers

Unity iPhone plugin - what data type to use? 1 Answer

Using NavigationController in an iOS plugin 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges