Open SMS View in iOS Using Appcelerator’s Titanium Platform

I was struggling with this for a while. I saw a few different syntaxes that were used but only one worked for me:
**UPDATE: added code to check if user has authorized app to access contacts

if(Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_AUTHORIZED){ 
     Ti.Contacts.showContacts({
          animated:true,
          selectedProperty:function(e){
               Ti.API.info(JSON.stringify(e));
	       var tempNum = e.value.replace(/[^0-9.]/g, "");
	       Ti.API.info('new number: ' + tempNum);
               Ti.Platform.openURL('sms://' + tempNum + '&body=' + encodeURIComponent('Check out my app! http://soundrapp.com'));
          },
          fields:["phone"]
     });
}else if(Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_UNKNOWN){
     Ti.Contacts.requestAuthorization(function(e){
          if(e.success){
               Ti.Contacts.showContacts({
                    animated:true,
                    selectedProperty:function(e){
                         Ti.API.info(JSON.stringify(e));
	                 var tempNum = e.value.replace(/[^0-9.]/g, "");
	                 Ti.API.info('new number: ' + tempNum);
                         Ti.Platform.openURL('sms://' + tempNum + '&body=' + encodeURIComponent('Check out my app! http://soundrapp.com'));
                    },
                    fields:["phone"]
               });
    
          }else
          {
               alert('authorization error message');
          }
     });
}

What I’m doing is allowing a user to share an app via text message. I even tried to put the above code in a function to call when the phone number was selected, but it always failed. I put in a regex cleanser for the number just to be sure it’s a valid phone number.

Tested on: Appcelerator Titanium SDK 3.4.0 and using iOS 8

Leave a Reply

Your email address will not be published. Required fields are marked *