user3841879
user3841879

Reputation: 659

Xamarin Forms Labs - Texting

I am using the xamarin forms labs package at the moment and I was wondering how I can use the phone feature from xamarin forms labs and send a text to the specified number

Upvotes: 1

Views: 1311

Answers (2)

SKall
SKall

Reputation: 5234

The iOS SMS sending was not tested and it previously wasn't working correctly as the URI method doesn't allow message body. If you get the latest source it has now been updated and tested (finished the implementation last night). You can run the sample project and test sending your current GPS location with E-mail and SMS.

https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/ViewModel/GeolocatorViewModel.cs

This is the SMS code for iOS:

https://github.com/XForms/Xamarin-Forms-Labs/blob/master/src/Xamarin.Forms.Labs/Xamarin.Forms.Labs.iOS/Services/PhoneService.cs

    public void SendSMS(string to, string body)
    {
        if (this.CanSendSMS)
        {
            var smsController = new MFMessageComposeViewController()
            {
                Body = body,
                Recipients = new[] { to }
            };

            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(smsController, true, null);
        }
    }

Upvotes: 2

Derek Beattie
Derek Beattie

Reputation: 9478

In IPhoneService there is:

 void SendSMS(string to, string body);

iOS impl:

public void SendSMS(string to, string body)
        {
            UIApplication.SharedApplication.OpenUrl(new MonoTouch.Foundation.NSUrl("sms:" + to));
        }

Android impl:

 public void SendSMS(string to, string body)
        {
            SmsManager.Default.SendTextMessage(to, null, body, null, null);
        }

WP impl:

public void SendSMS(string to, string body)
        {
            new SmsComposeTask{ To = to, Body = body }.Show();
        }

To call it:

var device = Resolver.Resolve<IDevice> ();
device.PhoneService.SendSMS("5015551212", "a test sms message");

Upvotes: 6

Related Questions