简体   繁体   中英

window.open(“sms:…”) works for android but not for ios

I have this function, which pre-fills sms text message on click. This works on Android, but on any version if iOS, it just open new empty window. Any idea, why?

 var ua = navigator.userAgent.toLowerCase();
    var url;
        
    if (ua.indexOf("iphone") > -1 || ua.indexOf("ipad") > -1)
    {
   

    var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
    var ver = [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
    if (ver[0] >= 8)
    {
    url = "sms:&body=" + encodeURIComponent("Hello guys, this is an awesome app! Check it out http://www.myurl.com/webapp/index.html");
    }
    else
    {
    url = "sms:;body=" + encodeURIComponent("Hello guys, this is an awesome app! Check it out http://www.myurlcom/webapp/index.html");
    }
    
    
    }
    else
    url = "sms:?body=" + encodeURIComponent("Hello guys, this is an awesome app! Check it out http://www.muyrl.com/webapp/index.html");

    window.open (url,'_system')

I tried with a proper HTML link, and it works well on iOS 9.

<a href="sms:+467111111111?&body=Hello my friend">Send SMS</a>

So I can see two potential issues :

  • I might not work with window.open();
  • It only works when a number is provided

Edit: I managed to make it work without a number

<a href="sms:?&body=Hello my friend">Send SMS</a>

So it seems to come from window.open()

The link below should help you test all the different combinations. Just open it from your cellphone.

What has been working for me is: href="sms://&body=TEST"

list of sms combination format here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM