欢迎各位兄弟 发布技术文章

这里的技术是共享的

发短信代码 sms 有大用


 

How to use an html link to open the sms app with a pre-filled body?

Everything I have read seems to indicate that sms:18005555555?body=bodyTextHere

Should work, but on the iPhone, this doesn't work. If I take out the ?body=bodyTextHere, and just use sms:phonenumber, it works.

I have seen several instances where QR codes do this through a safari link. How are they able to pre-populate the body text?

share|improve this question
 
   
I wonder if these QR scanners are opening the SMS app directly instead... –  merlincam Jun 25 '11 at 21:05
   
this still true?No method? –  Josh Cox Jul 16 '13 at 18:57
   
This is 100% possible. See my answer below for info. –  bradorego Oct 1 '13 at 21:32

It turns out this is 100% possible, though a little hacky.

If you want it to work on Android you need to use this format:

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

If you want it to work on iOS, you need this:

<a href="sms:/* phone number here */;body=/* body text here */">Link</a>

Live demo here: http://bradorego.com/test/sms.html (note the "Phone and ?body" and "Phone and ;body" should autofill both the to: field and the body text. View the source for more info)

share|improve this answer
 
   
On iOS it looks like the format is: <a href="sms:[phone number];body=body text">Link</a>. Notice no square brackets on the body. –  Rick Strahl Oct 9 '13 at 4:21 
   
