UIActivityViewController Swift!在iPad上崩溃

时间:2015-07-19 19:41:39

标签: ios swift ipad uiactivityviewcontroller

我在Xcode中遇到一个问题我的应用程序崩溃在iPad上的Swift中使用此代码!

我能做什么?

@IBAction func share(sender:UIBarButtonItem){

    let activityViewController = UIActivityViewController (
        activityItems: [(webview.request?.URL!.absoluteString)! as NSString],
        applicationActivities: nil
    )

    presentViewController(activityViewController, animated: true, completion: nil)
}

}

Crash image

3 个答案:

答案 0 :(得分:1)

你不能将viewController保留在任何地方。将其存储在成员var

1

有一个实例变量

var presentedActivityVC

2

在调用当前

之前设置它
presentedActivityVC = activityVC...
presentViewC....

3

presentViewController(activityViewController, animated: true, completion: { presentedAcitivityVC = nil })

在完成区中没有它,所以你不能保留它超过必要的时间

答案 1 :(得分:0)

在将它添加到activityitems数组之前,你应该检查webview.request?.URL!.absoluteString值是不是nil。否则,应用程序将崩溃。

  If let urlstring = webview.request?.URL?.absoluteString as NSString{
    let activityViewController = UIActivityViewController (
        activityItems: [urlstring],
        applicationActivities: nil
    )
presentViewController(activityViewController, animated: true, completion: nil)
}else{
   // show an error
}

答案 2 :(得分:0)

您可以查看以下代码。

//  ViewController.swift
//
//  Copyright © 2015 durul. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var mood = ""

    @IBOutlet weak var textField: UITextField!

    @IBAction func shareButton(sender: AnyObject) {

        mood = textField.text!

        let myShare = "I 'am feeling \(mood) today."

        let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)

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


    }


    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.
    }


}