我需要在此代码中更改要在共享表中显示的文本吗?

时间:2015-05-26 18:01:30

标签: swift

我的文字没有显示在它所说的调试区域中

“[6384:3178597]插件com.apple.share.Facebook.post无效”

我一直使用的代码如下:

@IBAction func Share(sender: AnyObject) { 
    let firstActivityItem = " Hello "
    let ActivityViewController : UIActivityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil) 
    self.presentViewController(ActivityViewController, animated: true, completion: nil)  
}

如果有人能给我添加图片的代码和动作表的链接,那也很有帮助

1 个答案:

答案 0 :(得分:1)

请遵循以下示例:

class DataToShow{
    var url: String = ""
    var text: String = ""
    var image: UIImage?
}

var myData: DataToShow!

@IBAction func shareButtonClick(sender: UIButton) {
    let firstActivityItem = myData.text
    let secondActivityItem : NSURL = NSURL(string: myData.url)!
    let image : UIImage? = myData.image

    var activities = [firstActivityItem, secondActivityItem]
    if image != nil{
        activities.append(image!)
    }

    let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: activities, applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view
    activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros

   //This part is to center it on current UIViewController 
    let x = self.view.center
    activityViewController.view.frame.origin.x = x.x
    activityViewController.view.frame.origin.y = x.y

    activityViewController.popoverPresentationController?.sourceRect = CGRectMake(x.x, x.y, 0, 0)
    activityViewController.popoverPresentationController?.delegate = self

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

//This is for maintening it centered when changing orientation...
override func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) {
    let x = popoverPresentationController.presentingViewController.view.center
    var newRect = CGRectMake(x.x , x.y, 0, 0)
    rect.initialize(newRect)
}

这已在iPad和iPhone上测试过。代码的中心部分仅适用于iPad,在iPhone上它表现不同。