Assigning to 'id<MFMessageComposeViewControllerDelegate>' from incompatible type 'MainViewController *' - iphone

I have the following warning in my code (XCode 4.3 / iOS 4/5) -
Assigning to 'id' from incompatible type 'MainViewController *'
The warning is raised in this section -
- (IBAction)sendInAppSMS:(id)sender
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"A test message from http://www.macoscoders.com";
controller.recipients = [NSArray arrayWithObjects:#"9880182343",nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
In particular this line -
controller.messageComposeDelegate = self;
In a bit confused as to what is wrong with my code. Googling for the Warning I found some references but I'm having difficulty in understanding the answers.
Any pointers / explanations would be greatly appreciated.
Best regards
rob
My complete .h file -
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MessageUI/MessageUI.h>
#import "EasyTracker.h"
#interface MainViewController : TrackedUIViewController <MFMailComposeViewControllerDelegate> {
IBOutlet UIView *volumeSlider;
AVPlayer *radiosound;
IBOutlet UIButton *playpausebutton;
IBOutlet UIActivityIndicatorView *activityIndicator;
NSTimer *timer;
}
#property(nonatomic, retain) AVPlayer *radiosound;
#property(nonatomic, retain) UIButton *playpausebutton;
- (void)updatebuttonstatus;
- (void)playCurrentTrack;
- (void)pauseCurrentTrack;
- (IBAction)playbutton;
- (IBAction)openMail:(id)sender;
- (IBAction)sendInAppSMS:(id)sender;
#end
Highlights from my .m file -
#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>
#import "radio99AppDelegate.h"
#implementation MainViewController
- (IBAction)sendInAppSMS:(id)sender
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"A test message from http://www.macoscoders.com";
controller.recipients = [NSArray arrayWithObjects:#"9880182343",nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SMSTester" message:#"User cancelled sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
case MessageComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SMSTester" message:#"Error occured while sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
case MessageComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SMSTester" message:#"SMS sent successfully..!"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"A Message from MobileTuts+"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"fisrtMail#example.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *myImage = [UIImage imageNamed:#"mobiletuts-logo.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"mobiletutsImage"];
NSString *emailBody = #"Have you seen the MobileTuts+ web site?";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
#pragma mark - MFMailComposeController delegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the Drafts folder");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send the next time the user connects to email");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was nog saved or queued, possibly due to an error");
break;
default:
NSLog(#"Mail not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
#end

You are using:
MFMailComposeViewControllerDelegate
Where it should be:
MFMessageComposeViewControllerDelegate
Change here:
#interface MainViewController : TrackedUIViewController <MFMessageComposeViewControllerDelegate> {

Implement the UINavigationControllerDelegate in your header file.

didn't work with this library, but as I can see, youe MainViewController is MF*Mail*ComposeViewControllerDelegate, but you setting it as MF*Message*ComposeViewControllerDelegate.

Related

Using MFMailController in UITabbar controller

I am using UITabbarcontroller in one of the viewcontroller, In one tabbar i need to have only Mail controller, when i am doing so , then that is entering into infinite loop, how would i overcome it, The Code related to
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSString *message = #"";
// Notifies users about errors associated with the interface
switch (result) {
case MFMailComposeResultCancelled:
message = #"Mail: canceled";
break;
case MFMailComposeResultSaved:
message = #"Mail: saved";
break;
case MFMailComposeResultSent:
message = #"Mail: sent";
break;
case MFMailComposeResultFailed:
message = #"Mail: failed";
break;
default:
message = #"Mail: not sent";
break;
}
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[controller dismissModalViewControllerAnimated:YES];
}
This is how I would do it
Drag and drop a UITabBarButton object on your UITabBar and call it "Email". Now create this IBAction
-(IBAction)composeMyEmail
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
Connect your IBAction to your "Email" button.
Remove all the code you have under viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

MFMailComposeViewController model does not display

I am presenting MFMailComposeViewController in my app for Email.
But the view is not getting disply , only upper SEND and CANCEL buttons getting displayed.
This is the screen shot of view that is getting display after clicking on "Email to Subscribe" button.
I have checked out all the related codes. I have added "MessageUI" framework and imported
MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h
I have used following code :
- (void)viewWillAppear:(BOOL)animated {
[self.view setFrame:CGRectMake(0, 62, 320, 418)];
[APPDELEGATE.window addSubview:self.view];
[self.navigationController.navigationBar setHidden:NO];
self.navigationItem.title=#"Free Subscription";
[super viewWillAppear:animated]; }
-(void)viewWillDisappear:(BOOL)animated {
[self.view removeFromSuperview];
[super viewWillDisappear:animated]; }
-(IBAction)btnEmailPressed {
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:#"Free Subscription"];
[Apicker setMessageBody:#" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
}
[Apicker release];
} }
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError
*)error{
NSString *message;
switch (result) {
case MFMailComposeResultCancelled:
message =#"You have canceled your email.";
break;
case MFMailComposeResultFailed:
message=#"Your email is failed";
break;
case MFMailComposeResultSent:
message=#"Your email was successfully sent.";
break;
case MFMailComposeResultSaved:
message=#" Your email has been saved";
break;
default:
message=#" Your email is not send";
break;
}
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:#"" message:message delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alt show];
[alt release];
[self dismissModalViewControllerAnimated:YES];
}
I could not get the reason for this problem.
Thanks.
Its for IOS 6
-(void)email{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"xyz#gmail.com", nil]];
[composer setSubject:#""];
// [composer.setSubject.placeholder = [NSLocalizedString(#"This is a placeholder",)];
[composer setMessageBody:#"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES completion:nil];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#",[error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Just try
For iOS 5
-(void)email:(id)sender{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"xyz#gmail.com", nil]];
[composer setSubject:#""];
// [composer.setSubject.placeholder = [NSLocalizedString(#"This is a placeholder",)];
[composer setMessageBody:#"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#",[error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}
try modifying your code like this :-
-(IBAction)btnEmailPressed
{
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:#"Free Subscription"];
[Apicker setMessageBody:#" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
NSString *recipients = #"mailto:info#xyz.com?cc=&subject=";
NSString *body = #"&body=";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
[Apicker release];
}
}
add these two delegate in .h file MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate
UPDATE
if ([MFMailComposeViewController canSendMail]) {
appDelegate.imgCapture = [self captureView];
[appDelegate.imgCapture retain];
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = #"Set Your Body Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}

Trouble opening the MFMailComposeViewController on the device, works in simulator.

I am doing this its working in simulator but when we try to open in device then program is terminating.
Plz suggess me fast.
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
//[self becomeFirstResponder];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
if ([MFMessageComposeViewController canSendText])
Your problem is here. You are trying to check whether device will be able to send Text messages and not email Message. you should try using
if([MFMailComposeViewController canSendMail])
The problem might be that your device is not configured to any accounts in the mail.Please check this once.
Have you implement MFMailComposeViewControllerdelegate methods in your code??
#pragma mark --------------------------------------------
#pragma mark MFMailComposeViewController delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Mail send canceled.");
/*
Execute your code for canceled event here ...
*/
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved.");
/*
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sent" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 1;
alert.delegate = self;
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sending Failed" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 2;
alert.delegate = self;
[alert show];
[alert release];
break;
}
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];//dismissing modal view controller
}
Your code looks ok, but did you check if the device can send mail:
if ([MFMailComposeViewController canSendText]) {
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
[mail release], mail = nil;
} else {
// show message to the use that he can't send an email.
}

MailComposeViewController - when i press button "Cancel" i don't see the panel with "Draft" ,"Save Draft" and "Cancel" buttons

I am using the MFMailComposeViewController Controller like this:
MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
picker1.mailComposeDelegate = self;
[picker1 setSubject:#"I have a pencil for you"];
UIImage *roboPic = [UIImage imageNamed:#"RobotWithPencil.jpg"];
NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"RobotWithPencil.jpg"];
NSString *emailBody = #"This is a cool image of a robot I found. Check it out!";
[picker1 setMessageBody:emailBody isHTML:YES];
picker1.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:picker1 animated:YES];
[picker1 release];
When I press the "Cancel" button I don't see the panel with "Draft", "Save Draft" and "Cancel" buttons, the screen is locked/frozen but the panel with buttons mentioned above doesn't appear.
I will be glad to get any assistance.
Thanks in advance
Moshe
you have to use this for mail composing. and in interface file write this delegte MFMailComposeViewControllerDelegate . also import messageUi framework. its working.
#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposer
{
if(![MFMailComposeViewController canSendMail])
{
[self setUpMailAccount];
return;
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Strpy's Revange"];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#""];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"", #"", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#""];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
if([UIAppDelegate gameCodeFlag]==0)
{
NSString *emailBody = [NSString stringWithFormat:#"Game Score! %d",[UIAppDelegate scorePost]];
[picker setMessageBody:emailBody isHTML:NO];
}
else {
[UIAppDelegate readFriendPlist];
NSString *emailBody = [NSString stringWithFormat:#"Game Code! %#",[UIAppDelegate gameCode]];
[picker setMessageBody:emailBody isHTML:NO];
}
[self presentModalViewController:picker 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:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Sending..."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
if([UIAppDelegate gameCodeFlag]==1)
{
[[CCDirector sharedDirector] pushScene:[StorePage scene]];
}
}

MFMailComposeViewController hangs my app

I am trying to add email functionality to my app. I can get the MFMailComposeViewController to display correctly and pre-populate its subject and body, but for some reason when the user clicks on the "Cancel" or "Send" buttons in the nav bar the app just hangs. I inserted a NSLog() statement into the first line of mailComposeController"didFinishWithResult:error and it doesn't even print that line out to the console.
Does anybody have an idea what would cause the MFMailComposeViewController to hang like that?
Here is my code from the header:
#import "ManagedObjectEditor.h"
#import <MessageUI/MessageUI.h>
#interface MyManagedObjectEditor : ManagedObjectEditor
<MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate,
UINavigationControllerDelegate> {
}
- (IBAction)emailObject;
#end
from the implementation file:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.delegate = self;
[mailComposer setSubject:NSLocalizedString(#"An email from me",
#"An email from me")];
[mailComposer setMessageBody:emailString
isHTML:YES];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
}
[error release];
[emailString release];
and here is the code from the callback:
#pragma mark -
#pragma mark Mail Compose Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error {
NSLog(#"in didFinishWithResult:");
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"saved");
break;
case MFMailComposeResultSent:
NSLog(#"sent");
break;
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error sending email!",#"Error sending email!")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"Bummer",#"Bummer")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Thanks!
I got bit by this too, you need to set the mailComposeDelegate, not the delegate.