无法使用SLComposeViewController在Twitter上共享图像和文本

问题描述 投票:3回答:2

我只是这样做:

if let vc = SLComposeViewController(forServiceType: SLServiceTypeTwitter) {
    vc.setInitialText("Tweet text")
    vc.add(image)
    present(vc, animated: true)
}

我已经在我的应用程序中完成了这个很长一段时间了,它曾经工作正常,但我刚刚意识到它不再起作用了。现在,只显示图像,文本部分为空。那么......发生了什么,该怎么办?

swift twitter ios11 slcomposeviewcontroller
2个回答
3
投票

尝试使用TWTRComposer而不是SLComposeViewController。 TwitterKit提供了具有相同功能的轻量级TWTRComposer,如果您的应用程序没有明确的登录功能,则会封装授权用户。

let vc = TWTRComposer()
vc.setText("Tweet text")
vc.setImage(UIImage(named: "hi"))
vc.setUrl(URL(string: "https://dev.twitter.com"))
vc.show(from: self, completion: nil)

你可以在这里下载TwitterKit:https://dev.twitter.com/twitterkit/ios/overview


0
投票

自从推特dropped support为TwitterKit以来,这个问题的接受答案已不再有效

在2018年10月31日,我们将不再对GitHub上的开源SDK(iOS,Android,Unity)做出积极贡献或接受问题并提出请求。在此日期之后,我们还将停止通过Cocoapods,Carthage和Bintray JCenter发布SDK。 GitHub上所有三个SDK的文档和源代码仍可在存档状态下使用。

此外,使用Twitter工具包要求您拥有Twitter应用程序,并且用户授予您的Twitter应用程序访问其帐户信息的权限。

我能够使用Branch.io深度链接解决这个问题。

TLDR

  1. 将分支SDK添加到项目中。
  2. 创建一个分支URL,其中包含您要共享的图像的URL以及任何其他附加信息。
  3. 将“twitter”添加到您的应用info.plist LSApplicationQueriesSchemes
  4. 使用引用in this answer的默认深层链接将该链接分享到Twitter。示例:twitter://post?message=\(myBranchUrl)

您可以找到有关将Branch集成到iOS项目here的更多信息

您还可以查看以下示例代码:

let buo = BranchUniversalObject.init(canonicalIdentifier: "content/12345")
buo.title = "My Content Title"
buo.contentDescription = "My Content Description"
buo.imageUrl = "https://lorempixel.com/400/400"
buo.publiclyIndex = true
buo.locallyIndex = true
buo.contentMetadata.customMetadata["key1"] = "value1"

let lp: BranchLinkProperties = BranchLinkProperties()
lp.channel = "facebook"
lp.feature = "sharing"
lp.campaign = "content 123 launch"
lp.stage = "new user"
lp.tags = ["one", "two", "three"]

lp.addControlParam("$desktop_url", withValue: "http://example.com/desktop")
lp.addControlParam("$ios_url", withValue: "http://example.com/ios")
lp.addControlParam("$ipad_url", withValue: "http://example.com/ios")
lp.addControlParam("$android_url", withValue: "http://example.com/android")
lp.addControlParam("$match_duration", withValue: "2000")

lp.addControlParam("custom_data", withValue: "yes")
lp.addControlParam("look_at", withValue: "this")
lp.addControlParam("nav_to", withValue: "over here")
lp.addControlParam("random", withValue: UUID.init().uuidString)

buo.getShortUrl(with: lp) { [weak self] (url, error) in
    if let err = error {
        // Handle Error
    }

    if let branchUrl = url, let urlScheme = URL(string: "twitter://post?message=\(branchUrl)") {
        if UIApplication.shared.canOpenURL(urlScheme) {
            UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
        } else {
            // Twitter not installed
        }
    } else {
        // Url Error
    }
}

这打开Twitter应用程序,看起来像这样:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.