2018-04-17 18:31:05 Brad_Wu 阅读数 1293

Swift实现邮件发送功能


使用mailComposeController可以多快好省地实现邮件发送功能

下面我们做个简单的App实验一下


先期工作:

  1. 新建一个single view app, 进入 Main.storyboard.swift
  2. 拖动一个button到屏幕中间, 命名为发送邮件,字体弄大,彰显气势

弄好之后看起来就是这样:

3.进入软件配置界面(点击文件列表最上面的那个),拖到最下面找到Linked Frameworks and Libraries这一栏,点击+添加MessageUI.framework


代码部分

import UIKit
import MessageUI
class ViewController: UIViewController,MFMailComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //当用户点击“发送邮件”,弹出邮件窗口
    //这里不要忘记control-drag将方法和按钮连接
    @IBAction func sentMail() {
        //0.首先判断设备是否能发送邮件
        if MFMailComposeViewController.canSendMail() {
            //1.配置邮件窗口
            let mailView = configuredMailComposeViewController()
            //2. 显示邮件窗口
            present(mailView, animated: true, completion: nil)
        } else {
            print("Whoop...设备不能发送邮件")
            showSendMailErrorAlert()
        }
    }


    //MARK:- helper methods
    //配置邮件窗口
    func configuredMailComposeViewController() -> MFMailComposeViewController {

        let mailComposeVC = MFMailComposeViewController()
        mailComposeVC.mailComposeDelegate = self

        //设置邮件地址、主题及正文
        mailComposeVC.setToRecipients(["<你的邮箱地址>"])
        mailComposeVC.setSubject("<邮件主题>")
        mailComposeVC.setMessageBody("<邮件正文>", isHTML: false)

        return mailComposeVC
    }


    //提示框,提示用户设置邮箱
    func showSendMailErrorAlert() {

        let sendMailErrorAlert = UIAlertController(title: "未开启邮件功能", message: "设备邮件功能尚未开启,请在设置中更改", preferredStyle: .alert)
        sendMailErrorAlert.addAction(UIAlertAction(title: "确定", style: .default) { _ in })
        self.present(sendMailErrorAlert, animated: true){}
    }


    //MARK:- Mail Delegate
    //用户退出邮件窗口时被调用
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        switch result.rawValue{
        case MFMailComposeResult.sent.rawValue:
            print("邮件已发送")
        case MFMailComposeResult.cancelled.rawValue:
            print("邮件已取消")
        case MFMailComposeResult.saved.rawValue:
            print("邮件已保存")
        case MFMailComposeResult.failed.rawValue:
            print("邮件发送失败")
        default:
            print("邮件没有发送")
            break
        }

        controller.dismiss(animated: true, completion: nil)
    }

}

备注:
模拟器不能实现发送邮件功能,必须真机调试,而且要开启手机的邮件功能之后才能正常发送邮件。
关于iPhone邮件功能开启,可以看: 《iPhone 开启邮件发送功能》

2015-01-16 09:59:58 mjbaishiyun 阅读数 1429

很多伙伴们们总是认为缩放图片的时候很是麻烦,因为不知道从哪里下手,特别是面对新的开发语言,其实图片缩放有很多种方法,但是万变不离其宗,那就是frame,所以,不要想的那么高大上。下面是一个利用蒙板实现的图片缩放demo,感兴趣的伙伴们可以看一下。

//

//  ViewController.swift

//  图片的缩放

//

//  Created by 悦兑科技 on 15/1/15.

