Contentful logo

Contentful Community

Building to iOS with Unity

I’m using the .NET SDK in Unity, all works as expected on Android but iOS is having issues.

If I do the following:

var vehicleBuilder = QueryBuilder<VehicleModel>.New.ContentTypeIs("vehicleModel").Include(3);
var entries = await _client.GetEntries(vehicleBuilder);

I get the error below (I will post just a few lines as it is quite large).

My best guess is it is something to do with Newtonsoft.Json.11.0.2. but can’t confirm for sure.

Any assistance would be great as this is a show stopper (just when I was had it working for Android!)

**NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor**

**at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, System.Boolean skipVisibility) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDynamicMethod (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDefaultConstructor[T] (System.Type type) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator (System.Type createdType) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateArrayContract (System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0** 

**at System.Func`2[T,TResult].Invoke (T arg) [0x00000] in <00000000000000000000000000000000>:0** 

**at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Utilities.ThreadSafeStore`2[TKey,TValue].Get (TKey key) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <00000000000000000000000000000000>:0**

Further investigation looks like this is a Newtonsoft.Json issue when attempting to use with iOS.

Is there a way to have a version of the SDK with dependency changed to this version? https://github.com/jilleJr/Newtonsoft.Json-for-Unity

dlls zipped here - https://www.dropbox.com/s/9hxt5muyncaasir/jillejr.newtonsoft.json-for-unity%4012.0.1-patch-001.zip?dl=0

Hi @jk123

This is unfortunate. There’s currently no plans as far as I know of switching the Json.Net dependency in the SDK, but with the upcoming release of https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/ it is very possible that the SDK will shift over to this once it’s generally available.

Best
Robert

Hi @robert

Is it possible to recompile the source code of the SDK to replace the Newtonsoft.Json.dll with the one in the Newtonsoft.Json AOT folder in the above zip?

I’m not looking to move away from Json.NET, just use a modified version for Unity.

Thanks.

Hi @robert

I downloaded the source code and change the nuget Json.NET dependancy to this one:
https://github.com/Daddoon/Json.NET.Aot

I compiled a new contentful .dll and it work for Android, but gave a different error in iOS.

**JsonSerializationException: Unable to find a constructor to use for type Contentful.Core.Models.SystemProperties. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'items[0].sys.space', line 11, position 16.**

Any thought on what this error could be?

Thanks.

I’ve been able to get past this issue by removing the Contentful .dll and replacing it with the source code.

Problems still persist in iOS with an EXC_BAD_ACCESS error so tracking that down now.

I think I’ve got the issue down to this. The dynamic keyword can’t be used in iOS.

It is described here - https://www.wintellect.com/cant-use-dynamic-c-keyword-xamarin-ios/

@robert any thoughts on how to work around this?

In my case I worked around this by changing the SyncedAsset’s Fields type from dynamic to Object and then scripted to handle this change in my code.

This, along with changing the Json library and added a link.xml condition in Unity look to have solve my issue.

That is great @jk123! Happy to hear that you could move forward.