locked
NSUrl not support double quotation? RRS feed

  • Question

  • User151233 posted

    Hi

    In Xamarin studio(5.8.3).

    string PrivateUrl = "http://www.google.com/?a=\"1\""; NSUrl newurl = new NSUrl (PrivateUrl);

    It will get error:

    Could not initialize an instance of the type 'MonoMac.Foundation.NSUrl': the native 'initWithString:' method returned nil.\nIt is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false."

    Is NSUrl not support double quotation?

    Wednesday, May 4, 2016 3:16 AM

Answers

  • User35201 posted

    From the docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLClass/#//appleref/occ/instm/NSURL/initWithString:

    An NSURL object initialized with URLString. If the URL string was malformed, returns nil.

    Our binding is less than helpful in this area, since you get the error instead of an exception. I've filed a "we should make this better" bug here:

    https://bugzilla.xamarin.com/show_bug.cgi?id=40861

    It appear that validating that a string is a valid url is non-trivial:

    http://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 4, 2016 2:21 PM

All replies

  • User35201 posted

    From the docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLClass/#//appleref/occ/instm/NSURL/initWithString:

    An NSURL object initialized with URLString. If the URL string was malformed, returns nil.

    Our binding is less than helpful in this area, since you get the error instead of an exception. I've filed a "we should make this better" bug here:

    https://bugzilla.xamarin.com/show_bug.cgi?id=40861

    It appear that validating that a string is a valid url is non-trivial:

    http://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 4, 2016 2:21 PM
  • User35201 posted

    I took a look at this further this morning. We have a second set of bindings, the static ones like:

    NSUrl.FromString ()

    and those return null if the string is invalid.

    Thursday, August 25, 2016 4:04 PM
  • User384903 posted

    Did this ever get actioned?

    Thursday, March 5, 2020 4:17 PM
  • User35201 posted

    The bugzilla bug notes:

    We have a second set of bindings, the static versions like NSUrl.FromString that return null if the input is invalid.

    If you are unsure your URL is correctly formatted, then you should use that instead of the constructor. C# constructors have no way to return errors beyond throwing an exceptions (what we're doing there).

    Monday, March 9, 2020 3:53 PM