ios - Send a text message directly via an IBAction (Objective-C) -
how send text message (using mfmessagecomposeviewcontroller
) directly via ibaction? like, when button pressed, text message sent preset number, , no keyboard shows or anything. alert saying "sms sent successfully," example.
all coding done, except "direct sending-function".
well, can't technically "auto-send" message, because require user confirmation go through.
you can, however, set message's contents , recipient(s) using mfmessagecomposeviewcontroller
(quite mouthful) , display dialog require 1 tap send.
to have access dialog, you'll have #import <messageui/messageui.h>
, add mfmessagecomposeviewcontrollerdelegate
view controller declaration in header file.
then, can write ibaction
. first, want check device can send messages text content using cansendtext
. then, you'll create view controller, populate data, , present dialog.
- (ibaction)sendmessage:(uibutton *)sender { if([mfmessagecomposeviewcontroller cansendtext]) { mfmessagecomposeviewcontroller *messagecontroller = [[mfmessagecomposeviewcontroller alloc] init]; // create message vc messagecontroller.messagecomposedelegate = self; // set delegate current instance nsmutablearray *recipients = [[nsmutablearray alloc] init]; // create array hold recipients [recipients addobject:@"555-555-5555"]; // append example phone number array messagecontroller.recipients = recipients; // set recipients of message created array messagecontroller.body = @"example message"; // set initial text example message dispatch_async(dispatch_get_main_queue(), ^{ // present vc when possible [self presentviewcontroller:messagecontroller animated:yes completion:null]; }); } }
one last thing: have implement delegate method tell message view controller dismiss when user presses "cancel" in send dialog:
- (void)messagecomposeviewcontroller:(mfmessagecomposeviewcontroller *)controller didfinishwithresult:(messagecomposeresult)result { [self dismissviewcontrolleranimated:yes completion:null]; }
Comments
Post a Comment