When sending an SMS from iPad, the country code of the recipient is automatically changed.

Hello.

My development environment is as follows.


iPhone 7 - Ver iOS14.5.1

iPad 8th- Ver iPadOS14.5.1

Microsoft Visual Studio Enterprise 2019 - Ver 16.9.5

Xamarin - Ver 16.9.000.292

Xamarin.iOS and Xamarin.Mac.SDK - Ver 14.16.0.5


https://support.apple.com/en-us/HT208386

Using the above article as a reference, I linked my iPhone and iPad with an Apple ID and sent SMS from an iPad application created with Xamarin, but the country code I specified as the recipient was automatically changed to a different number than the one I wanted.

The source code is as follows.

ISMS.cs

    class CSMSiOS : ISMS 
    {
        public void SendSMS(string Dial,string WideDial)
        {
            if(!MFMessageComposeViewController.CanSendText) //SMS function available
            {
                Console.WriteLine("SMS function not available");
                return;
            }
            
            MFMessageComposeViewController smsController = new MFMessageComposeViewController();

            if (!string.IsNullOrWhiteSpace(Dial))
            {
                string[] recipients = Dial.Split(';');
                if (recipients.Length > 0)
                    smsController.Recipients = recipients;
            }

            smsController.Body = WideDial+ "Message";
            smsController.SetEditing(false, true);
            

            PresentMessageComposeViewController(smsController);

            Console.WriteLine("SMS sending page");
        }

        private void PresentMessageComposeViewController(MFMessageComposeViewController smsController)
        {
            EventHandler<MFMessageComposeResultEventArgs> handler = null;
            handler = (sender, args) =>
            {
                smsController.Finished -= handler;

                if (!(sender is UIViewController uiViewController))
                {
                    throw new ArgumentException("sender");
                }

                uiViewController.DismissViewController(true, () => { });
            };

            smsController.Finished += handler;
            ClsSMSPageView.PresentUsingRootViewController(smsController);
        }
    }

ClsSMSPageView.Xaml.cs

static class ClsSMSPageView
    {

        public static void PresentUsingRootViewController(this UIViewController controller)
        {
            if (controller == null)
                throw new ArgumentNullException(nameof(controller));

            var visibleViewController = GetVisibleViewController(null);
            if (visibleViewController == null)
            {
                throw new InvalidOperationException("Could not find a visible UIViewController on the Window hierarchy");
            }
            visibleViewController.PresentViewController(controller, true, () => { });
        }

        private static UIViewController GetVisibleViewController(UIViewController controller)
        {
            UIViewController viewController = null;
            UIWindow window = UIApplication.SharedApplication.KeyWindow;

            if (window == null)
                throw new InvalidOperationException("There's no current active window");

            if (window.WindowLevel == UIWindowLevel.Normal)
                viewController = window.RootViewController;

            if (viewController == null)
            {
                window = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel)
                                      .FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);
                if (window == null)
                    throw new InvalidOperationException("Could not find current view controller");
                else
                    viewController = window.RootViewController;
            }

            while (viewController.PresentedViewController != null)
                viewController = viewController.PresentedViewController;

            return viewController;
        }
    }

For example, if you specify [8081234567], [+0] would normally be specified as the country code and it would be [+0 8081234567], but somehow [+44] would be specified as the country code and it would be [+44 8081234567].

I welcome any and all opinions and advice you may have. Thank you very much.

Answered by SoftDoKK in 676425022

I found out that I could send the message to my iPhone. What I tried to do was to change the [+] to [international prefix number].

For example, I changed it to [011 0 8081234567].

Why do I need to specify an international prefix number when I'm sending domestically...?

Anyway, I can now send iMessage (blue message) to iPhone, and SMS (green message) to Android.

Thank you for your help!

I noticed when a country code is missing the device adds the default country code for the device in use. If the provided phone number does not include it, make sure to request it.

Accepted Answer

I found out that I could send the message to my iPhone. What I tried to do was to change the [+] to [international prefix number].

For example, I changed it to [011 0 8081234567].

Why do I need to specify an international prefix number when I'm sending domestically...?

Anyway, I can now send iMessage (blue message) to iPhone, and SMS (green message) to Android.

Thank you for your help!

When sending an SMS from iPad, the country code of the recipient is automatically changed.
 
 
Q