App crash after Xamarin.iOS update. System.Exception

JHainesJHaines USMember

Hi. So I´m having some issues since updating to Xamarin.iOS 7.0.6. The problem occurs when trying to download a .pdf document. There´s also a few redirects in order to authenticate the user. This has never been an issue before updating. The exception I keep getting is:
"System.Exception has been thrown.
Could not initialize an instance of the type 'MonoTouch.Foundation.NSString': the native 'initWithData:encoding:' method returned nil. It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false."

This happens in my NSURLConnectionDelegate, more specifically in the FinishedLoading() method. Here´s some of the code:

`
internal class NativeUrlDelegate : NSUrlConnectionDelegate
{

        public NativeUrlDelegate(Action<string, int> success, Action<string, int> failure)
        {
            _successCallback = success;
            _failureCallback = failure;
            _data = new NSMutableData();
        }

        public override void ReceivedData(NSUrlConnection connection, NSData d)
        {
            _data.AppendData(d);
        }

        public override void FinishedLoading(NSUrlConnection connection)
        {
            if (_statusCode != 200)
            {
                _failureCallback(string.Format("Did not receive a 200 HTTP status code, received '{0}'", _statusCode),
                    _statusCode);
                return;
            }

            _successCallback(_data.ToString(), _statusCode);
        }

`
The http request if anyone want to see it:

                    var newRequest = Util.NetworkUtils.CreateNativeRequest (url,
                                         _headers);
                    NSUrlConnection.FromRequest (newRequest, new Util.NativeUrlDelegate ((body, httpStatus) => {
                        webview.LoadRequest (newRequest);

                    }, (reason, httpStatus) => {
                        Logger.Logg ("AttachmentController:Failed to connect because: ", reason);
                        Status = httpStatus;
                        Util.NetworkUtils.ShowAlert (Status, Errors.PrintFailed);
                        BTProgressHUD.Dismiss ();
                    }
                    ));

The exception gets thrown at _successCallback. When I change the request to a different(not a .pdf) url I don´t get an exception. As I said, this never happened before and works fine on all the other requests. Any ideas? Would it be possible to do a roll back to version 7.0.5 or 0.4? I would very much appreciate any suggestions.

Posts

  • SebastienPouliotSebastienPouliot CAXamarin Team Xamurai

    _data.ToString() tries to create an NSString (first), using the UTF8 encoding, then a string.

    However not every byte stream can be turned into a valid string.

    Why it does not work ?

    It's likely that the content of your PDF is not UTF8 compatible (not every byte stream is) and that no valid NSString can be initialized from it.

    Why it worked before ?

    Older XI versions would have returned an invalid instance of NSString (null handle) without any warnings. That made it difficult to track errors (and generally it would fail later when you tried to use this invalid instance).

    Recent versions of XI has started to check for both alloc (some versions ago) and init* (in 7.0.6) that returns invalid instances. This way you'll know exactly when the trouble occured.

    How can this be fixed ?

    Ideally you'll fix your code to handle such cases. You're not getting a valid string value and that might affect your code (what you likely got is String.Empty).

    Alternatively you can, like the exception message said, opt-out of the new check. Doing so means you'll never be notified of such failures.

    It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.

Sign In or Register to comment.