IOS Delegate in Native Extension - ios

I try to integrate the MFMessageComposeViewController in a custom native extension.
The controller works perfectly except for one thing... I can't cancel or dismiss the controller when I hit the cancel button or when the message is send.
There is a problem with the delegate that I don't know how to fix.
Header
#interface platools : UIViewController <MFMessageComposeViewControllerDelegate>
Function call
bool shareIMessage(const char *url)
{
if(![MFMessageComposeViewController canSendText])
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return false;
}
NSLog(#"shareIMessage");
NSString *ImageStrUrl = [NSString stringWithFormat:#"%s",url];
NSLog(#"%#",ImageStrUrl);
NSURL *ImageURL = [NSURL URLWithString:ImageStrUrl];
NSData *imageData = [NSData dataWithContentsOfURL:ImageURL];
UIViewController *root = [[[UIApplication sharedApplication] keyWindow] rootViewController];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = (id<MFMessageComposeViewControllerDelegate>)[[UIApplication sharedApplication]delegate];
if ([MFMessageComposeViewController canSendAttachments])
{
NSLog(#"Attachments Can Be Sent.");
BOOL didAttachImage = [messageController addAttachmentData:imageData typeIdentifier:#"public.data" filename:#"image.gif"];
if (didAttachImage)
{
NSLog(#"Image Attached.");
}
else
{
NSLog(#"Image Could Not Be Attached.");
}
}
[root presentViewController:messageController animated:YES completion:nil];
return true;
}
Function delegate
void messageComposeViewController(MFMessageComposeViewController* controller)
{
NSLog(#"messageComposeViewController");
UIViewController *root = [[[UIApplication sharedApplication] keyWindow] rootViewController];
// Close the Mail Interface
[root dismissViewControllerAnimated:YES completion:nil];
}
void didFinishWithResult(MessageComposeResult result)
{
NSLog(#"didFinishWithResult");
}
Full code : http://pastebin.com/tuJY0mwv (the target function is sendIMessage)

Related

Send Image with text message

I want to send an image with my text Message. I have used MFMessageComposeViewController. This is the code i have done:
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
NSArray *recipents = #[[arr_promoterDetaildata valueForKey:#"phone_number"], #"72345524"];
UIImage *img = [UIImage imageNamed:#"ic_dummy_img"];
NSData *imgData = UIImagePNGRepresentation(img);
BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:#"image.png"];
NSString *message = [NSString stringWithFormat:#"Sending SMS"];
[messageController setRecipients:recipents];
[messageController setBody:message];
if (didAttachImage)
{
// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
}
else
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Failed to attach image"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[warningAlert show];
return;
}
I have tried so many method’s but it is not attaching image in my message screen. Can anyone help?

Convert NSArray to NSMutable to remove string dynamically

I have a code that I modified to pull data from an external JSON file. There are two NSArrays. One for the image and one for color. Then, when a particular array is pressed it opens a webviewcontroller. I got it to work perfectly. Here is the problem. I want to be able to modify the JSON anytime I want externally and have the app update the information but since the NSArray information is hardcoded I don't know how to remove an array if I remove an objectforkey from the JSON. Any help would be appreciated.
The code to call the images and colors is:
- (IBAction)onclick:(id)sender {
NSArray *images = #[
[UIImage imageNamed:[NSString stringWithFormat: #"%#",[dataDictionary objectForKey:#"PhoneUrl"]]],
[UIImage imageNamed:[NSString stringWithFormat: #"%#",[dataDictionary objectForKey:#"TextUrl"]]],
[UIImage imageNamed:[NSString stringWithFormat: #"%#",[dataDictionary objectForKey:#"MailUrl"]]]
];
NSArray *colors = #[
[UIColor [NSString stringWithFormat: #"%#",[dataDictionary objectForKey:#"PhoneColor"]],
[UIColor [NSString stringWithFormat: #"%#",[dataDictionary objectForKey:#"TextColor"]]],
[UIColor [NSString stringWithFormat: #"%#",[dataDictionary objectForKey:#"MailColor"]]],
];
Sidebar *callout = [[Sidebar alloc] initWithImages:images borderColors:colors];
callout.delegate = self;
[callout show];
}
Then when pressed it calls the following:
- (void)sidebar:(Sidebar *)sidebar didTapItemAtIndex:(NSUInteger)index {
self.webViewController = [[PBWebViewController alloc] init];
PBSafariActivity *activity = [[PBSafariActivity alloc] init];
self.webViewController.applicationActivities = #[activity];
self.webViewController.excludedActivityTypes = #[UIActivityTypeMail, UIActivityTypeMessage];
NSLog(#"Tapped item at index %lu",(unsigned long)index);
if (index == 0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[dataDictionary objectForKey:#"PhoneWeb"]]];
[sidebar dismissAnimated:YES completion:nil];
}
if (index == 1) {
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSArray *recipents = #[[dataDictionary objectForKey:#"TextWeb"]];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipents];
[[messageController navigationBar] setTintColor:[UIColor whiteColor]];
[self presentViewController:messageController animated:YES completion:nil];
[sidebar dismissAnimated:YES completion:nil];
}
if (index == 2) {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
[[mailer navigationBar] setTintColor:[UIColor whiteColor]];
mailer.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:[dataDictionary objectForKey:#"MailWeb"], nil];
[mailer setToRecipients:toRecipients];
[self presentViewController:mailer animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
[sidebar dismissAnimated:YES completion:nil];
}

MFMailComposeViewController black screen on iPad

I'm using cocos2d-x framework and I need to popup the mail when clicking button.
The code works fine on iphone5 (6.0), ipod touch 5(6.0):
MailSender.h
#interface MailSender : UIViewController <MFMailComposeViewControllerDelegate>
{
UIViewController *currentModalViewController;
}
-(void)sendMail:(const char *)subject receiver:(const char *)receiver;
#end
MailSender.mm
#import "MailSender.h"
#import "../cocos2dx/platform/ios/EAGLView.h"
#implementation MailSender
- (void)sendMail:(const char *)subject receiver:(const char *)receiver
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithUTF8String:subject]];
NSArray *toRecipients = [NSArray arrayWithObject:[NSString stringWithFormat:#"%s", receiver]];
[mailer setToRecipients: toRecipients];
//NSString *emailBody = [NSString stringWithFormat:#"<p>This is a sample posting in iOS. My Score is %s!</p>",score];
NSString *emailBody = #"";
[mailer setMessageBody:emailBody isHTML:YES];
// only for iPad
// mailer.modalPresentationStyle = UIModalPresentationFormSheet;
UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
currentModalViewController = [UIViewController alloc];
[rootViewController.view addSubview:currentModalViewController.view];
[currentModalViewController presentViewController:mailer animated:true completion:nil];
}
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];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
const char *message;
switch (result)
{
case MFMailComposeResultCancelled:
message = "Mail cancelled";
break;
case MFMailComposeResultSaved:
message = "Mail saved";
break;
case MFMailComposeResultSent:
message = "Mail send";
break;
case MFMailComposeResultFailed:
message = "Mail failed";
break;
default:
message = "Mail cancelled";
break;
}
NSLog(#"%s",message);
[currentModalViewController dismissViewControllerAnimated:true completion:nil];
[currentModalViewController.view.superview removeFromSuperview];
[currentModalViewController release];
}
#end
But on my ipad mini (6.0) the mail popped up correctly but when clicked the "send mail" or "cancel" the view was removed and leaving a black screen (everything on the screen is gone)
Any advice will be appreciated, thanks :)
try this code
if ([MFMailComposeViewController canSendMail]) {
mailComposer = [[MFMailComposeViewController alloc]init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:#[#"yourmail#com"]];
[mailComposer setSubject:#"Subject"];
[mailComposer setMessageBody:#"hello \n" isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Error"
message:#"can not send mail with this device"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}
pragma mark MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (result) {
NSLog(#"Result : %d",result);
}
if (error) {
NSLog(#"Error : %#",error);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I'm using this code for feedback via email in my cocos2d-x game.
Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL);
You can add subject:
Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL + "?subject=Hello from NX");
This solution tested on iOS and Android.

MFMailComposeViewController - Unable to presentViewController

Here is my code
-(IBAction)emailButtonPressed :(UIButton *)sender {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"Mail has not been set up on this device" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
return;
}
NSString *targetFile = [self saveCompleteImage];
MFMailComposeViewController *mailpicker = [[MFMailComposeViewController alloc] init];
[mailpicker setMailComposeDelegate:self];
NSString *mimeType = [StringHelper getMimeType:targetFile];
[mailpicker addAttachmentData:[NSData dataWithContentsOfFile:targetFile] mimeType:mimeType fileName:[targetFile lastPathComponent]];
[mailpicker setSubject:[self.currentDocument getNameForUntitled]];
mailpicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self.presentedViewController presentViewController:mailpicker animated:YES completion:nil];
}
It's not presenting mailpicker. Kindly tell me where am i wrong.
Issue is with this code:
[self.presentedViewController presentViewController:mailpicker animated:YES completion:nil];
You need to use:
[self presentViewController:mailpicker animated:YES completion:nil];
or
[self.presentingViewController presentViewController:mailpicker animated:YES completion:nil];
Check ModalViewControllers Reference to understand the difference between presentedViewController and presentingViewController

MFMailComposeViewController crash in iOS6 ARC

Iam presenting MFMailComposeViewController from my custom class(not a viewController). In iOS5 it is working fine but in iOS6 it is getting crash immediately after presenting the compose sheet. I found the issue the dealloc method is getting called after presenting the view, so self is deallocating. Due to this mailcomposer cannot call the delegate method on self so it is crashing. I didnt get a solution for that. Am using ARC. How to prevent self from deallocating until the delegate method is getting called?
-(void)shareOnViewController:(UIViewController *)viewController completion:(ShareCompletionHandler)completion
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
mailer.modalPresentationStyle = UIModalPresentationPageSheet;
[mailer setSubject:[self.userInfo objectForKey:#"title"]];
NSData *imageData = UIImagePNGRepresentation([self.userInfo objectForKey:#"image"]);
if (imageData) {
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"AttachedImage"];
}
NSURL *emailBody = [self.userInfo objectForKey:#"url"];
if (![emailBody isEqual:#""]) {
[mailer setMessageBody:[emailBody absoluteString] isHTML:NO];
}
[viewController presentModalViewController:mailer animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unable to send mail"
message:#"Device is not configured to send mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
self.completionHandler = completion;
}
According to me , The presentModalViewController method is deprecated in iOS 6.0 .
Instead you need to use
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
Else can you show the crash log ??
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc]
init];
NSData *imageData = UIImagePNGRepresentation(image);
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:subject];
NSArray * recipents = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%#",NSLocalizedString(#"client_email", #"")],nil];
[mailComposer setToRecipients:recipents];
[mailComposer addAttachmentData:imageData mimeType:#"image/png" fileName:[NSString stringWithFormat:#"imageProfile-%#-%#",someId,lastname]];
mailComposer.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[vc presentViewController:mailComposer animated:YES completion:nil];
}
else
{
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Email Account Not Found"
message:#"You need to setup an Email Account!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
}
One of the possible root cause is ARC.
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer will be auto-released as soon as the method body is fully executed.
You could create a reference to hold the object to avoid ARC releasing it before it is ready.
#property (nonatomic, strong) MFMailComposeViewController * composer;
....
self.composer = [[MFMailComposeViewController alloc] init];
...
[viewController presentViewController:self.composer animated:YES];
[Edited suggestion]
Sorry, I miss out the first part of your question where you were calling the method from another custom class.
I came across the similar situation also. In the end, I converted the "Custom Class" into a Singleton class.
#pragma mark Singleton Methods
+ (id)sharedInstance {
static CustomClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
This will make sure the class will be retained nicely. But this will depend on the usage and design of the CustomClass. Just to share what I did.
[[CustomClass sharedInstance] shareOnViewController:vc completion:...];

Resources