`
cenphoenix
  • 浏览: 160574 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

iPhone Coding Tutorial – In Application Emailing

阅读更多

A lot of applications you see have an email button. When you click it then it will leave the application and take you to the Mail application. It can get really annoying leaving the application and then going back in after your done sending the email. This is just a great way to show off your app and make your app look more professional and make it easier on the user by filling in the To and Subject for them. They are also able to change the From to whatever email then want you to receive an email from just like you can do in the Mail app. So todays tutorial is gonna show you how to email within your application. We will be using the MessageUI framework and we will also include an attachment in the email. Theres a screenshot to the left of how it will look.

 

 

So lets get started…

 



1. Create A New View Based Application

You can name yours whatever you want, in the tutorial I will be referring it as the NameViewControllers.

2. Import The Frameworks

The first thing we need to do is import the framework. So go to your Frameworks folder in the Files In Pain section on the left. Open the folder and right click one of the frameworks and click “Reveal In Finder.” Heres a screenshot on what you should do. 



 Go ahead and look for the “MessageUI.framework” and highlight it then drag it into your frameworks folder. Make sure that when you click Add on the thing that makes sure you want to import it that “Copy items into destination group’s folder (if needed)” is not check. Thats a very important step or you will have big time problems and you do this with any frameworks you would ever use in the future.

3. Implementing The Code

Now that we have imported the framework lets get into some coding. So go ahead and jump in the NameViewController.H and make an IBAction for a button. Then copy that code and paste it in the NameViewController.M with curly brackets. Also make sure to add the button in Interface Builder and link it up with a Touch Up Inside method. You guys are at the point to knowing how to hook up actions and dragging stuff into Interface Builder. After that we want to on the top of the NameViewController.h import the framework. So on the top do #import “MessageUI/MessageUI.h” and the reason why we do this is because we must import out MessageUI framework to make calls to the associated header files. Now we need to also put in the delegate protocols for this framework. @interface NameViewController: UIViewController do this code <MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>. Heres the code here for the NameViewController.H

#import <MessageUI/MessageUI.h> @interface MailComposerViewController : UIViewController <MFMailComposeViewControllerDelegate,UINavigationControllerDelegate> { } -(IBAction)pushEmail; @end


 Now after your done with the .H jump into the .M viewcontroller. Now in your button action code do this:

-(IBAction)pushEmail { MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init]; mail.mailComposeDelegate = self; if ([MFMailComposeViewController canSendMail]) { //Setting up the Subject, recipients, and message body. [mail setToRecipients:[NSArray arrayWithObjects:@"email@email.com",nil]]; [mail setSubject:@"Subject of Email"]; [mail setMessageBody:@"Message of email" isHTML:NO]; //Present the mail view controller [self presentModalViewController:mail animated:YES]; } //release the mail [mail release]; } //This is one of the delegate methods that handles success or failure //and dismisses the mail - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self dismissModalViewControllerAnimated:YES]; if (result == MFMailComposeResultFailed) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Message Failed!” message:@”Your email has failed to send” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:nil]; [alert show]; [alert release]; } }


 What if we wanted to include an image attachment to the email? Well its quite simple. Just add this to the code right under the part where you set up the Recipient, Subject, and the Message.

UIImage *pic = [UIImage imageNamed:@"Funny.png"]; NSData *exportData = UIImageJPEGRepresentation(pic ,1.0); [mail addAttachmentData:exportData mimeType:@"image/jpeg" fileName:@"Picture.jpeg"];


 

Now we are setting up the MailComposer in the first part of the code in the action. Then we call a didFinishWithResult method where we are setting up if the email fails or sends. Also it sets up a Cancel button for you so we have to call the dismiss method so that it works. In the attachment code just edit the imageNamed:@”" with your images name.That is basically it! The source code is below for the people that just doesn’t wanna copy and paste or type… I am just jking with you guys. Happy iCoding!

from:http://icodeblog.com/2009/11/18/iphone-coding-tutorial-in-application-emailing/

 

 

 

 

 

  • 大小: 29.4 KB
  • 大小: 27.1 KB
分享到:
评论

相关推荐

    iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView

    这篇教程“iPhone Coding Tutorial – Inserting A UITextField In A UIAlertViewController”将会教你如何在现代iOS环境中,在警告视图控制器中添加一个文本字段。 首先,我们需要了解`UIAlertController`的基本...

    Multicast with Network Coding in Application-Layer Overlay Networks

    本文《Multicast with Network Coding in Application-Layer Overlay Networks》深入探讨了如何利用应用层覆盖网络的独特属性,通过引入网络编码技术,显著提升端到端的组播吞吐量。 #### 应用层覆盖网络特性 应用...

    Coding Games in Python

    Each chapter in Coding Games in Python shows how to construct a complete working game in simple numbered steps. The book teaches how to use freely available resources, such as PyGame Zero and Blender,...

    Coding project in python

    Using fun graphics and easy-to-follow instructions, Coding Projects in Python is a straightforward, visual guide that shows young learners how to build their own computer projects using Python, an ...

    Coding_Games_in_Scratch(英文版)

    《Coding Games in Scratch》是一本面向初学者的编程教材,主要使用Scratch编程语言来教授游戏开发的基础知识。Scratch是由麻省理工学院(MIT)的“终身幼儿园团队”开发的一款图形化编程工具,旨在帮助孩子们学习编程...

    cvpr12_tutorial_sparse_coding.ppt

    cvpr12_tutorial_sparse_coding.ppt

    Coding.Projects.in.Python.2017.pdf

    The simple, logical steps in Coding Projects in Python are fully illustrated with fun pixel art and build on the basics of coding, so kids can have the skills to build whatever kind of project they ...

    Error Control Coding-Fundamentals and Applications(Pearson 2004 2nd Ed.part07

    Coverage of the fundamentals of coding and the applications of codes to the design of real error control systems. Contains the most recent developments of coded modulation, trellises for codes, soft-...

    A Tutorial on Reed-Solomon Coding for Fault-Tolerance in RAID-like Systems

    ### 一、里德-所罗门编码(Reed-Solomon Coding)基础 里德-所罗门编码是一种非二进制线性错误校正码,广泛应用于数据存储和通信系统中,以提供强大的错误检测和纠正能力。它由Irving S. Reed和Gustave Solomon于...

    Coding Interview In Java

    1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth ...

    Coding Projects in Scratch

    《Coding Projects in Scratch》这本书是DK Children在2016年7月5日出版的一本面向9至12岁青少年的编程入门指南。Scratch是由麻省理工学院(MIT)的终身幼儿园团队开发的一种图形化编程语言,它通过拖拽积木式的编程块...

    Coding Projects and Games in Python.rar

    "Coding Projects in Python" 和 "Coding Games in Python" 这两本书籍是DK出版的专为儿童设计的编程教育资源,旨在帮助孩子们轻松入门编程,培养他们的逻辑思维能力和问题解决技巧。 "Coding Projects in Python" ...

    Coding Projects in Python 无水印pdf

    Coding Projects in Python 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或...

    Grokking the Coding Interview - Patterns for Coding Questions.zip

    《Grokking the Coding Interview - Patterns for Coding Questions》是一份专为北美算法面试准备的压缩包资源,其中包含了多种常见的编程题目模式。这份资源旨在帮助面试者熟练掌握各种算法和数据结构,以便在面试...

    Coding-Projects-in-Python.pdf.pdf

    Coding-Projects-in-Python.pdf

Global site tag (gtag.js) - Google Analytics