I've just tested the "new" Xamarin forms cross platform plugin. It works as it should and you get navigation to the phones messaging app when you are about to send a message. As I could read it is restricted by the OS to not let you send sms from within the app directly, is this the same if you go all native to the NS api on iOS? A solution if you don't want to have the sms app enabled is to have a backend/api, but then it also requires more than just the app and a separate sms gateway. So my question is to know if there is a custom workaround to just send a sms if the user allowed it, directly from the app?
问题:
回答1:
In Android, you can use SmsManager
class to send an Sms. There is an example in Xamarin's Documentation on how to do it.You need to add SEND_SMS
permission. Here is the code snippet from the site
SmsManager.Default.SendTextMessage ("1234567890", null,"Hello from Xamarin.Android", null, null);
iOS does not allow us to Send an sms directly using an API alone. This is a restriction imposed by Apple. Recommended approach is use the default application installed in the iPhone. Best approach we can take to send message from within the App is to use the MFMessageComposeViewController
class. With this we can present a UI (provided by iOS) within the App in which user can populate "Body" and "Recipient" fields and send Sms.
This blog post has an example of how to use MFMessageComposeViewController
in Xamarin
回答2:
Starting from iOS 4 you can use MFMessageComposeViewController
which shows interface for sending sms messages inside your app. You can use PhoneService class from Xamarin-Forms-Labs project or Xamarin.Plugins pcl library.
These projects also provide the same functionality for sending sms messages in Android.