标签:
项目开发常用实用技术
1 import UIKit 2 import MessageUI 3 4 class ViewController: UIViewController ,UINavigationControllerDelegate, 5 MFMailComposeViewControllerDelegate{ 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 //首先要判断设备具不具备发送邮件功能 11 if MFMailComposeViewController.canSendMail(){ 12 let controller = MFMailComposeViewController() 13 //设置代理 14 controller.mailComposeDelegate = self 15 //设置主题 16 controller.setSubject("我是邮件标题") 17 //设置收件人 18 controller.setToRecipients(["a1@hangge.com","a2@hangge.com"]) 19 //设置抄送人 20 controller.setCcRecipients(["b1@hangge.com","b2@hangge.com"]) 21 //设置密送人 22 controller.setBccRecipients(["c1@hangge.com","c2@hangge.com"]) 23 24 //添加图片附件 25 var path = NSBundle.mainBundle().pathForResource("hangge.png", ofType: "") 26 var myData = NSData(contentsOfFile: path!) 27 controller.addAttachmentData(myData, mimeType: "image/png", fileName: "swift.png") 28 29 //设置邮件正文内容(支持html) 30 controller.setMessageBody("我是邮件正文", isHTML: false) 31 32 //打开界面 33 self.presentViewController(controller, animated: true, completion: nil) 34 }else{ 35 println("本设备不能发送邮件") 36 } 37 } 38 39 //发送邮件代理方法 40 func mailComposeController(controller: MFMailComposeViewController!, 41 didFinishWithResult result: MFMailComposeResult, error: NSError!) { 42 controller.dismissViewControllerAnimated(true, completion: nil) 43 44 switch result.value{ 45 case MFMailComposeResultSent.value: 46 println("邮件已发送") 47 case MFMailComposeResultCancelled.value: 48 println("邮件已取消") 49 case MFMailComposeResultSaved.value: 50 println("邮件已保存") 51 case MFMailComposeResultFailed.value: 52 println("邮件发送失败") 53 default: 54 println("邮件没有发送") 55 break 56 } 57 } 58 59 override func didReceiveMemoryWarning() { 60 super.didReceiveMemoryWarning() 61 } 62 }
1 import UIKit 2 import MessageUI 3 4 class ViewController: UIViewController ,UINavigationControllerDelegate, 5 MFMessageComposeViewControllerDelegate{ 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 11 //首先要判断设备具不具备发送短信功能 12 if MFMessageComposeViewController.canSendText(){ 13 let controller = MFMessageComposeViewController() 14 //设置短信内容 15 controller.body = "短信内容:欢迎来到hangge.com" 16 //设置收件人列表 17 controller.recipients = ["123456","120000"] 18 //设置代理 19 controller.messageComposeDelegate = self 20 //打开界面 21 self.presentViewController(controller, animated: true, completion: { () -> Void in 22 23 }) 24 }else{ 25 println("本设备不能发送短信") 26 } 27 } 28 29 //发送短信代理 30 func messageComposeViewController(controller: MFMessageComposeViewController!, 31 didFinishWithResult result: MessageComposeResult) { 32 controller.dismissViewControllerAnimated(true, completion: nil) 33 switch result.value{ 34 case MessageComposeResultSent.value: 35 println("短信已发送") 36 case MessageComposeResultCancelled.value: 37 println("短信取消发送") 38 case MessageComposeResultFailed.value: 39 println("短信发送失败") 40 default: 41 break 42 } 43 } 44 45 override func didReceiveMemoryWarning() { 46 super.didReceiveMemoryWarning() 47 } 48 }
1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 } 8 9 //开始摇晃 10 override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) { 11 println("开始摇晃") 12 } 13 14 //摇晃结束 15 override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { 16 println("摇晃结束") 17 } 18 19 //摇晃被意外终止 20 override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) { 21 println("摇晃被意外终止") 22 } 23 }
1 import UIKit 2 3 class ViewController: UIViewController { 4 5 @IBOutlet weak var orientationLabel: UILabel! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 //感知设备方向 - 开启监听设备方向 11 UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() 12 //添加通知,监听设备方向改变 13 NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedRotation", 14 name: UIDeviceOrientationDidChangeNotification, object: nil) 15 16 //关闭设备监听 17 //UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications() 18 } 19 20 //通知监听触发的方法 21 func receivedRotation(){ 22 var device = UIDevice.currentDevice() 23 switch device.orientation{ 24 case .Portrait: 25 orientationLabel.text = "面向设备保持垂直,Home键位于下部" 26 case .PortraitUpsideDown: 27 orientationLabel.text = "面向设备保持垂直,Home键位于上部" 28 case .LandscapeLeft: 29 orientationLabel.text = "面向设备保持水平,Home键位于左侧" 30 case .LandscapeRight: 31 orientationLabel.text = "面向设备保持水平,Home键位于右侧" 32 case .FaceUp: 33 orientationLabel.text = "设备平放,Home键朝上" 34 case .FaceDown: 35 orientationLabel.text = "设备平放,Home键朝下" 36 case .Unknown: 37 orientationLabel.text = "方向未知" 38 default: 39 orientationLabel.text = "方向未知" 40 } 41 } 42 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4668953.html