UIActivityViewController自动关闭

时间:2016-04-12 09:21:14

标签: ios swift uiactivityviewcontroller

我通过这种方式在iPhone上展示活动控制器:

let shareText = track.name
    let shareUrl = NSURL(string: "https://api.test.com/"
    let shareVC = UIActivityViewController(activityItems: [shareText, shareUrl], applicationActivities: nil)
    self.presentViewController(shareVC, animated: true, completion: nil)

我有UITabbarC - > UINavigationC - > UIViewC - >以模态方式呈现UINavigationC - > UIViewC - >从这里打开UIActivityViewC。它打开没有问题,但是当我按下电子邮件应用程序时,它显示接近0.5-1秒并自动关闭。

有人知道是什么原因以及如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您必须在最高或最后一个视图控制器中显示UIActivityViewController。它可以是例如navigationController ..

您还没有指定项目结构,例如可以尝试:

if let vc = self.parentViewController {
   let shareText = track.name
   let shareUrl = NSURL(string: "https://api.test.com/"
   let shareVC = UIActivityViewController(activityItems: [shareText, shareUrl], applicationActivities: nil)
   shareVC.setValue(shareText, forKey: "subject")
   vc.presentViewController(shareVC, animated: true, completion: nil) 
}

如果您的问题仍然存在,请在iPad上测试时查看这些行:

  

展示视图控制器时,必须使用   适用于当前设备的方法。在iPad上,你必须出席   弹出窗口中的视图控制器。在iPhone和iPod touch上,你必须   以模态呈现。

if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone {
        self.presentViewController(shareVC, animated: true, completion: nil)
    }
    else {            
        if activityViewController.respondsToSelector("popoverPresentationController") {
            activityViewController.popoverPresentationController!.barButtonItem = sender as? UIBarButtonItem
            self.presentViewController(shareVC, animated: true, completion: nil)
        }

    }

尝试使用以下方法进行测试:

let shareText: String! = "Hello"
let shareurl: NSURL! = NSURL(string: "http://www.google.com")

(此测试是因为我想排除您的HTTPS网址,utf8编码和围绕shareText的一些奇怪的麻烦)

要记住: 根据Apple论坛here

  

模拟器不支持邮件。您应该尝试测试邮件   设备中的功能。

<强>更新 要隔离邮件问题,请尝试使用此类:

import Foundation
import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

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

    @IBAction func sendEmailButtonTapped(sender: AnyObject) {
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert()
        }
    }

    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

        mailComposerVC.setToRecipients(["someone@somewhere.com"])
        mailComposerVC.setSubject("Sending you an in-app e-mail...")
        mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

        return mailComposerVC
    }

    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    }

    // MARK: MFMailComposeViewControllerDelegate Method
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

如何使用例如:

var emailTitle = "Feedback"
var messageBody = "Feature request or bug report?"
var toRecipients = ["myuser@myemail.com", "my2user@gmail.com"]
var mc:MFMailComposeViewController = MFMailComposeViewController()

mc.mailComposeDelegate = self

mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipients)

一些细节和讨论here ..

答案 1 :(得分:0)

我的观点结构是UITabbarC - &gt; UINavigationC - &gt; UIViewC1 - &gt;以模态方式呈现UINavigationC - &gt; UIViewC2

当我从UIViewC2调用UIActivityViewController时(通过UIViewC1呈现模态)我描述了问题。

但是当我在显示UIActivityViewController之前关闭UIViewC2时 - 它可以正常工作

我的解决方案:

将underController变量添加到UIViewC2并在UIVIewC2呈现之前手动设置

let shareVC = UIActivityViewController(activityItems: [shareText, shareUrl], applicationActivities: nil)

self.dismissViewControllerAnimated(true) {
    self.underController?.presentViewController(shareVC, animated: true, completion: nil)
}