Ah, whoops. The square brackets aren't part of the format - that's where you should be putting in those values. I changed them to comments so hopefully it's a little clearer –  bradorego Nov 11 '13 at 16:15
3 
Note: I haven't tested these on the latest OS versions (iOS7/the iPhone 5S/5C and Android 4.4). There are some reports that they might not be working anymore, especially with Android's switch to using Hangouts instead of a native SMS app. I'll look into it when I have the chance. –  bradorego Jan 24 at 17:04
1 
I took a look now because it sometimes was working and sometimes not, which seemed weird. My findings: If you dial a number you have at least sent one SMS before, the link works fine with "body". If it's a complete new number you're out of luck and you only get the number :( –  marczking Jan 24 at 17:36 
1 
None of these appear to work on iOS 7. –  OrangeDog Sep 5 at 9:28

We found a proposed method and tested:

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

Here are the results:

  • iPhone4 - fault (empty body of message);
  • Nokia N8 - ok (body of message - "Hello my friend", To "12345678");
  • HTC Mozart - fault (message "unsupported page" (after click on the "Send sms" link));
  • HTC Desire - fault (message "Invalid recipients(s):
    <12345678?body=Hellomyfriend>"(after click on the "Send sms" link)).

I therefore conclude it doesn't really work - with this method at least.

share|improve this answer
 
   
No body shows on the Nexus 5 –  Dai Bok Aug 21 at 9:03

The iPhone doesn't accept any message text, it will only take in the phone number. You can see this herehttps://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/uid/TP40007899-CH7-SW1

share|improve this answer
 
1 
this page is not accessible anymore –  Kirk Hammett Mar 17 at 16:40
   
edited to fix link –  Luciano Jul 3 at 13:33

For iOS 8, try this:

<a href="sms:/* phone number here */&body=/* body text here */">Link</a>

Switching the ";" with a "&" worked for me.

share|improve this answer
 
   
Yup. This works on iOS 8 –  Jay Dec 1 at 23:44
<a href="###" data-telno="13800000000" data-smscontent="hello" class="XXXXX XXXXXX XXXXXX sendsms"/>

$('.sendsms').on('click', function(){
    var p = $(this).data('telco'),
        c = $(this).data('smscontent'),
        t = ';';

    if (!ios) {
        t = '?';
    }
    location.href = 'sms:'+ p + t + c;
})
share|improve this answer
 
   
Works on most android, and IOS 5 and 6. Does not work on IOS 7. –  David C Mar 6 at 16:24

Neither Android nor iPhones currently support the body copy element in a Tap to SMS hyperlink. It can be done programmatically though,

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;

picker.recipients = [NSArray arrayWithObject:@"48151623"];  
picker.body = @"Body text.";

[self presentModalViewController:picker animated:YES];
[picker release];
share|improve this answer
 
   
Is that Objective C? How do you call that from JavaScript or use it in HTML? Sounds impossible? –  SpiderApr 13 '12 at 11:37
7 
A web solution was asked, not a native code one. –  PhistucK Dec 11 '12 at 12:38
   
Can this please be un-marked as accepted? This doesn't at all answer the question. I actually posted something that does work and does answer the question. –  bradorego Dec 23 '13 at 18:46

This works on both iPhone and Android:

sms:number:bodyText
share|improve this answer
 
1 
This does not work. –  PhistucK Dec 16 '12 at 8:37
   
Not working at all. –  Baral Aug 27 '13 at 17:49
   
Totally wrong .. –  Matteo Mosconi Dec 25 '13 at 18:17

来自 http://stackoverflow.com/questions/6480462/how-to-pre-populate-the-sms-body-text-via-an-html-link
















兼容性一直是前端工程师心中永远的痛.手机浏览器,因为基本是webkit(blink)内核当道,很多公司,不用考虑IE系的浏览器,所以感觉兼容性上的问题可能会少一些.

但是手机端,虽然出了很多工具,但是调试依然比PC端麻烦很多.而且很多坑是因为手机浏览器本身的bug,一旦出现,相应的解决方案很难根据以前的经验进行推测.只能寄希望于谷歌 + 猜.

这里记录一下我做手机端浏览器曾经踩过的坑.之所以用”曾经”,随着版本的更新,有些问题没了.

另外我(我司)没有足够的人力和物力对很多手机浏览器进行测试,我也只需要兼容iPhone 4s(老大有一个4s的玩物,不然可以从5开始!-!)以上系列和很新的安卓手机.这点和大公司没法比,群里有个百度的朋友,低版本的安卓和windows phone都要兼容…所以这篇文章当抛砖引玉了


(1)position:fixed

position:fixed

应该是反映最多的问题.但是手机屏幕很小,对于一些要重点突出的地方,还特别需要fixed在视口的某个位置,

我遇到的问题包括:

  1. 呼出系统输入法后,fixed元素位置乱飘.
  2. fixed的元素是后弹出的,点击发生击穿现象(会触发弹层下面元素的click)
  3. 涉及整页动画时,元素位置乱飘
  4. 如果祖先元素使用了transform相关的样式,fixed元素的不按照视口来定位.

解决的思路

  • 用代码,触发一下滚动(document.body.scrollTop = xx),我用这个方法解决了iPhone某版本上,弹层击穿现象.如果测试的时候,出现fixed相关问题,都可以先试着手动滚动一下屏幕,看能否修复.
  • 用absolute元素替换.基本能解决问题,但是要多些一些代码,而且有明显的卡顿感.
  • 改设计,比如不用fixed(一般很难做到),简化一些动画

现在的情况

现在的情况是,新版的手机浏览器处理fixed元素的健壮性越来越好.呼出输入法虽然还有些bug,但是比以前出现的问题要好很多,一般可以接受.除了第四条,其他问题基本都还ok.

关于第四条,把fixed元素移出去就好.如果你再body上应用transform,那么请容许我给你点根蜡烛.stackoverflow上有个相关的问题,大家可以看一下.

补充说明

有一个全屏遮罩的fixed写法,一直再用

.full-cover{
    position:fixed;
    top:0;left:0;right:0;bottom:0;
}

这个用过bootstrap的有心人应该都知道.但是还是遇到不少不知道的同学,在这啰嗦一下.

(2) 链接的打电话和发短信功能

通过设置a标签的href 为tel:电话号码sms:电话号码,可以直接使用手机的打电话和发短信功能.但是这里有两个小坑.

1.安卓某些版本的webview上,tel:会报找不到页面

2.发短信时,如果有预加内容,安卓和iPhone的schema时不同的,需要区别对待,主要?;

安卓: <a href="sms:123456789?body=sometext">点击发短信</a>
//ios7下sms的body处好像有问题,无法预制发送内容.
ios:<a href="sms:123456789;body=sometext">点击发短信</a>

注:短信功能,我并没有用在实际项目上,只是查找打电话功能的时候,碰巧搜到相关内容,然后简单的试了一下,内容主要来自stackoverflow.有不对之处,请指正

(3)touch

touch事件,之前我是自己管理的,因为一般的页面,需要去管理触摸得地方很少.不过总归要尝试一下别人的先进有法(直接使用别用的库)

1.首先一个巨无霸hammer.js.这个我没用过,因为实在太屌,各种手势.看它的github的star数,好像也比较靠谱.不过我只是简单的考察了一下,没实际用过,就不多说了.

我考察的比较多得是,zeptoappframework的触摸插件,两者都添加的tap系列和swipe系列事件,代码实现也很相似zepto touch.js,af.toucheEvent.js

项目使用的时候,我被af的注释所迷惑

//Touch events are based from zepto/touch.js
//Many modifications and enhancements made

然后就用了appframework的touch插件.但是事实表明,zepto的touch.js要靠谱一些.

这里列举几个可能会出现问题的地方,算是af.touchEvent.js的采坑记录,如果以后出现类似的情况,大家可以参考一下解决方案.

1.安卓下swipe*的方法很难触发

这是安卓的一个老问题了.谷歌一下 类似zepto android swipe的关键字,就能发现不少.很多项目中的issue都能找到这个问题,连安卓自身项目里都有相关issue(链接要翻墙)

解决方法就是在touchstarttouchmove事件中,主动调用e.preventDefault()

如果使用zepto的库,有两种方案,

//1.在监听函数中调用e.preventDefault()
$('#ele_id').on('swipeLeft',function(e){
    e.preventDefault();
    //....
})

//2.修改库的源代码,再bind touchmove事件时,添加
.on('touchmove MSPointerMove pointermove', function(e){
            if((_isPointerType = isPointerEventType(e, 'move')) &&
                !isPrimaryTouch(e)) return
            firstTouch = _isPointerType ? e : e.touches[0]
            cancelLongTap()
            touch.x2 = firstTouch.pageX
            touch.y2 = firstTouch.pageY

            deltaX += Math.abs(touch.x1 - touch.x2)
            deltaY += Math.abs(touch.y1 - touch.y2)

            //只有下面两行是要修改的代码,上面的代码帮大家定位
            //这里当时安卓系统,且是明显的横向滑动时,调用preventDefault
            if(isAndroid && deltaX > 10 && deltaX>deltaY){
                e.preventDefault()
            }
})

(:`isAndroid`变量是用来表征是不是安卓手机

var isAndroid = false;
if (navigator.userAgent.match(/Android/i)){
    isAndroid =true;
}

)//end of 注

对于安卓的swipeUp和swipeDown,就不建议去做任何干涉,因为会影响浏览器本身的滚动

2.以上下滑动为主的操作,有可能触发swipeLeft/Right事件,而不是swipeUp/Down事件.这个主要是因为native scroll的一些特性,导致算距离时竖向的距离可能会很小,而上下滑的最后,一般会有一个横向滑动的连带动作,导致deltaX>deltaY,从而判断出错.

zepto中,delta在touchmove中是累加的,所以基本没啥问题.

deltaX += Math.abs(touch.x1 - touch.x2)

af.touchEvents.js中,问题很严重.我是把取坐标时的pageX/pageY改成了clientX/clientY来解决这个问题的.供大家参考

补充说明

最后说一下,还是慎用左右滑动的设计,一是兼容性问题.二是,微信等app是手机网页主要的入口,而从左向右的滑动,很容易关闭页面.

(5)audio

看国外的资料,因为MP3的版权问题,会让大家准备ogg等格式的文件.在天朝,版权问题吗(省略几个字)…所以直接上MP3.

<audio>
    <source id="mp3source" src="yours.mp3" type="audio/mpeg">
</audio>

虽然很多资料上说,MP3格式的兼容性最好.但是这里还是有坑.当时运营同学给我一段音频,mp3格式,发现iPhone下放不了.查了很多资料,最后在一个论坛里找到,ios下MP3要满足一定条件

Try changing the MP3 to 44100Hz, 128kbs

其他参数的我没试过,但是按上面要求转换出得MP3,都可以在iPhone上使用.

(6)localStorage/sessionStorage

PC端,这两个API在低版本的IE下是没有,所以是需要用try..catch包裹的.

在移动端,我刚刚开始是不加的,所测试的手机也没问题.但是现在很多浏览器有无痕模式,这个模式下,localStorage相关的API时禁用的.所以使用时,还是要保证代码的健壮性

(7)小贴士:

最后说些细节吧,后面可能会进行补充.也希望大家多提供素材

  • 注意input的type: 比如希望输入手机号的input,应该用type=tel(希望输入纯数字的,个人觉得type=tel也是最好的选择)

  • 监听input 的oninput事件来代替onchange,这两个事件的含义,查一下就知道,不啰嗦了

  • transform的时候,加上类似translateZ(0px),有助于开启硬件加速.也看到有人说,现在不加z轴,也会开启硬件加速,这个我还没找到确切依据,不过加上不麻烦,我一般都是加上的.

  • 如果input或button的有怪异的默认样式,尝试appearance:none;(-webkit-appearance:none;)

  • 做透明的样式,background:RGBA(x,x,x,x)和ps对应的更好,opacity我主要用于动画效果

    来自  http://www.html-js.com/article/Big-search-team-columns-at-the-front-end-of-the-car


     

    [IPhone] Send SMS sample with IPhone

    分类: iOS 5328人阅读 评论(0) 收藏 举报

    Introduction

     

    I will demo some sample to present how to send SMS in iphone programming.

     

    Sample 1

     

    The easiest way is this demo.

     

    1. [[UIApplication sharedApplication] openURL: @"sms:12345678"];  

     

    1. HTML links:  
    2.   
    3. <a href="sms:" mce_href="sms:">Launch Text Application</a>   
    4. <a href="sms:1-408-555-1212" mce_href="sms:1-408-555-1212">New SMS Message</a>   
    5.   
    6. Native application URL strings:  
    7.   
    8. sms:   
    9. sms:1-408-555-1212   

     

    Sample 2

     

    We can use MessageUI Framework and MFMessageComposeViewController to send SMS.

     

    1. -(IBAction) sendInAppSMS:(id) sender  
    2. {  
    3.     MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];  
    4.     if([MFMessageComposeViewController canSendText])  
    5.     {  
    6.         controller.body = @"Hello from Mugunth";  
    7.         controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];  
    8.         controller.messageComposeDelegate = self;  
    9.         [self presentModalViewController:controller animated:YES];  
    10.     }  
    11. }  

     

    You have to handle a callback method of MFMessageComposeViewControllerDelegate so as to dismiss the modal view controller.

     

    1. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  
    2. {  
    3.     switch (result) {  
    4.         case MessageComposeResultCancelled:  
    5.             NSLog(@"Cancelled");  
    6.             break;  
    7.         case MessageComposeResultFailed:  
    8.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"  
    9.                                                            delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];  
    10.             [alert show];  
    11.             [alert release];  
    12.             break;  
    13.         case MessageComposeResultSent:  
    14.    
    15.             break;  
    16.         default:  
    17.             break;  
    18.     }  
    19.    
    20.     [self dismissModalViewControllerAnimated:YES];  
    21. }  

     

    Sample 3

     

    This is a full sample code for MFMessageComposeViewController.

     

    1. //  SMS2ViewController.h  
    2. //  SMS2  
    3. #import <UIKit/UIKit.h>  
    4. #import <MessageUI/MessageUI.h>  
    5. #import <MessageUI/MFMessageComposeViewController.h>  
    6. @interface SMS2ViewController : UIViewController <MFMessageComposeViewControllerDelegate>  
    7. {  
    8.     UILabel *message;  
    9. }  
    10. @property (nonatomic, retain) UILabel *message;  
    11. -(void)displayComposerSheet;  
    12. @end  

     

    1. //  SMS2ViewController.m  
    2. //  SMS2  
    3. #import "SMS2ViewController.h"  
    4. @implementation SMS2ViewController  
    5. @synthesize message;  
    6. /* 
    7. // The designated initializer. 
    8.  Override to perform setup that is required before the view is loaded. 
    9.  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    10.  { 
    11.     if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    12.     { 
    13.         // Custom initialization 
    14.     } 
    15.     return self; 
    16. } 
    17. */  
    18. // Implement loadView to create a view hierarchy programmatically, without using a nib.  
    19. /* 
    20. - (void)loadView 
    21. { 
    22. } 
    23. */  
    24. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
    25. - (void)viewDidLoad  
    26. {  
    27.     [super viewDidLoad];  
    28.     UIButton *smsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    29.     smsButton.frame = CGRectMake(97.0, 301.0, 125.0, 37.0);  
    30.     smsButton.adjustsImageWhenDisabled = YES;  
    31.     [smsButton setTitle:@" Send SMS" forState:UIControlStateNormal];  
    32.     [smsButton setTitleColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateNormal];  
    33.     [smsButton setTitleShadowColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateNormal];  
    34.     [smsButton addTarget:self action:@selector(displayComposerSheet) forControlEvents:UIControlEventTouchUpInside];  
    35.     message = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 360.0, 280.0, 29.0)];  
    36.     message.frame = CGRectMake(20.0, 360.0, 280.0, 29.0);  
    37.     message.adjustsFontSizeToFitWidth = YES;  
    38.     message.hidden = YES;  
    39.     message.text = @"";  
    40.     message.userInteractionEnabled = NO;  
    41.     [self.view addSubview:smsButton];  
    42.     [self.view addSubview:message];  
    43. }  
    44. -(void)displayComposerSheet  
    45. {  
    46.     MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];  
    47.     icker.messageComposeDelegate = self;  
    48.     picker.recipients = [NSArray arrayWithObject:@"123456789"];  
    49.     // your recipient number or self for testing  
    50.     picker.body = @"test from OS4";  
    51.     [self presentModalViewController:picker animated:YES];  
    52.     [picker release];  
    53.     NSLog(@"SMS fired");  
    54. }  
    55. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  
    56. {  
    57.     message.hidden = NO;/  
    58.     switch (result)  
    59.     {  
    60.         case MessageComposeResultCancelled:  
    61.             message.text = @"Result: canceled";  
    62.             NSLog(@"Result: canceled");  
    63.             break;  
    64.         case MessageComposeResultSent:  
    65.             message.text = @"Result: sent";  
    66.             NSLog(@"Result: sent");  
    67.             break;  
    68.         case MessageComposeResultFailed:  
    69.             message.text = @"Result: failed";  
    70.             NSLog(@"Result: failed");  
    71.             break;  
    72.         default:  
    73.             message.text = @"Result: not sent";  
    74.             NSLog(@"Result: not sent");  
    75.             break;  
    76.      }  
    77.      [self dismissModalViewControllerAnimated:YES];  
    78. }  
    79. // Override to allow orientations other than the default portrait orientation.  
    80. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
    81. {  
    82.     return YES;  
    83. }  
    84. - (void)didReceiveMemoryWarning  
    85. {  
    86.     // Releases the view if it doesn't have a superview.  
    87.     [super didReceiveMemoryWarning];  
    88.     // Release any cached data, images, etc that aren't in use.  
    89. }  
    90. - (void)viewDidUnload  
    91. {  
    92.     // Release any retained subviews of the main view.  
    93.     // e.g. self.myOutlet = nil;  
    94. }  
    95. - (void)dealloc  
    96. {  
    97.     [super dealloc];  
    98. }  
    99. @end 

    <a href="mailto:958186957@qq.com">发邮件</a>

唯物品评历史

关注"唯物品评历史",跟着泪痕春雨先生,读懂历史,看彻人生

打开隐藏二维码