神刀安全网

Cross-Platform Messaging for iOS, Android, and Windows

Messaging is a central component to the mobile experience and it’s not unusual for apps to want to take advantage of this in, for example, an employee directory app with employee’s contact information or a shopping application that wants to make it easy for customers to get in touch. However, these functionalities are all specific to each platform, making the code difficult to share. In this blog post, you’ll learn how to send texts, make phone calls, and send emails with a common API for iOS, Android, and Windows using the Messaging Plugin for Xamarin and Windows .

Introducing the Messaging Plugin for Xamarin and Windows

Plugins for Xamarin expose different aspects of platform-specific functionality via an API that can be called from a Portable Class Library (PCL) or Shared Project, such astaking a photo, using device geolocation , orstoring app settings. The Messaging Plugin for Xamarin and Windows allows you to send SMS messages, call a phone number, and send emails, allowing you to share even more code, increase developer productivity, and reduce costs.

Plugin functionality can be accessed via the MessagingPlugin class, with separate properties such as SmsMessenger , PhoneDialer , and EmailMessenger that expose specific functionality.

Sending a Text

To send a text message, first check if the device is capable of sending SMS messages with the SmsMessenger.CanSendSms boolean property. If this device capability is available, you can use the SmsMessenger.SendSms method, with the first parameter being the phone number and the second the SMS message to send. Note that the SMS will not send automatically—the OS requires the user to actually press the Send button to avoid abuse. Instead, the native user interface for sending SMS for that particular platform will be displayed.

var smsMessenger = MessagingPlugin.SmsMessenger; if (smsMessenger.CanSendSms)   smsMessenger.SendSms("+272193343499", "Hello from your friends at Xamarin!"); 

Making a Phone Call

Just as we did for sending a text, we must first check if the device is capable of making a phone call with the PhoneDialer.CanMakePhoneCall property. You can then use the PhoneDialer.MakePhoneCall method, which will launch the native phone app and begin calling the phone number passed in as a parameter.

// Make Phone Call var phoneCallTask = MessagingPlugin.PhoneDialer; if (phoneCallTask.CanMakePhoneCall)      phoneCallTask.MakePhoneCall("+272193343499"); 

Sending an Email

Just as we did for sending SMS messages and making phone calls, we must first check that the device supports email with the EmailMessenger.CanSendEmail property. If the device supports it, you can send an email using the EmailMessenger.SendEmail method, or use the EmailMessageBuilder class’ fluent interface to construct more complex email messages.

var emailTask = MessagingPlugin.EmailMessenger; if (emailTask.CanSendEmail) {     // Send simple e-mail to single receiver without attachments, CC, or BCC.     emailTask.SendEmail("plugins@xamarin.com", "Xamarin Messaging Plugin", "Hello from your friends at Xamarin!");       // Send a more complex email with the EmailMessageBuilder fluent interface.     var email = new EmailMessageBuilder()       .To("plugins@xamarin.com")       .Cc("plugins.cc@xamarin.com")       .Bcc(new[] { "plugins.bcc@xamarin.com", "plugins.bcc2@xamarin.com" })       .Subject("Xamarin Messaging Plugin")       .Body("Hello from your friends at Xamarin!")       .Build();       emailTask.SendEmail(email); }   

The Messaging Plugin for Xamarin and Windows also supports more complex email scenarios, such as sending an email with an HTML body or sending an attachment.

// Construct HTML email (iOS and Android only) var email = new EmailMessageBuilder()   .To("plugins@xamarin.com")   .Subject("Xamarin")   .BodyAsHtml("Hello from your friends at <b>Xamarin</b>!")   .Build();   // Construct email with attachment on Android. var email = new EmailMessageBuilder()   .To("plugins@xamarin.com")   .Subject("Xamarin Messaging Plugin")   .Body("Hello from your friends at Xamarin!")   .WithAttachment("/storage/emulated/0/Android/data/MyApp/files/Pictures/temp/IMG_20141224_114954.jpg");   .Build(); 

Conclusion

Plugins for Xamarin and Windows allow you to share more code and save even more time when working with certain pieces of platform-specific functionality, such as taking a photo, using device geolocation, or storing app settings. In this blog post, we covered how to send SMS messages, make phone calls, and send emails using the the Messaging Plugin for Xamarin and Windows . For more information, check out the documentation and sample app .

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Cross-Platform Messaging for iOS, Android, and Windows

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