Question

I've currently got an iOS app where an item is randomly chosen from an array of items and is displayed on the screen. I've already built the "randomness" and display functionality into the app, but am now trying to set it up so that you can email the item from the array that is currently on the screen from within the app. For example, you hit the button and it randomly displays a number from 1-10. I'd like to be able for the user to email whatever number is randomly shown on the screen, with the email body pre-populated with the number on the screen. So, the user gets the number "3", hit's the email button and when the email compose comes up "3" is already pre-populated in the body.

I am having two issues, first is figuring out how to implement the email function code within my current code. I've already built a tester app that has a button that triggers the email compose to appear, and populate the body with some static text, so I've got a basic understanding of how the code works, but I do not know how to integrate it with the code I've already written.

My second issue is getting the body of the mail message to be pre-populated with the random number from the screen.

For the first problem here is what my ViewController.h looks like (I've already add the MessageUI framework)

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController {
NSArray *testArray;
}
- (IBAction)buttonGo:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UILabel *testLabel;
@property (strong, nonatomic) NSArray *testArray;

- (void) makePrediction;

@end

My ViewController.m looks like

#import "ViewController.h"
#import <MessageUI/MessageUI.h>

@interface ViewController ()

@end

@implementation ViewController
@synthesize testArray;
@synthesize testLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.testArray = [[NSArray alloc] initWithObjects:@"number one",@"number `two",@'numberthree", nil];`

    - (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonGo:(id)sender {
    NSUInteger index = arc4random_uniform(self.testArray.count);

    self.testLabel.text = [self.testArray objectAtIndex:index];
}

- (void) makePrediction {
    NSUInteger index = arc4random_uniform(self.
testArray.count);

    self.testLabel.text = [self.testArray objectAtIndex:index];
}


- (BOOL) canBecomeFirstResponder {
    return YES;
}


- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    self.testLabel.text = @"";
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( motion == UIEventSubtypeMotionShake ){
        [self makePrediction];
    }

}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"motion cancelled");
}


@end

The app runs fine, but I'm unsure where to implement my email code. I'm also unsure how to populate my email body with the random choice from my array. I am assuming it will have something to do with this bit of MessageUI

NSString * sentFrom = @"text in email body";
    [myMail setMessageBody:sentFrom isHTML:YES];
Was it helpful?

Solution

This is the place to do your job as you will be holding text

- (IBAction)buttonGo:(id)sender

and for second issue Note isHTML has to be set NO in case your text doesnt use it.

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

[mailController setSubject:@"randomNumber"];                  
[mailController setMessageBody:self.ideaLabel.text isHTML:NO];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top