4

I currently have a button in a table view. I have a custom cell class and an update cell method.

public void UpdateCell (string subtitle2)
{               
    call.SetTitle(subtitle2, UIControlState.Normal);
    call.TouchUpInside += (object sender, EventArgs e) => {
        UIApplication.SharedApplication.OpenUrl(new NSUrl("tel:" + subtitle2));
    };          
}

However, when i run this segment of code, i get an error.

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

poupou
  • 43,413
  • 6
  • 77
  • 174
user3748957
  • 107
  • 1
  • 4
  • 11

5 Answers5

3

Try this:

public void UpdateCell (string subtitle2)
{
    //Makes a new NSUrl
    var callURL = new NSUrl("tel:" + subtitle2);

    call.SetTitle(subtitle2, UIControlState.Normal);
    call.TouchUpInside += (object sender, EventArgs e) => {
    if (UIApplication.SharedApplication.CanOpenUrl (callURL)) {
    //After checking if phone can open NSUrl, it either opens the URL or outputs to the console.

        UIApplication.SharedApplication.OpenUrl(callURL);
    } else {
    //OUTPUT to console

    Console.WriteLine("Can't make call");
    }
  };          
}

Not sure if phone calls can be simulated in the iPhone Simulator, otherwise just connect your device :-)

Emil Elkjær
  • 685
  • 1
  • 9
  • 31
3

As mentioned in other answers, NSUrl cannot handle spaces (and some other symbols) in URL string. That symbols should be encoded before passing to the NSUrl constructor. You can do the encoding with the NSString.CreateStringByAddingPercentEscapes method. Another way is using of the .NET Uri class:

new NSUrl(new Uri("tel:" + subtitle2).AbsoluteUri)

The issue you might encounter here is parsing of the schema by the Uri class. To avoid it, don't use ://, so, URL strings should look as "tel:(123) 345-7890", not as "tel://(123) 345-7890"

Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30
2

Try

UIApplication.SharedApplication.OpenUrl(new NSUrl("tel://" + subtitle2));
Jason
  • 86,222
  • 15
  • 131
  • 146
  • It's still giving me the same error Could not initialize an instance of the type 'MonoTouch.Foundation.NSUrl': the native 'initWithString:' method returned nil. It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false. – user3748957 Aug 01 '14 at 14:22
2

This error means that calling initWithString: returned nil. ObjC init* methods can return nil while .NET constructors can't (and throws).

In general ObjC returns nil on init* selectors when the input parameters are invalid. In this case I suspect your URL format is invalid.

What's the content of your subtitle2 variable ?

poupou
  • 43,413
  • 6
  • 77
  • 174
2

I resolved the same problem, after trying various ways, by finally figuring out that there should not be any spaces (instead of e.g. 1800 111 222 333, should be like 1800111222333) between numbers to make call in iOS.

number = number.Replace(" ", "");

NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", number));
return UIApplication.SharedApplication.OpenUrl(url);
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Div
  • 72
  • 6