UIActivityViewController仅为FB,Twitter和Mail共享图像,但不包含消息

时间:2016-05-24 09:13:25

标签: swift facebook uiactivityviewcontroller

所以我使用以下代码来获取所有UIActivityTypes的文本。

        @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return ""
    }

    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        if activityType == UIActivityTypeMessage {
            return "String for message"
        } else if activityType == UIActivityTypeMail {
            return "String for mail"
        } else if activityType == UIActivityTypePostToTwitter {
            return "String for twitter"
        } else if activityType == UIActivityTypePostToFacebook {
            return "String for facebook"
        }
        return nil
    }

    func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
        if activityType == UIActivityTypeMessage {
            return "Subject for message"
        } else if activityType == UIActivityTypeMail {
            return "Subject for mail"
        } else if activityType == UIActivityTypePostToTwitter {
            return "Subject for twitter"
        } else if activityType == UIActivityTypePostToFacebook {
            return "Subject for facebook"
        }
        return ""
    }   
}

这就是有趣的部分发生的地方。我将URL中的图像添加到objectsToShare数组中。如果我想让所有图像都显示出来,这样可以正常工作,但我的问题是我不想显示UIActivityTypeMessage的图像。那个应该只包含文本,而不是图像。我怎么能这样做?

    // Load image to share
let articleURL = URLBuilder.sharedInstance.addOnlyHttps((promo?.imageUrl)!)
KingfisherManager.sharedManager.cache.retrieveImageForKey(articleURL, options: nil) { (image, _) -> () in
    if image != nil {
        objectsToShare.append(image!)
        self.openShareViewController(objectsToShare, sender: sender )
    } else {
        KingfisherManager.sharedManager.downloader.downloadImageWithURL(NSURL(string: articleURL)!, progressBlock: nil, completionHandler: { (image, error, imageURL, originalData) in
            if image != nil {
                KingfisherManager.sharedManager.cache.storeImage(image!, forKey: articleURL)
                objectsToShare.append(image!)
                self.openShareViewController(objectsToShare, sender: sender)
            }
        })

    }
}

1 个答案:

答案 0 :(得分:3)

  1. 要为不同的ActivityItem设置不同的内容,最好的方法是使用 UIActivityItemSource 。有关ActivityItemSource的详细信息,请检查此Documentation
  2. 您已经按照相同的方式进行操作,因此很容易找到问题的解决方案。
  3. 创建两个UIActivityItemSource类,如下所示

    <强>的SourceOne:

    class MyStringItemSourceOne: NSObject, UIActivityItemSource {
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
       return ""
    }
    
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
    
       if activityType == UIActivityTypeMessage {
         return "String for message"
       } else if activityType == UIActivityTypeMail {
        return "String for mail"
       } else if activityType == UIActivityTypePostToTwitter {
        return "String for twitter"
       } else if activityType == UIActivityTypePostToFacebook {
        return "String for facebook"
       }
       return nil
       }
    }
    
  4. 通过添加上面的代码,我们设置要共享的字符串内容。注意根据活动的活动类型我们设置不同的字符串值。

  5. 我们的要求是为所有activityType设置图像,期望 UIActivityTypeMessage ,这样我们就可以创建另一个 UIActivityItemSource

    <强> SourceTwo

     class MyStringItemSourceTwo: NSObject, UIActivityItemSource {
     @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return ""
      } 
    
     @objc func activityViewController(activityViewController:  UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
    
        let Image: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://i.telegraph.co.uk/multimedia/archive/03589/Wellcome_Image_Awa_3589699k.jpg")!)!)!
        if activityType == UIActivityTypeMessage {
           return nil
        } else if activityType == UIActivityTypeMail {
        //return "String for mail"
           return Image
        } else if activityType == UIActivityTypePostToTwitter {
           return Image
        } else if activityType == UIActivityTypePostToFacebook {
           return Image
        }
        return nil
       }
    
    }
    
  6. 接下来将以上两个来源添加到UIActivityViewController中作为 activityitems ,如下所示

    @IBAction func Test(sender: AnyObject) {
    
       let activityVC = UIActivityViewController(activityItems: [MyStringItemSourceOne(),MyStringItemSourceTwo()] as [AnyObject], applicationActivities: nil)
       //NOTE: Instead of using rootviewcontroller go with your own way.
       if let window = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window
       {
          window.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
       }
    }
    
  7. 现在一切都会按照你的要求正常工作。