Httpwebrequest xcode exc_bad_access

I’m using the google weather api in the C# script and it runs correctly in Unity. The problem is when I build and run it with xcode it gives the following error GDB: Program received signal: “EXC_BAD_ACCESS”. And the Unity program won’t run. The error comes from the http web request in the code below.

GWP_Request = (HttpWebRequest)WebRequest.Create(string.Format(baseUrl));
GWP_Request.UserAgent = @“Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4”;
GWP_Response = (HttpWebResponse)GWP_Request.GetResponse();
GWP_XMLdoc = new XmlDocument();
GWP_XMLdoc.Load(GWP_Response.GetResponseStream());

Just the first line alone would get me the EXC_BAD_ACCESS error. How should I change the web request so it works with xcode?

WebRequests are not supported on iOS out of the box, though they might start working with some manual effort:

  1. you need proper “machine.config” file. The full machine.config can be found at /Applications/Unity/Unity.app/Contents/Frameworks/Mono/etc/mono/2.0 and eventually it should be copied to Xcode project’s Data/Managed/mono/2.0 folder before hitting build in Xcode.
  2. you need to tune stripping (probably the best idea is to turn it off for experiments)
  3. switching between .NET 2.0 full and subset profiles also might be required

One method I used was to use the TCPClient class and then build in the HTTP protocol on top of it. The basic protocol is really simple, and you can slowly add support for things you need as you need them(chunked requests, Get vs. Post, Keep-Alive connections, etc…).
The TCPClient works under .NET subset and Byte Code Stripping without any issues which can help keep your build compact.

HTTPWebRequest, even once the config file is modified, still doesn’t handle ByteCodeStripping well from my experience, which at least for me was a deal breaker.