使用Swift自定义Share中的内容

时间:2015-05-13 21:54:42

标签: swift share

我正在为我的游戏制作一个分享按钮。您知道,当您单击时弹出菜单会出现您的选项。我想知道如何让不同的选择说出不同的东西。我也想知道如何制作一个" Save to Camera Roll"选项。我发布下面的代码。

@IBAction func share(sender: UIButton) {

    let firstActivity = "Yes! I scored \(ScoreNumber) in Dotcha! #dotcha @Snowcapps_Dev https://itunes.apple.com/us/app/dotcha!/id977870313?ls=1&mt=8" 

    let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [firstActivity], applicationActivities: il)        
    self.presentViewController(activityViewController, animated: true, completion: nil)        
}

1 个答案:

答案 0 :(得分:2)

如果您想自定义Twitter,Facebook,Mail等文本,您可以通过以下方式从类UIActivityItemProvider继承:

class CustomProvider : UIActivityItemProvider {

   var facebookMessage : String!
   var twitterMessage : String!
   var emailMessage : String!

   init(placeholderItem: AnyObject, facebookMessage : String, twitterMessage : String, emailMessage : String) {
      super.init(placeholderItem: placeholderItem)
      self.facebookMessage = facebookMessage
      self.twitterMessage = twitterMessage
      self.emailMessage = emailMessage
   }

   override func item() -> AnyObject! {

      switch (self.activityType!) {
      case UIActivityTypePostToFacebook:
          return self.facebookMessage
      case UIActivityTypePostToTwitter:
          return self.twitterMessage
      case UIActivityTypeMail:
          return self.emailMessage
      default:
          return "Any Message"
      }        
   }
}

然后您可以通过以下方式呼叫UIActivityViewController,例如:

let firstActivityItem = CustomProvider(placeholderItem: "Default", facebookMessage: "Facebook", twitterMessage: "Twitter", emailMessage: "Mail")
let secondActivityItem : NSURL = NSURL(string: "http://www.google.com")!
let image : UIImage = UIImage(named: "nameofYOurImage.ext")!

let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)    

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

CustomProvider课程中,我只设置了Facebook,Twitter和Mail,但如果你愿意,你可以放更多,这取决于你。

我希望这对你有所帮助。