iOS开发——实用篇Swift篇&项目开发常用实用技术
项目开发常用实用技术
实现拨打电话
邮件发送功能的实现

import UIKit import MessageUI class ViewController: UIViewController ,UINavigationControllerDelegate, MFMailComposeViewControllerDelegate{ override func viewDidLoad() { super.viewDidLoad() //首先要判断设备具不具备发送邮件功能 if MFMailComposeViewController.canSendMail(){ let controller = MFMailComposeViewController() //设置代理 controller.mailComposeDelegate = self //设置主题 controller.setSubject("我是邮件标题") //设置收件人 controller.setToRecipients(["a1@hangge.com","a2@hangge.com"]) //设置抄送人 controller.setCcRecipients(["b1@hangge.com","b2@hangge.com"]) //设置密送人 controller.setBccRecipients(["c1@hangge.com","c2@hangge.com"]) //添加图片附件 var path = NSBundle.mainBundle().pathForResource("hangge.png", ofType: "") var myData = NSData(contentsOfFile: path!) controller.addAttachmentData(myData, mimeType: "image/png", fileName: "swift.png") //设置邮件正文内容(支持html) controller.setMessageBody("我是邮件正文", isHTML: false) //打开界面 self.presentViewController(controller, animated: true, completion: nil) }else{ println("本设备不能发送邮件") } } //发送邮件代理方法 func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { controller.dismissViewControllerAnimated(true, completion: nil) switch result.value{ case MFMailComposeResultSent.value: println("邮件已发送") case MFMailComposeResultCancelled.value: println("邮件已取消") case MFMailComposeResultSaved.value: println("邮件已保存") case MFMailComposeResultFailed.value: println("邮件发送失败") default: println("邮件没有发送") break } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
短信发送功能的实现
import UIKit import MessageUI class ViewController: UIViewController ,UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //首先要判断设备具不具备发送短信功能 if MFMessageComposeViewController.canSendText(){ let controller = MFMessageComposeViewController() //设置短信内容 controller.body = "短信内容:欢迎来到hangge.com" //设置收件人列表 controller.recipients = ["] //设置代理 controller.messageComposeDelegate = self //打开界面 self.presentViewController(controller, animated: true, completion: { () -> Void in }) }else{ println("本设备不能发送短信") } } //发送短信代理 func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { controller.dismissViewControllerAnimated(true, completion: nil) switch result.value{ case MessageComposeResultSent.value: println("短信已发送") case MessageComposeResultCancelled.value: println("短信取消发送") case MessageComposeResultFailed.value: println("短信发送失败") default: break } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
手机摇晃的监测和响应
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } //开始摇晃 override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) { println("开始摇晃") } //摇晃结束 override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { println("摇晃结束") } //摇晃被意外终止 override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) { println("摇晃被意外终止") } }
判断设备方向(或监听设备方向的改变)

import UIKit class ViewController: UIViewController { @IBOutlet weak var orientationLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() //感知设备方向 - 开启监听设备方向 UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() //添加通知,监听设备方向改变 NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedRotation", name: UIDeviceOrientationDidChangeNotification, object: nil) //关闭设备监听 //UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications() } //通知监听触发的方法 func receivedRotation(){ var device = UIDevice.currentDevice() switch device.orientation{ case .Portrait: orientationLabel.text = "面向设备保持垂直,Home键位于下部" case .PortraitUpsideDown: orientationLabel.text = "面向设备保持垂直,Home键位于上部" case .LandscapeLeft: orientationLabel.text = "面向设备保持水平,Home键位于左侧" case .LandscapeRight: orientationLabel.text = "面向设备保持水平,Home键位于右侧" case .FaceUp: orientationLabel.text = "设备平放,Home键朝上" case .FaceDown: orientationLabel.text = "设备平放,Home键朝下" case .Unknown: orientationLabel.text = "方向未知" default: orientationLabel.text = "方向未知" } } }
使用相机拍摄照片
通过设置图片控制器UIImagePickerController的来源为UIImagePickerControllerSourceType.Camera,便可以打开相机
import UIKit class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { override func viewDidLoad() { super.viewDidLoad() } //拍照 @IBAction func fromPhotograph(sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(.Camera){ //创建图片控制器 let picker = UIImagePickerController() //设置代理 picker.delegate = self //设置来源 picker.sourceType = UIImagePickerControllerSourceType.Camera //允许编辑 picker.allowsEditing = true //打开相机 self.presentViewController(picker, animated: true, completion: { () -> Void in }) }else{ println("找不到相机") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
2,调用前置,后置摄像头
//如果有前置摄像头则调用前置摄像头 if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front){ picker.cameraDevice = UIImagePickerControllerCameraDevice.Front }
3,设置闪光灯
通过cameraFlashMode属性可以设置闪光灯:开启/关闭/自动
系统声音服务的使用(播放声音,提醒,震动)
(1)声音播放
@IBAction func systemSound(sender: AnyObject) { //建立的SystemSoundID对象 var soundID:SystemSoundID = //获取声音地址 var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav") //地址转换 var baseURL = NSURL(fileURLWithPath: path!) //赋值 AudioServicesCreateSystemSoundID(baseURL, &soundID) //播放声音 AudioServicesPlaySystemSound(soundID) }
(2)提醒
@IBAction func systemAlert(sender: AnyObject) { //建立的SystemSoundID对象 var soundID:SystemSoundID = //获取声音地址 var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav") //地址转换 var baseURL = NSURL(fileURLWithPath: path!) //赋值 AudioServicesCreateSystemSoundID(baseURL, &soundID) //提醒(同上面唯一的一个区别) AudioServicesPlayAlertSound(soundID) }
(3)振动
@IBAction func systemVibration(sender: AnyObject) { //建立的SystemSoundID对象 var soundID = SystemSoundID(kSystemSoundID_Vibrate) //振动 AudioServicesPlaySystemSound(soundID) }
判端网络连接状态,连接类型(3G还是Wifi)

使用样例:
import UIKit class ViewController: UIViewController { @IBOutlet weak var statusLabel: UILabel! @IBOutlet weak var typeLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func checkConnect(sender: AnyObject) { //判断连接状态 if IJReachability.isConnectedToNetwork(){ statusLabel.text = "网络连接:可用" }else{ statusLabel.text = "网络连接:不可用" } //判断连接类型 let statusType = IJReachability.isConnectedToNetworkOfType() switch statusType{ case .WWAN: typeLabel.text = "连接类型:移动网络" case .WiFi: typeLabel.text = "连接类型:WiFi" case .NotConnected: typeLabel.text = "连接类型:没有网络连接" } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
iOS开发——实用篇Swift篇&项目开发常用实用技术的更多相关文章
- ios开发——实用技术篇Swift篇&播放MP3
播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...
- ios开发——实用技术篇Swift篇&地址薄、短信、邮件
//返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...
- ios开发——实用技术篇Swift篇&拍照
拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...
- ios开发——实用技术篇Swift篇&照片选择
照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...
- ios开发——实用技术篇Swift篇&系统声音
系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...
- ios开发——实用技术篇Swift篇&视频
视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...
- ios开发——实用技术篇Swift篇&录音
录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...
- ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...
- ios开发——实用技术篇Swift篇&多点触摸与手势识别
多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ...
- 【CC2530入门教程-01】IAR集成开发环境的建立与项目开发流程
[引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:1.CC2530单片机开发入门.2.通用I/O端口的输入和输出.3.外部中断初步应用.4.定时/计 ...
随机推荐
- 我们究竟什么时候可以使用Ehcache缓存
一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...
- JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码
本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 提升到 20 (提升了 7 倍) ...
- CMDB反思2
当云灭掉CMDB http://blog.vsharing.com/xqscool/A1193910.html 虽然之前也思考过当运维底层都被替换为云时,现有的传统运维可能就消失了,其所依赖的ITIL ...
- XSLT2.0实用的新功能 .(转)
转自:http://blog.csdn.net/crystalbruce/article/details/7407631 2007年1月,W3C发布了XSLT2.0规范,2009年发布了XSLT2.1 ...
- flashback table恢复数据
flashback table恢复数据 flashback table主要是是用undo 表空间的内容,进行对数据修改的回退操作 语法如下: 根据scn号来进行回退 SQL> flashback ...
- 五指CMS 3.0 手动升级方法
- Hibernate之Session对象的相关方法以及持久化对象的状态
一.持久化对象的状态 站在持久化的角度, Hibernate 把对象分为 4种状态: 持久化状态,临时状态,游离状态,删除状态.Session 的特定方法能使对象从一个状态转换到另一个状 ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- Configure the handler mapping priority in Spring MVC
Often times, you may mix use of multiple handler mappings strategy in Spring MVC development. For ex ...
- [C语言 - 1] C语言数据类型
基本数据类型: byte short int unsigned int long long long unsigned long float double char char * The size ( ...