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.定时/计 ...
随机推荐
- 自定义控件:抽屉SlidingDrawer——wrap_content非全屏
android:allowSingleTap 指示抽屉是否可以打开/通过手柄上的一个水龙头关闭. android:animateOnClick 表示所述抽屉是否应该打开/与当用户点击手柄动画关闭 ...
- 1. 搭建NDK集成开发环境
- UITableView 点击展开的实现
推介看下这里的 内容 http://www.cnblogs.com/kenshincui/p/3931948.html IOS8 above UITabliViewCell 利用 autolayou ...
- Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(二)
// Chapter 2介绍的是2d下的投影变换,摘录下了以下定理 Result 2.1. The point x lies on the line l if and only if xTl = 0. ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 list_selection_should_be(self, locator, *items)
def list_selection_should_be(self, locator, *items): """Verifies the selection of sel ...
- ASP.NET单点登录(代码)
[p=25, null, left]由于某些原因,在我们的应用中会遇到一个用户只能在一个地方登录的情况,也就是我们通常所说的单点登录.在ASP.NET中实现单点登录其实很简单,下面就把主要的方法和全部 ...
- 【和我一起学python吧】初学Python,版本如何选择?
早在四年多以前,在我进入英才网之前,去面试过一家海归创业的公司.他们需要的是有unix开发经验的技术人员,但是因为他们当时所处的阶段对很多成熟 技术人员不是很吸引,所以条件放宽为熟悉面向对象的程序开发 ...
- WeChat Official Account Admin Platform API Introduction
Keyword: WeChat API Introduction Message and GeneralAuthor: PondBay Studio[WeChat Developer EXPERT] ...
- dfs.datanode.max.xcievers参数导致hbase集群报错
2013/08/09 转发自http://bkeep.blog.163.com/blog/static/123414290201272644422987/ [案例]dfs.datanode.max.x ...
- 【移动开发】安卓Lab2(02)
Design UI 总共有三个activity 1. MainActitivty: search bar:先用edittext来代替 map:用imageview装图片 2. DetailActiti ...