1

我试着这样做,但它不起作用,文本没有被复制

 if let urlScheme = URL(string: "instagram-stories://share") {

 if UIApplication.shared.canOpenURL(urlScheme) {
                
 let imageData: Data = UIImage(systemName:"pencil.circle.fill")!.pngData()!
 let items:[String: Any] = ["public.utf8-plain-text": "text","com.instagram.sharedSticker.backgroundImage": imageData]
    
 UIPasteboard.general.setItems([items])
    
 UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
 }}

我真的很感激任何建议

4

1 回答 1

0

我能想到的2件事:

首先,我不确定您的数组中的以下数据是否可以被 pastebin 正确处理

let items:[String: Any] = ["public.utf8-plain-text": "text","com.instagram.sharedSticker.backgroundImage": imageData]

接下来,共享活动似乎会导致 PasteBoard 中的数据丢失,因此我可以提供将有效数据放入 PasteBoard 的解决方案(例如,我使用字符串,您可以使用其他内容)来自共享的完成处理程序行动,这样的事情可能会解决它:

UIApplication.shared.open(urlScheme, options: [:]) { (_) in
    UIPasteboard.general.string =
        "click on the screen until the paste button appears: https://google.com"
}

编辑

看来您的设置是正确的,在阅读文档时,IG 故事应该自动处理粘贴,因为它似乎在您执行此 url 方案时检查粘贴板:instagram-stories://share- 所以似乎 IG 检查粘贴板并以编程方式执行粘贴,那就是为什么粘贴板被清除。

也许是因为您选择的图像在黑色 instagram 背景上是黑色的,似乎没有共享任何内容,但使用一些适当的图像,结果似乎很好。

我在阅读他们的文档后注意到的另一件事,他们不再允许你设置标题,我再也找不到这个键了public.utf8-plain-text

我可以提供的另一个分享文本的想法是将文本转换为图像并将其添加为sticker贴纸层,因为贴纸层位于背景图像层的顶部。

您可以找到多种将文本转换为图像的方法,这与您的解决方案无关,这是我使用的一种方法

所以把代码放在一起,我有这个:

// Just an example to convert text to UIImage 
// from https://stackoverflow.com/a/54991797/1619193
extension String {
    
    /// Generates a `UIImage` instance from this string using a specified
    /// attributes and size.
    ///
    /// - Parameters:
    ///     - attributes: to draw this string with. Default is `nil`.
    ///     - size: of the image to return.
    /// - Returns: a `UIImage` instance from this string using a specified
    /// attributes and size, or `nil` if the operation fails.
    func image(withAttributes attributes: [NSAttributedString.Key: Any]? = nil, size: CGSize? = nil) -> UIImage? {
        let size = size ?? (self as NSString).size(withAttributes: attributes)
        return UIGraphicsImageRenderer(size: size).image { _ in
            (self as NSString).draw(in: CGRect(origin: .zero, size: size),
                                    withAttributes: attributes)
        }
    }
}

// Then inside some function of yours
func someFunction() {
    if let urlScheme = URL(string: "instagram-stories://share") {
        
        if UIApplication.shared.canOpenURL(urlScheme) {
            
            let imageData: Data = UIImage(named: "bg")!.pngData()!
            
            let textImage: Data = "Shawn Test".image(withAttributes: [.foregroundColor: UIColor.red,
                                                                      .font: UIFont.systemFont(ofSize: 30.0)],
                                                     size: CGSize(width: 300.0, height: 80.0))!.pngData()!
            
            let items = ["com.instagram.sharedSticker.stickerImage": textImage,
                         "com.instagram.sharedSticker.backgroundImage": imageData]
            
            UIPasteboard.general.setItems([items])
            
            UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
        }
    }
}

然后我在 IG 故事中看到这一点,具有正确的背景和文本作为可以移动的贴纸。

使用 Swift 将数据发送到使用 UIPasteBoard 的 Instagram 故事

使用贴纸的唯一缺点是您无法在 Instagram 中编辑文本。

于 2022-01-18T06:37:07.810 回答