简体   繁体   English

在我的iOS应用中实现Mail

[英]Implementing Mail within my iOS app

I've currently got an iOS app where an item is randomly chosen from an array of items and is displayed on the screen. 我目前有一个iOS应用,其中从一个项目数组中随机选择一个项目并显示在屏幕上。 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. 例如,您单击按钮,它将随机显示一个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. 因此,用户得到数字“ 3”,单击电子邮件按钮,电子邮件出现时,“ 3”已预先填充在正文中。

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) 对于第一个问题,这是我的ViewController.h的外观(我已经添加了MessageUI框架)

#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 我的ViewController.m看起来像

#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 我假设它将与MessageUI的这一点有关

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

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. 对于第二个问题,注意isHTML必须设置为NO,以防您的文本不使用它。

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

[mailController setSubject:@"randomNumber"];                  
[mailController setMessageBody:self.ideaLabel.text isHTML:NO];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM