Sending SMS to dynamic Telephone number - iphone

I have an app which sends an sms and then calls a dynamic telephone number, here is the code:
#pragma mark - PictureListMainTableCellDelegate methods
-(void)pictureListMainTableCell:(PictureListMainTableCell *)cell wantsToCallNumber:(NSString *)number
{
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = #"Your Message here";
[messageComposer setBody:message];
messageComposer.recipients = [NSArray arrayWithObjects:#"0003233", nil];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
NSLog(#"Texting telephone number [%#]", messageComposer);
NSString *urlString = [NSString stringWithFormat:#"tel://%#", number];
NSLog(#"calling telephone number [%#]", number);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
NSLog(#"%#", [self deviceLocation]);
}
the telephone call works but not the sms, can anyone help?

First check if your device can send SMS messages by:
if ( [MFMessageComposeViewController canSendText] ) {
// build your text message like you posted
}
else {
// handle appropriately
// no point in trying to display the compose view controller
}
Whether this solves your specific problem, I am not sure, since I am just following Apple's docs for this.

just replace this line in your method and you'll be able to send dynamic messages
messageComposer.recipients = [NSArray arrayWithObjects:number,nil];
and you can send emails and messages automatically without pressing send button but in that case there is a chance of rejecting of your app from the apple app store as it is not under their legal process so they'll reject your application.
and you should check condition for
if([MFMessageComposeViewController canSendText])
otherwise in iPad and iPod your application might meet crash.

Related

MFMessageComposeViewController shows blank white screen in iOS7

there's a problem when I try to send a large recipients list (e.g more than 40) using MFMessageComposeViewController. In iOS7, it will show a blank white screen for 20s or more before displaying the SMS compose view. This does not occur for iOS5 and iOS6.
Below is the existing code that I'm using,
NSArray * recipients;
for (NSIndexPath * index in selectedRows)
{
NSDictionary *dictionary = [data objectAtIndex:index.row];
NSString *phoneNum = [dictionary objectForKey:#"contactNum"];
recipients = [NSArray arrayWithObjects:phoneNum, nil]];
}
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self ;
controller.wantsFullScreenLayout = NO;
[(id)_delegate presentModalViewController:controller animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Below are the output message that I received when I try to send to many.
timed out waiting for fence barrier from com.apple.mobilesms.compose
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
I had a similar problem where I got the message in the console "
timed out waiting for fence barrier from com.apple.mobilesms.compose
The problem was that I tried in my app to add the number as a string, but because of the localization request, I have put it in a form:NSArray *recipents = #[NSLocalizedString(#"numberForRegistrationViaSms", #"")];
and
[messageController setRecipients:#[recipents]];
That didn't work for some reason but, when I put just simply, [messageController setRecipients:#[#"123456789"]];, the SMS composer appears without any problem.
I to had the same problem then figured
controller.recipients = // should always be an array of strings.
Make sure the phone numbers you send to controller.recipients are NSString.
I think I maybe resolve this:
//must initiate new NSString object
NSString *phoneStr = [NSString stringWithFormat:#"%#",... ];
MFMessageComposeViewController *aCtrl = [[MFMessageComposeViewController alloc] init];
aCtrl.recipients = #[phoneStr];
...
Then OK.
I had same problem.
timed out waiting for fence barrier from com.apple.mobilesms.compose
Message Cancelled
Instead of this:
NSString *phoneNumber = #"888888888";
[picker setRecipients:#[phoneNumber]];
Try this:
NSString *phoneNumber = person.phoneNumber;
[picker setRecipients:#[[NSString stringWithFormat:#"%#", phoneNumber]]];
This worked for me.
The problem has been resolved in iOS 7.0.3.

Send text then call Contact iPhone App

I have an emergency call app. When the user presses on the contact it calls them. Then the user has to return to the app to send the automated SMS. However I would like it that when pressed on the contact the user is taken to the message framework and once send is pressed it then calls the person. Here is the code so far.
- (NSString *)deviceLocation {
return [NSString stringWithFormat:#"Hi, you have been contacted as there has been an emergancy. Here is the location of the caller: Latitude: %f Longitude: %f . Please call for help to that location. From Kiddy Call the iPhone app. ", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}
#pragma mark - PictureListMainTableCellDelegate methods
-(void)pictureListMainTableCell:(PictureListMainTableCell *)cell wantsToCallNumber:(NSString *)number
{
if([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = (#"%#", [self deviceLocation]);
[messageComposer setBody:message];
messageComposer.recipients = [NSArray arrayWithObjects:number , nil];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
NSLog(#"Texting telephone number [%#]", messageComposer);
}
NSString *urlString = [NSString stringWithFormat:#"tel://%#", number];
NSLog(#"calling telephone number [%#]", number);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
NSLog(#"%#", [self deviceLocation]);
}
Any solutions to this will be greatly appreciated.
You're doing:
messageComposer.messageComposeDelegate = self;
just define:
messageComposeViewController:didFinishWithResult
It will be called when someone finished editing text and you can get result out of second parameter.
MessageComposeResultCancelled,
MessageComposeResultSent,
MessageComposeResultFailed
And from this callback you can make a call.
To make a call use:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:#"123123123"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
or tel:// for calling without prompting.

MFMailComposeViewController issue with From address

I send mail using following code:
- (IBAction)sendMailPressed:(id)sender
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:self.strMailSubject];
// Attach pdf to the email
NSURL *urlToLoad = [[NSBundle mainBundle] URLForResource:self.strSorce withExtension:self.strExtention];
NSData *myData = [NSData dataWithContentsOfURL:urlToLoad];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:[NSString stringWithFormat:#"%#.%#", self.strSorce, self.strExtention]];
// [self presentModalViewController:picker animated:YES];
[[Singleton sharedInstance] pushModalViewController:picker whereCurrentController:self animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
[[Singleton sharedInstance] popModalViewControllerAnimated:YES];
}
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = [NSString stringWithFormat:#"mailto:&subject=%#", self.strMailSubject];
NSString *email = [NSString stringWithFormat:#"%#", recipients];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
And I get this view:
But I need this view (please, pay attention only to From string, not to the image).
I don't know how to make From string appear
You can use setCcRecipients: for this in your code.
just add this line in your displayComposerSheet method after create MFMailComposeViewController and also just set the NSArray with Recipients names...
NSString *strFullName = [NSString stringWithFormat:#"From : %#",yourSenderEmailAddress];/// here write your email address which dynamic get from your string
NSArray *arrRecipients = [[NSArray alloc]initWithObjects:strFullName, nil];
[picker setCcRecipients:arrRecipients];
I Update code with one example also...
Also Vishal's Answer is also right, i just complete it..
If more than one mail account have been added, the from field will be visible in order to select the sender from multiple accounts.
There isn't any way to set the From: field of MFMailComposeViewController programmatically, probably for security reasons. The owner of the device has control over what accounts are registered on the device and therefore what the From: field is allowed to contain.
The From: field only shows if multiple email accounts have been configured on the device; that field then allows the user to pick which account to send from. If only one account exists on the device, it is not shown (it "goes without saying" which account is being used).
You can use "setPreferredSendingEmailAddress" for iOS 11 and newer.

Email issue: want to send email without additional window

I want to send email without showing MFMailComposeViewController. I just want to send email to some sequence of emails (so user should see only my spinner, not MFMailComposeViewController with send button).
The only way to send emails I know is: (but it's not I want)
-(void)showMessageController:(id)sender
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[appDelegate setNavigationBarTextured:YES navigationBar:picker.navigationBar];
picker.mailComposeDelegate = self;
[picker setSubject:#"Please, look at my photo!"];
// Attach an image to the email (mydata - data of the image)
[picker addAttachmentData:myData mimeType:#"image/png" fileName:#"photo"];
// Fill out the email body text
NSString *emailBody = #"Hello!\nPlease, have a look at my photo!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
// NSString *recipients = #"mailto:first#example.com?cc=second#example.com,third#example.com&subject=Hello from California!";
NSString *recipients = #"mailto:subject=Please, look at my photo!";
NSString *body = #"&body=Hello!\nPlease, have a look at my photo!";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
How can I send email without additional screen of MFMailComposeViewController?
Thank you in advance!
You can do it by Web services. Write a web service that takes message for Email body and receiver's email address, subject etc as parameters and sends mail from backend.
You can create PHP script to send email and can call the php service call with To, Message, Subject passed from device,
There is no API provided by Apple that would let you send emails without user interaction.

Iphone sms app issue

iam making an application where i have to send a same body sms to my contact list . I m using
Class messageClass = (NSClassFromString(#"MFMessageComposeViewController"));
if (messageClass != nil)
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
if ([appdelegate.NumberArray count] > 0) {
NSString *aa = [appdelegate.NumberArray objectAtIndex:0];
NSMutableArray *myArray = [aa componentsSeparatedByString:#","];
NSString *n = [myArray objectAtIndex:0];
NSString *m=[myArray objectAtIndex:1];
NSString *new = [appdelegate.globalmsg stringByReplacingOccurrencesOfString: #"forgot" withString:n];
controller.body = new;
controller.recipients= [NSArray arrayWithObject:m];
controller.messageComposeDelegate = self;
//if([appdelegate.NumberArray count] ==2 ){
[self presentModalViewController:controller animated:YES];
im taking out my contact one by one from my mutable array . the problem is that my program sending an sms one at a time, not to the complete list. and if i wanted to send sms to next person i have to click send button again. is there any way i can send sms to everyone without clicking that send button again again?
u can check this app .... http://itunes.apple.com/us/app/automatic-custom-sms/id409247779 in this app last page comes automatically and send everyone from your contact list. ??
Ok i fix it. i use a local variable an initialize it in view did load with value 0 and then in view did appear update it to 1 and make making present model view appear till my array finished.