在带有附件的应用程序SMS中 - MFMessageComposeViewControllerDelegate相关错误

时间:2016-01-30 20:57:37

标签: ios swift switch-statement sms mfmessagecomposeview

我有以下代码在应用程序中拍摄我的屏幕的部分快照,然后打开一个SMS对话窗口。

import UIKit
import MessageUI
import AVFoundation


class ViewController: UIViewController, MFMailComposeViewControllerDelegate
{


@IBAction func smsScreenShot(sender: AnyObject) {

    audioPlayer.play()

    // Declare the snapshot boundaries 
    let top: CGFloat = 100
    let bottom: CGFloat = 60

    // The size of the cropped image
    let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)

    // Start the context
    UIGraphicsBeginImageContext(size)
    CGContextTranslateCTM(context, 0, -top)

    // Draw the view into the context (this is the snapshot)
    view.layer.renderInContext(context)
    let snapshot = UIGraphicsGetImageFromCurrentImageContext()

    // End the context (this is required to not leak resources)
    UIGraphicsEndImageContext()

    // Composing the SMS

    if !MFMessageComposeViewController.canSendText() {
        print("SMS services are not available")
    }

    if (MFMessageComposeViewController.canSendText()) {

        let composeVC = MFMessageComposeViewController()

        composeVC.recipients = ["Enter tel no"]
        composeVC.body = "Have a look at this cool image!";

        // Attaching the image to the SMS.
        let image = snapshot
        let imageData = UIImagePNGRepresentation(image)
        composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myImage")

        self.presentViewController(composeVC, animated:true, completion:nil)

    }

}

我意识到我必须在'{MFMailComposeViewControllerDelegate'之后的MFMessageComposeViewControllerDelegate构造中添加class

这样做,我收到以下错误:

Type 'ViewController' does not conform to protocol 'MFMessageComposeViewControllerDelegate'

这是因为我没有解雇控制器。

为了解雇MFMMessageComposeViewController,我添加了以下代码:

func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {

switch result.value {
case MessageComposeResultCancelled.value:
    NSLog("cancelled")
case MessageComposeResultFailed.value:
    NSLog("cancelled")
case MessageComposeResultSent.value:
    NSLog("cancelled")
default:
    NSLog("default...")
}
controller.dismissViewControllerAnimated(true, completion: nil)
}

我刚刚插入此代码:

self.presentViewController(composeVC, animated:true, completion:nil)

但是现在我遇到了同样的Type 'ViewController' does not conform to protocol 'MFMessageComposeViewControllerDelegate'错误以及switch语句中的错误。

切换声明错误

首先,我使用result.value时出错,因此我将其更改为result.rawValue。这似乎解决了这个错误。

但我现在在case语句中有错误:Value of type 'MessageComposeResult' has no member 'value'。我认为这是因为围绕MessageComposeResult的语法已过时或错误?

如何修复我的个案陈述?我想如果我修复案例陈述,我的所有问题都会得到解决吗?

我花了几个小时在网上搜索类似的问题和材料,但我真的很难过。

有人可以告诉我哪里出错了吗?以及如何修改我的代码?

非常感谢。

修订后的代码

import UIKit
import MessageUI
import AVFoundation


    class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate {

   // Composing the SMS

    if !MFMessageComposeViewController.canSendText() {
        print("SMS services are not available")
    }

    if (MFMessageComposeViewController.canSendText()) {


        let composeVC = MFMessageComposeViewController()


        composeVC.recipients = ["Enter tel no"]
        composeVC.body = "Have a look at this cool image!";
        composeVC.messageComposeDelegate = self;

        // Attaching the image to the SMS.

        let image = snapshot
        let imageData = UIImagePNGRepresentation(image)
        composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myImage")



        self.presentViewController(composeVC, animated:true, completion:nil)

        func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
            switch result {
            case MessageComposeResultCancelled:
                NSLog("Cancelled")

            case MessageComposeResultFailed:
                NSLog("Failed")

            case MessageComposeResultSent:
                NSLog("Sent")

            default:
                NSLog("Unknown result")

            }

        }

1 个答案:

答案 0 :(得分:1)

您的问题是您使用的是MFMessageComposeViewController,但您说您的课程是MFMailComposeViewControllerDelegate - 消息 vs 邮件。您已为MFMessageComposeViewController实施了委托方法,该方法与您的类的声明不匹配,因此您会收到错误。

你想要

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate

然后

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
    switch result {
    case MessageComposeResultCancelled:
        NSLog("Cancelled")

    case MessageComposeResultFailed:
        NSLog("Failed")

    case MessageComposeResultSent:
        NSLog("Sent")

    default:
        NSLog("Unknown result")

    }

}

此外,您还没有将视图控制器设置为委托,因此不会调用委托方法:

let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate=self