//  Copyright (c) 2015 BSY. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    var imageview = UIImageView()

    var button = UIButton()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        // 初始化UIImageView

        var imageview:UIImageView = UIImageView(frame: CGRectMake(100, 100, 100, 100))

        var image = UIImage(named: "1024")

        imageview.image = image

        self.view.addSubview(imageview)

        self.imageview = imageview

        // 初始化UIButton(蒙板)

        var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

        var frame = imageview.frame

        button.frame = frame

        self.view.addSubview(button)

        button.addTarget(self, action: "changeImage:", forControlEvents: UIControlEvents.TouchUpInside)

        self.button = button

        self.button.selected = false

    }

    // 初始化UIButton方法实现

    func changeImage(sender:UIButton){

        [UIView .beginAnimations("", context: nil)];

        [UIView .setAnimationDelay(0.1)];

        [UIView .setAnimationDuration(0.5)];

        self.button.selected = !sender.selected

        if(self.button.selected == true){

        self.imageview.frame = UIScreen.mainScreen().bounds

        }else if(self.button.selected == false){

        self.imageview.frame = CGRectMake(100, 100, 100, 100)

        [UIView.commitAnimations];}}

}





2018-03-13 10:42:59 yqh19880321 阅读数 458

1、添加粒子效果

 func addPteEffect(view:UIView, point : CGPoint)  {
        // 1.创建发射器
        let emitter = CAEmitterLayer()
        // 2.发射器位置
        emitter.emitterPosition = point
        // 3.开启三维效果
        emitter.preservesDepth = true
        var cells = [CAEmitterCell]()
        for i in 0..<10 {
            // 4.设置 Cell(对应其中一个粒子)
            // 4.0.创建粒子
            let cell = CAEmitterCell()
            // 4.1.每秒发射粒子数
            cell.birthRate = Float(arc4random_uniform(4)) + 3
            // 4.2.粒子存活时间
            cell.lifetime = 5
            cell.lifetimeRange = 2.5
            // 4.3.缩放比例
            cell.scale = 0.7
            cell.scaleRange = 0.3
            // 4.4.粒子发射方向
            cell.emissionLongitude = CGFloat(-(Double.pi / 2))
            cell.emissionRange = CGFloat((Double.pi / 4) * 0.6)
            // 4.5.粒子速度
            cell.velocity = 100
            cell.velocityRange = 50
            // 4.6.旋转速度
            cell.spin = CGFloat(Double.pi / 2)
            // 4.7.粒子内容
            cell.contents = UIImage(named: "good\(i)_30x30")?.cgImage
            cells.append(cell)
        }
        // 5.将粒子添加到发射器中
        emitter.emitterCells = cells
        view.layer.addSublayer(emitter)
    }

2、删除粒子效果

func removePteEffect(view:UIView) {

        view.layer.sublayers?.filter({ $0.isKind(of: CAEmitterLayer.self)}).last?.removeFromSuperlayer()
  }



2016-03-16 14:04:36 u014063717 阅读数 365

使用MessageUI.framework框架可以实现短信发送功能,步骤如下:
(1)首先判断设备是否有发送短信功能
(2)如果设备允许发送短信,创建一个MFMessageComposeViewController视图控制器,并对其设置短信内容和收件人号码列表。
(3)短信发送后,会执行回调代理方法,可以获取发送结果(成功,失败或取消)
代码如下:

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 = ["123456","120000"]
            //设置代理
            controller.messageComposeDelegate = self
            //打开界面
            self.presentViewController(controller, animated: true, completion: { () -> Void in

            })
        }
        else{
            print("本设备不能发送短信")
        }
    }

    //发送短信代理
    func messageComposeViewController(controller: MFMessageComposeViewController,
        didFinishWithResult result: MessageComposeResult) {
            controller.dismissViewControllerAnimated(true, completion: nil)
            switch result.rawValue{
                case MessageComposeResultSent.rawValue:
                    print("短信已发送")
                case MessageComposeResultCancelled.rawValue:
                    print("短信取消发送")
                case MessageComposeResultFailed.rawValue:
                    print("短信发送失败")
                default:
                    break
            }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

swift实现归档解档

阅读数 366

swift实现归档解档

博文 来自: qq_34101611

Swift实现单例模式

博文 来自: u010124617
没有更多推荐了,返回首页