Texting contacts within an app - ios

I've developed an app that allows the user to text contacts that they add in the app. The problem I'm having is it seems that if the user already exists in the person's native iOS address book, the text will send no problem. But if the contact exists only within the app, the text will not go through. Has anyone else experienced something like this before?
EDIT: Code below
if([MFMessageComposeViewController canSendText])
{
controller.body = #"";
controller.recipients = arrayContactMobileStrings;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Message not sent", #"") message:NSLocalizedString(#"Error sending message", #"")
delegate:self cancelButtonTitle:NSLocalizedString(#"OK", #"") otherButtonTitles: nil];
switch (result)
{
case MessageComposeResultCancelled:
[alert show];
break;
case MessageComposeResultFailed:
[alert show];
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Related

Mail Controller in iOS keeps on getting "cancelled"

Only the email-subject is getting set to "Test mail" and recipients remain empty. MailController opens for a while and gives the alert as "Message Cancelled".
Anyone please help me out!
- (IBAction)sendEmail:(id)sender { //This is a button to send E-mail
mailController=[[MFMailComposeViewController alloc]init];
NSString *emailBody = #"Test mail from Fortune";
[mailController setToRecipients:[NSArray arrayWithObjects:#"hi#fortune.com",#"hello#fortune.com", nil]];
[mailController setCcRecipients:#[#"gthg65#gmail.com"]];
[mailController setBccRecipients:#[#"resumes#fortune.com"]];
[mailController setMessageBody:emailBody isHTML:NO];
[mailController setSubject:#"Test mail "];
mailController.mailComposeDelegate=self;
[self presentViewController:mailController animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *messageResult;
if (error!=nil)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Mail Error" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else{
switch (result) {
case MFMailComposeResultCancelled:
messageResult=#"Mail Cancelled";
break;
case MFMailComposeResultFailed:
messageResult=#"Mail Failed";
break;
case MFMailComposeResultSaved:
messageResult=#"Mail Saved";
break;
case MFMailComposeResultSent:
messageResult=#"Mail Sent";
break;
default:
break;
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Mail Result" message:messageResult delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Works on a test device. Also Apple recently changed their developer account memberships and you can register for a free apple developer account and run the app on a test device that way.

Can you send sms without using the native ios application?

I have seen several tutorials to send SMS messages but all open the native iOS message application and what I want is that my application send it without changing application that is sending something invisible to the user.
This is the code I'm using but opens the native application:
#pragma mark - IBAction methods
-(IBAction)sendSMS:(id)sender {
//check if the device can send text messages
if(![MFMessageComposeViewController canSendText]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Advertencia" message:#"Su dispositivo no permite el envio de mensajes SMS" delegate:nil cancelButtonTitle:#"Aceptar" otherButtonTitles:nil];
[alert show];
return;
}
//set receipients - telefonos
NSArray *recipients = [NSArray arrayWithObjects:textNumero.text, nil];
NSLog(#"telefono: %#",recipients);
//set message text
NSString * message = textMensaje.text;
NSLog(#"mensaje: %#",recipients);
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];
// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
}
#pragma mark - MFMailComposeViewControllerDelegate methods
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Advertencia" message:#"Ha ocurrido un error en el envio del mensaje" delegate:nil cancelButtonTitle:#"Aceptar" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Using MFMessageComposeViewController to Send to an Array of Numbers Individually

Currently I am trying to send sms from a users phone through an app.
The idea is that the user selects the recipients of the message and then one by one the message is sent to each recipient.
Currently this is how I am doing it after a button is clicked this happens
if (self.abNumbersSelected.count > 0){
for(NSString *phoneNumber in self.abNumbersSelected)
{
if(self.currentController)
[self.currentController dismissViewControllerAnimated:YES completion:^{[self sendInviteMessage:phoneNumber];}
];
else
[self sendInviteMessage:phoneNumber];// message that invites selected message
}
}
abNumbersSelected is an array of numbers that the user has picked the sendInviteMessage is here:
- (void)sendInviteMessage:(NSString*)invitingNumber
{
//Invites the numbers using sms
//First checks if it duplicates it
if ([self checkDuplicate:invitingNumber]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Invalid Phone Number" message:#"The phone number you entered is not valid." delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
self.phoneField.text = #"";
}
//if not a duplicate then it sends the message
else {
if (SharedSessionManager.authToken) {
NSString* currentUserName = #"";
if (SharedSessionManager.currentUser.firstName && SharedSessionManager.currentUser.lastName) {
currentUserName = [NSString stringWithFormat:#"%# %#", SharedSessionManager.currentUser.firstName, SharedSessionManager.currentUser.lastName];
}
else if (SharedSessionManager.currentUser.firstName) {
currentUserName = SharedSessionManager.currentUser.firstName;
}
if (!customLoadingView.isLoading)
[customLoadingView beginLoading];
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
NSString *message = [NSString stringWithFormat:#"%# invited you to join Friendli - a mobile, location based social network. Join now at friendli.co",currentUserName];
controller.body = message;
controller.recipients = [NSArray arrayWithObjects:invitingNumber, nil];
controller.messageComposeDelegate = self;
controller.disableUserAttachments;
controller.title = #"Invite Friend";
self.currentController = controller;
[self presentModalViewController:self.currentController animated:YES];
}
}
}
}
Currently no message shows up to be sent.
Also here is the messageComposeViewController: method
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
In the log this is what is said when 2 recipients are picked and the button to trigger the MFMessageComposeViewController to be created is clicked by the user
2015-06-05 13:29:10:521 Friendli[241:807] [self.abNumbersSelected count]: 2
2015-06-05 13:29:11:638 Friendli[241:807] Reading auth token from keychain. (friendli.116.authtoken)
2015-06-05 13:29:11:643 Friendli[241:807] Reading auth token from keychain. (friendli.116.authtoken)
2015-06-05 13:29:12.869 Friendli[241:10525] Remote compose controller timed out (NO)!
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{
if (self.abNumbersSelected.count > 0 && self.multipleRecipients) {
[self sendInviteMessage:[self.abNumbersSelected objectAtIndex:0]];
}
}];
}
In the didFinish method for the MFMessageComposeController in the dismiss statement using the if statement in the block of code the code will continue to create more MFMessageComposeViewControllers till it is done.

Mail Compose not dismiss when click cancel button

I have this code who send a e-mail to users, Im using MFMailCompose:
.h file
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface TestViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
#property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;
.m file
#synthesize mailComposeDelegate
-(void)sendEmail:(NSString*)valor{
//Valor receive the email
NSString *corpoMensagem = #"No have body yet...";
// From within your active view controller
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;// Required to invoke mailComposeController when send
[mailCont setSubject:#"FazerBem - Recibo de Compra"];
[mailCont setToRecipients:[NSArray arrayWithObject:valor]];
[mailCont setMessageBody:corpoMensagem isHTML:YES];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if (error){
NSString *errorTitle = #"Erro to send";
NSString *errorDescription = [error localizedDescription];
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[errorView show];
}
[self becomeFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
}
when I click the send button he can send the email to the recipient and can close the screen, now when I click the cancel button, it opens 2 more options 'delete draft' and 'save draft' when I click on one of the app crashes and returns me the following error:
[TestViewController respondsToSelector:]: message sent to deallocated instance 0x16b3b470
How I can solve this problem?
Use Switch case to for performing actions:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
UIAlertView *alert;
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
alert = [[UIAlertView alloc] initWithTitle:#"Draft Saved" message:#"Composed Mail is saved in draft." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
case MFMailComposeResultSent:
alert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"You have successfully referred your friends." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
case MFMailComposeResultFailed:
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"Sorry! Failed to send." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:nil];
}
Remove this line:
[self becomeFirstResponder];
That should fix your problem.
If above solution didn't work for you as it was for me, here, instead of self use instance of MFMailComposeViewController:
Instead Of
[self dismissViewControllerAnimated:YES completion:nil];
Use
[mailComposer dismissViewControllerAnimated:YES completion:nil];

Multi touch is not working after facebook share

In my project i am having 5 levels. In First and second level i can able to move and shoot at a time. When completing 2nd level i want to share it with Facebook. After facebook share i cant able to move and shoot at a time. Only one process is working(Either shoot or move).
What i want to do for solve this problem.
My coding is here:
{
UIViewController*v=[[UIViewController alloc]init];
[[[CCDirector sharedDirector]openGLView]addSubview:v.view];
SLComposeViewController*mySLComposerSheet;
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
NSString *strg=[NSString stringWithFormat:#" "];
NSString *bodyStr=[NSString stringWithFormat:#"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"!\n;
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:bodyStr]; //the message you want to post
[mySLComposerSheet addImage:[UIImage imageNamed:#"my.jpg"]]; //an image you could post
NSURL *url = [NSURL URLWithString:#"https:example.com"];
[mySLComposerSheet addURL:url];
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output,*head;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"FREE levels NOT unlocked";
head=#"Facebook Share Unsuccessfull";
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:head message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
[self reload];
break;
case SLComposeViewControllerResultDone:
output = #"Extra FREE levels successfully unlocked";
head=#"Successfull";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:head message:output delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
alert.tag=22;
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
You are presenting the ViewController mySLComposerSheet
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
but there is no dismiss... statement in your completionHandler
[self dismissViewControllerAnimated:YES completion:nil];
see the post https://stackoverflow.com/a/12651085/2787026

Resources