Sending SMS in iOS with Swift -



Sending SMS in iOS with Swift -

first of all, i'm surprised not duplicate, because there tons of stackoverflow questions solve in objective-c, have yet see reply used swift.

what i'm looking code snippet in swift sends arbitrary string body of text message given phone number. essentially, i'd this apple's official documentation, in swift instead of objective-c.

i imagine isn't difficult, can done in couple of lines of code in android.

edit: i'm looking 5-20 lines of swift code, not agree broad. in java (for android), solution looks this:

package com.company.appname; import android.app.activity; import android.telephony.smsmanager; public class mainactivity extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); public static final mphonenumber = "1111111111"; public static final mmessage = "hello phone"; smsmanager.getdefault().sendtextmessage(mphonenumber, null, mmessage, null, null); } }

now android solution, , it's 11 lines. java tends much more verbose swift, uncertainty i'm asking "too broad", more don't know how utilize objective-c messagecomposer object, because documentation linked above unclear regard usage in swift.

not sure if got answer. in similar hunt , came across solution , got work.

import uikit import messageui class viewcontroller: uiviewcontroller, mfmessagecomposeviewcontrollerdelegate { @iboutlet weak var phonenumber: uitextfield! override func viewdidload() { super.viewdidload() } @ibaction func sendtext(sender: uibutton) { if (mfmessagecomposeviewcontroller.cansendtext()) { allow controller = mfmessagecomposeviewcontroller() controller.body = "message body" controller.recipients = [phonenumber.text] controller.messagecomposedelegate = self self.presentviewcontroller(controller, animated: true, completion: nil) } } func messagecomposeviewcontroller(controller: mfmessagecomposeviewcontroller!, didfinishwithresult result: messagecomposeresult) { //... handle sms screen actions self.dismissviewcontrolleranimated(true, completion: nil) } override func viewwilldisappear(animated: bool) { self.navigationcontroller?.navigationbarhidden = false } }

ios swift sms

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -