【问题标题】:SwiftUI UIPasteboard is cleared when posting to instagram-stories发布到 instagram-stories 时清除 SwiftUI UIPasteboard
【发布时间】:2022-08-18 23:26:30
【问题描述】:

我正在从我的应用程序中发布 Instagram 故事,它运行良好。但是,当我在存储 URL 的位置添加 UIPasteboard 字符串值时,将其粘贴到 instagram 链接时它会被清除。我注意到 Instagram 在启动时正在清除粘贴板项目。有什么方法可以将粘贴板项目保留在 Instagram 中?这是我的代码 sn-p:

let urlScheme = URL(string:\"instagram-stories://share\")!
if UIApplication.shared.canOpenURL(urlScheme) {

    UIPasteboard.general.string = Global.hostURL // THIS GETS CLEARED
    
    let pasteBoardItems:Array<[String:Any]> = [
        [\"com.instagram.sharedSticker.backgroundImage\" : background.pngData()!]
    ]
    
    let expirationDate = Date().addingTimeInterval(60 * 5)
    let pasteBoardOptions = [UIPasteboard.OptionsKey.expirationDate: expirationDate]
    UIPasteboard.general.setItems(pasteBoardItems, options: pasteBoardOptions)
    
    UIApplication.shared.open(urlScheme)
}

    标签: ios swift uipasteboard instagram-story


    【解决方案1】:
    UIPasteboard.general.setItems(pasteBoardItems, options: pasteBoardOptions)
    UIApplication.shared.open(shareURL, options: [:])
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        UIPasteboard.general.string = url.absoluteString
       // ☝️.setItems again 0.1s after opening Instagram.
      // or 0.2s, 0.25, 0.3 play around with it until it works.
      // Try to get a value that works 10/10 times. 0.1s wasn't as reliable as 0.2s
    
    }
    

    替代解决方案是 UIApplication.shared.open(shareURL, options: [:])

    在应用程序委托的applicationDidEnterBackground 中运行UIPasteboard.general.string = url.absoluteString

    或者在 SwiftUI 的新生命周期中

    .onChange(of: scenePhase) { scenePhase in
                switch scenePhase {
                case .background:
                        UIPasteboard.general.string = url.absoluteString
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多