本文介绍了通过UIActivityViewController(Twitter,Facebook,Instagram)与hashtag共享图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UIActivityViewController与主题标签共享图像,并且在尝试分享到Twitter,Facebook和Instagram时遇到了一些奇怪的行为。似乎没有关于这些服务的共享扩展的大量文档。

I am attempting to share an image with a hashtag using UIActivityViewController and I am encountering some strange behavior when attempting to share to Twitter, Facebook and Instagram. There does not seem to be a lot of documentation about these service's share extensions.

场景1:具有带图像和文本的活动项数组的Init控制器

如果我像这样初始化控制器,Twitter和Facebook将显示在控制器中(没有Instagram,因为它不支持文本项),并且两者都将以编程方式预先-populate文本输入字段中的#标签:

If I initialize the controller like so, Twitter and Facebook will show up in the controller (no Instagram as it does not support text items), and both will programmatically pre-populate the hashtag in the text entry field:

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

场景2:仅带图像的Init控制器

在这种情况下,所有网络都会显示,但我(显然)会丢失自动主题标签功能:

In this scenario, all networks show up, but I (obviously) lose the automatic hashtag feature:

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

场景3:UIActivityItemSource子类

如果我创建自己的UIActivityItemSource子类,我几乎可以完成所有工作。然而,这是我无法弄清楚的,使用我下面的协议方法导致自动主题标签适用于Facebook,但不是Twitter。这怎么可能 - 推特需要一个特殊的密钥吗?如果它在场景#1中工作,必须有一种工作方式...

If I make my own UIActivityItemSource subclass, I can almost get everything to work. However, and this is what I cannot figure out, using the protocol methods as I have below results in the automatic hashtag working for Facebook, but not Twitter. How can this be possible -- is there a special key needed for Twitter? There must be a way for it to work if it works in Scenario #1...

有趣的是,如果我插入一个URL,这种方法适用于Twitter和Facebook(评论出来)。那么为什么地球上的文字不适用于Twitter!?

Interestingly, this method works for both Twitter and Facebook if I insert a URL (commented out). So why on earth won't the text work for Twitter!?

let activityItem = CustomItemSource(image: image, message: "#TestTag")
let activityVC = UIActivityViewController(activityItems: [activityItem], applicationActivities: nil)

...

class CustomItemSource: NSObject, UIActivityItemSource {

    private var image: UIImage!
    private var message: String!

    // MARK: Init
    init(image: UIImage, message: String) {
        super.init()

        self.image = image
        self.message = message
    }

    // MARK: Item Source Protocol
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return image
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        if activityType == .postToTwitter || activityType == .postToFacebook {
            //return ["url": URL(string: "https://www.google.com")!, "image": image]

            return ["text": message, "image": image]
        }
        else {
            return ["image": image]
        }
    }

}


推荐答案

定义两个 UIActivityItemSource 类,一个用于图像,一个用于文本

Define two UIActivityItemSource classes, one for Image and one for Text.

在第一个只返回图像。
第二个为placeHolder返回 NSObject(),并返回文本 nil 取决于活动。通过返回 NSObject(),UIActivity将允许所有服务可用。

In first one only return the image.In second one return NSObject() for placeHolder, and return Text or nil depending on activity. By returning NSObject(), UIActivity will allow all services to be available.

UIActivityViewController(activityItems: [ImageProvider(), TextProvider()], applicationActivities: nil)

and提供者:

class TextProvider: NSObject, UIActivityItemSource {
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return NSObject()
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        if activityType == .postToTwitter || activityType == .postToFacebook {
            return "Tweet with #Hashtag"
        }
        return nil
    }
}

class ImageProvider: NSObject, UIActivityItemSource {
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return UIImage(named: ...)
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        return UIImage(named: ...)
    }
}



解释



首先,密钥并不是真正敏感,唯一的敏感密钥是电子邮件和支持它的应用程序的主题,它在 UIActivityController 的API,我们可以直接设置它。如果你提供带键image或1的 UIImage 并不重要。

Explaination

First of all, Keys are not really sensitive, The only sensitive-key was "subject" for email and apps supporting it, which is implemented in UIActivityController's API and we can set it directly. It doesn't matter if you provide UIImage with key "image" or "1".

当它转过来时如果在 ... itemForActivity ... 方法中没有直接 直接 返回文本,Twitter活动将无效。因此,解决方案是分离项目来源。

As it turns out, Twitter activity will not work if it's text is not returned directly in ...itemForActivity... method. So the solution is to separate item sources.

如果占位符收到除了以外的任何内容,Twitter活动也将无效 String ,但是通过返回 String Instagram活动将无效,所以返回 NSObject() 将忽略类型,并且所有服务都可用。
如果你想限制某些服务使用 UIActivityViewController.excludedActivityTypes

Twitter activity also will not work, if placeholder receives anything other than String, but by returning String Instagram activity will not work, So by returning NSObject() Type will be ignored and all services will be available.if you want to limit some services use UIActivityViewController.excludedActivityTypes

这篇关于通过UIActivityViewController(Twitter,Facebook,Instagram)与hashtag共享图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:24