I am using xcode 4.2 and i am developing an iphone APP , part of this app is sending SMSs
here is the code that I typed:
-(IBAction)SMSbutton{
MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
sms.body= [NSString stringWithFormat: #"text"];
sms.messageComposeDelegate = self;
[self presentModalViewController:sms animated:YES];
}
I can open the SMS application and send SMS but the problem is whether I click on the "cancel" or the "send" button the message application does not go away . how to do that ?
thanks
Set your calling class to be a delegate of the message composer, then catch the cancel and send events to dismiss the modal view controller as needed.
Set your header file to adhere to the MFMessageComposeViewControllerDelegate, and when you initialize the composer, set the message delegate to self:
MFMessageComposeViewController *george = [MFMessageComposeViewController alloc] init];
george.messageComposeDelegate = self;
Then implement the delegate method...easiest way is:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
}
...but you can catch and deal with errors and events as necessary.
Implement messageComposeViewController:didFinishWithResult: in your delegate"
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
if(result == MessageComposeResultCancelled) {
//Message cancelled
} else if(result == MessageComposeResultSent) {
//Message sent
}
[self dismissModalViewControllerAnimated:YES];
}
Have you tried dismissViewControllerAnimated:completion: or dismissModalViewControllerAnimated:?
[self dismissModalViewControllerAnimated: YES];
Related
In my application while user go to the next view controller I am showing indicator with cancel button. On cancel button click i m setting value -2 for one variable like appDel.Val = -2; And before push to next controller i m checking that if appDel.Val = -2; then return. So no need further process.
this is working fine with iOS6 devices. When user tap on cancel in UIAlertView Delegate method for Alert click calls immediately and value set for appDel.Val=-2. So it will return without any further process.
Bit With iOS7 it creating issue like when user tap on cancel in alert, alertview delegate method is calling after completing current process for push view controller. So appDel.Val=-2 will be set after push controller from alert click method as alert delegate method is not calling immediately same like iOS 6
Can any one please suggest, How can i fix this for iOS 7?
Here is my code.
-(void)goToNextViewController:(NSDictionary*)outputDictionary
{
myViewController *mtvc=[appDel.myDict valueForKey:[outputDictionary valueForKey:#"id"]];
if (mtvc) {
[appDel showindicator:#"Please Wait..."];
}else{
[appDel showindicator:#"Connecting..."];
}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
self.rootDict = [[NSMutableDictionary alloc] initWithDictionary:outputDictionary];
appDel.activeId = [outputDictionary valueForKey:#"id"];
if(!mtvc)
{
myViewController *mtvc = [[myViewController alloc]initWithNibName:#"myViewController" bundle:nil];
appDel.tempViewController=mtvc;
appDel.tempViewController.isVeryFirst = YES;
[mtvc release];
}
else
{
appDel.tempViewController=mtvc;
appDel.tempViewController.isVeryFirst = NO;
#try {
[self.navigationController pushViewController: mtvc animated: YES];
}
#catch (NSException * e) {
NSLog(#"Exception: %#", e);
}
[appDel performDismiss];
return;
}
if (appDel.Val==-2)
{
[appDel.newConnections removeObjectForKey:appDel.activeId];
appDel.tempViewController.isNewConnection = NO;
appDel.Val = 0;
[appDel performDismiss];
return;
}
[appDel.newConnections setObject:appDel.tempViewController forKey:[outputDictionary valueForKey:#"id"]];
[self.navigationController pushViewController: appDel.tempViewController animated: YES];
[appDel performDismiss];
}
Alert click
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alert.tag==654)
{
self.Val = -2;
[baseAlert dismissWithClickedButtonIndex:0 animated:YES];
baseAlert=nil;
}
}
Thanks for any help.
I have this code in my Application:
if (self.uiSwtichState == TRUE)
{
if ([CLLocationManager locationServicesEnabled])
{
[self.textString stringByAppendingString:[[[SMSLocationModel alloc] init] getLocation]];
}
}
MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
sms.messageComposeDelegate = self;
sms.body = self.textString;
sms.recipients = self.arrayOfNumbers;
[self presentModalViewController:sms animated:YES];
this code is into a method called "send".
As you can imagine, the method getLocation get the current user location and, the first time the Application run, a UIAlertView appear on the screen asking the user if he want to allow the application to get his location.
The problem is that when this UIAlertView appear, she is immediately replaced by the sms Vie Controller (MFMessageComposeViewController) and in this way the user doesn't have enough time for answer YES or NO in the UIAlertView.
There is a way for wait that the user press a button in the UIAlertView and THEN present the ModalViewController?
Thanks!
you could use the CLLocationManagers delegate method to wait until the authorization state changes. Save the sms viewController to an instance variable if you are not authorized to use location services. then ask the location manager to authorize you. If the authorization state changes present that view controller.
// somewhere else
self.locationManager.delegate = self;
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(#"New Authorization State: %d", status);
if (self.viewControllerToPresentAfterLocationDidChange) {
if (status == kCLAuthorizationStatusAuthorized) {
// set location of viewController
[self presentViewController:self.viewControllerToPresentAfterLocationDidChange animated:YES completion:NULL];
}
else {
// user did not authorize you to use location services
}
self.viewControllerToPresentAfterLocationDidChange = nil;
}
}
- (IBAction)buttonPressed:(id)sender {
UIViewController *vc = [[UIViewController alloc] init];
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
// authorized already, no alert will appear.
// set location of vc
[self presentViewController:vc animated:YES completion:NULL];
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
self.viewControllerToPresentAfterLocationDidChange = vc;
// access location so alert will show
}
else {
// not authorized or restricted.
}
}
Maybe you can use this delegate:
- (void)didPresentAlertView:(UIAlertView *)alertView{
}
For example, initializing a boolean which can prevent of presenting this modal view controller. Then if dismissed change the boolean to allow presenting this view controller.
Yes, you can implement the AlertViewDelegate.
See UIAlertViewDelegate Documentation for further information.
Then you can use the alertView:clickedButtonAtIndex: method and present your MessageComposer when the user has clicked the alert view.
In my iPhone application, I have just implemented in-app SMS functionality. SMS functionality is working fine. But after opening MFMessageComposeViewController, if user wants to cancel sending sms, they have no option. The only option left is to send an sms, then only return on previous view. There should be a cancel button on navigation bar just as it is in the email composer. Below is the line of code that I wrote to have in-app sms functionality:
-(void) smsComposer{
MFMessageComposeViewController *_smsCompose = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
_smsCompose.body = #"SMS BODY";
_smsCompose.messageComposeDelegate = self;
[self presentModalViewController:_smsCompose animated:YES];
}
}
Is there anything that I am missing?
Thanks in advance,
PC
Try This....
in .h file
#import <MessageUI/MFMessageComposeViewController.h>
and
#interface TestViewController : UIViewController <MFMessageComposeViewControllerDelegate>
And then Button Click method
-(void)buttonPressed:(UIButton *)button
{
[self sendSMS:#"Body of SMS..." recipientList:[NSArray arrayWithObjects:#"+1-111-222-3333", #"111-333-4444", nil]];
}
MFMessageComposeViewController to create the SMS content and another method for handling the user interaction with the SMS dialog.
-(void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
And
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled")
else if (result == MessageComposeResultSent)
NSLog(#"Message sent")
else
NSLog(#"Message failed")
}
And remember: You cannot send SMS messages from within the simulator. Test on Device.
With the SDK / Cocoa Touch, is it feasible to make an app that will SMS automatically?
My objective isn't to SPAM anyone.
It is not possible, exactly for the reason you mentioned: it would make spamming possible.
You would need a server that would handle the SMS for you, and an API for the app to interact with that server. It's not possible to have your app send messages directly from the phone, but you can certainly have your app interact with an external service that will send the messages for you.
Three is a way to pre-build an SMS using MFMessageComposeViewController. The only issue with this method is that a modal view will be shown to the user to accept the SMS (like the window that sends an e-mail by the default way). There is no way to send a SMS in "silent mode" without jailbreak.
{
...
[self sendSMS:#"_SMS_TEXT_" recipientList:[NSArray arrayWithObjects:#"PHONE_NUMBER", nil]];
...
}
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled")
else if (result == MessageComposeResultSent)
NSLog(#"Message sent")
else
NSLog(#"Message failed")
}
I am having difficulties adding the ability for a user to send a support email to myself (the maker of said app). I have gotten it work almost perfectly however the view doesn't close when you click cancel, or when you click send. Thanks for the help!
Here is the code:
MFMailComposeViewController *mail = [[[MFMailComposeViewController alloc] init] autorelease];
mail.mailComposeDelegate = self;
[mail setToRecipients:[NSArray arrayWithObject:#"support#lindahlstudios.com"]];
[mail setSubject:#"Fraction Calculator Lite Support"];
[self presentModalViewController:mail animated:YES];
You're setting self to be the MFMailComposeViewController's delegate. In mailComposeController:didFinishWithResult:error: be sure to call
[self dismissModalViewControllerAnimated:YES];
like so:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
You need to implement the delegate method from MFMailComposeViewControllerDelegate.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}