I'm trying to integrate sending mail into my app, but I end up with 2 warnings. I am using Obj-C, the Cocos2d Framework. This is my code.
-(void) mailTapped: (id) sender {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"", nil]];
[composer setSubject:#"Check Out This Awesome App!"];
[composer setMessageBody:#"I found this great game on the App Store! It's called Mole Attack. It's a side scroller with an epic story. You can check out some screenshots of the gameplay and download it here. Download link - " isHTML:NO]; //Include link and pics
[self presentModalViewController:composer animated:YES]; // <--- warning - GameOver (name of class) may not respond to '-presentModalViewController:animated:'
}
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES]; // <--- warning - GameOver may not respond to '-dismissModalViewControllerAnimated:YES'
if (result == MFMailComposeResultFailed) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"The email was not sent. You must be in wifi or 3G range. Try again later." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Thanks in advance!
Are you sure your GameOver class is inheriting from UIViewController? That's the class that defines the two methods that you're getting warnings about.
Related
I created a camera button in a collection view controller cell to take a picture, and output it to a UIImageView the size of the cell. Right now I'm getting a error with the delegate and I'm not quite sure what it wants me to do.
- (IBAction)cameraButtonClicked:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
UIAlertView *cameraAlertView = [[UIAlertView alloc] initWithTitle:#"Camera Not Available" message:#"There is no camera on this device which really defeats the purpose of this game. We suggest you get an iDevice with a camera." delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[cameraAlertView show];
}else{
//Show the Image Picker Controller Here
UIImagePickerController * ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.allowsEditing = NO;
//Set the Delegate
ipc.delegate = self;
[self.navigationController presentViewController:ipc animated:YES completion:nil];
}
}
It's telling you that self does not conform to the UINavigationControllerDelegate and/or UIImagePickerControllerDelegate protocols. Implement those protocols on your class.
Did you specify the UIImagePickerControllerDelegate protocol in your header?
In iOS 6 the presented MFMailComposeViewController will not dismiss if user attempts to send second email...
Everything works perfectly the first go around and email is sent. However, if email option is selected again the MFMailComposeViewController will not dismiss on cancel.
Here is how I implemented it:
- (IBAction)buttonEmailClick:(id)sender {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Can't send" message:#"This device is unable to send emails." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
NSDictionary *contactsInfo = [self contactsInfoFromPlistNamed:kConfigPlistName];
[mailComposeViewController setToRecipients:[NSArray arrayWithObject:[contactsInfo objectForKey:#"email"]]];
//[mailComposeViewController setSubject:kEmailDefaultSubject];
//[mailComposeViewController setMessageBody:text isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
}
and then this:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
UIAlertView *alert = nil;
if (result == MFMailComposeResultSent) {
alert = [[UIAlertView alloc] initWithTitle:#"Sent" message:#"Your email was sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
else if (result == MFMailComposeResultFailed) {
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"An error occured and your email was not sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
It works fine in iOS 5 but not in iOS 6. I have tried replacing with non deprecated methods for iOS 6, but it doesn't work.
Have you tried creating a fresh MFMailComposeViewController each time they go to send an email? I'm not sure if you should be reusing it.
You can try this:
MFMailComposeViewController * composer = [[MFMailComposeViewController alloc] init];
composer.delegate = self;
-(void)mailComposeController:didFinishWithResult:error: should be called if you assign that class to the delegate
I am new to iphone development. I am building an app through which i can send the email.I am using the following code
- (IBAction)btnsendPressAction:(id)sender {
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer=[[MFMailComposeViewController alloc]init];
mailer.mailComposeDelegate=self;
[mailer setSubject:#"A Message for testing"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"rajshetty2125#gmail.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *image=[UIImage imageNamed:#"logo.png"];
NSData *imageData=UIImagePNGRepresentation(image);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"logo.png"];
NSString *emailBody=#"This is test email";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
mailer.modalPresentationStyle = UIModalPresentationPageSheet ;
}
else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"failure" message:#"Alert" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
My problem is mailComposeController delegate is not getting called and I am not able to send email through my app.I went through many links , but every where I found the same method. Can anyone help me on this ?
You won't be able to send email from Simulator. You can only check this functionality on iOS device with any account configured in Mail application.
Here use this link and your add(configured) an account in device then only you can send the mail.
This snippet is working for me:
if ([MFMailComposeViewController canSendMail]) {
self.mailController = [[MFMailComposeViewController alloc] init];
[self.mailController setSubject:#"Invitation"];
[self.mailController setMessageBody:#"Message" isHTML:NO];
self.mailController.mailComposeDelegate = self;
[self presentModalViewController:self.mailController animated:YES];
}
but make sure that you're implement delegate in interface
#interface MyClass() <MFMailComposeViewControllerDelegate>
and check your callback method:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
hey i am not sure but may be it's helpful, replace your last line
mailer.modalPresentationStyle = UIModalPresentationPageSheet ;
with
[self presentModalViewController: mailer animated:YES];
The following code leaks whenever I send email, and I do not know what to change. There are people saying online that MFMailComposeViewController leaks, but it is kind of unbelievable that the leak was never fixed.
Any idea, what might be wrong with the following code, or confirm that MFMailComposeViewController leaks.
- (void) email {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:[[[NSMutableArray alloc] initWithObjects:#"123#123.com", nil] autorelease]];
[mailViewController setSubject:#"Contact us"];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Cannot send email"
message:#"Please check your email setting"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}
There's nothing wrong with that code. If you're seeing a leak in an app with just that in it then I suggest filing a radar for it with specific instructions for what steps cause the leak.
On another note I would change the settings of recipients line to this:
[mailViewController setToRecipients:[NSArray arrayWithObject:#"123#123.com"]];
After I choose a picture through the UIImagePickerController interface from the Photo Library, the Photo Library view stays displayed, even though I've called dismissModelViewControllerAnimated in imagePickerController:didFinishPickingImage:editingInfo.
Has anyone seen this? These are the three relevant methods I'm using:
- (IBAction)choosePictureFromLibrary:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Error accessing Photo Library" message:#"This device does not support a Photo Library." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Picture picked!" message:#"You picked a picture!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[picker dismissModalViewControllerAnimated:YES];
}
I would have thought that calling imagePickerController:didFinishPickingImage:editingInfo would completely dismiss the Photo Library view, but it doesn't seem to. Is there anything else I have to do to make it go away?
You need to access the viewController of the picker not the picker itself. Try this line instead.
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
You can just call
[self dismissModalViewControllerAnimated:YES];
to dismiss any modal view controller on top of the current view.
This makes sense since you present the view controller by calling:
[self presentModalViewController:picker animated:YES];
Just an update to the answers to this
[self dismissModalViewControllerAnimated:YES];
has been deprecated in iOS 6.0 so you now need to use.
[self dismissViewControllerAnimated:YES completion:nil];
Not a huge change but for anyone that looks at this question and they are using iOS 6.0 they will need an updated answer.
[self presentModalViewController:filePicker animated:YES];
has also been deprecated in favor of
[self presentViewController:filePicker animated:YES completion:nil];