Make phonecall ios xamarin

I currently have a button in the form of a table. I have my own cell class and 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 code segment, I get an error message.

Failed to initialize instance of type 'MonoTouch.Foundation.NSUrl': native method initWithString: 'returned. This condition can be ignored by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure - false.

+4
source share
5 answers

Try the following:

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 iPhone Simulator, otherwise just plug in your device :-)

+3

, NSUrl ( ) URL. , NSUrl. NSString.CreateStringByAddingPercentEscapes. .NET Uri:

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

, , - Uri. , ://, URL "tel:(123) 345-7890", "tel://(123) 345-7890"

+3

Try

UIApplication.SharedApplication.OpenUrl(new NSUrl("tel://" + subtitle2));
+2

, initWithString: nil. ObjC init* nil, .NET ( ).

ObjC nil init*, . , URL .

subtitle2?

+2

, , , iOS ( 1800 111 222 333, 1800111222333).

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

NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", number));
return UIApplication.SharedApplication.OpenUrl(url);
+2

All Articles