使用SLComposeViewController无法在Twitter上同时分享图像和文本

4
我只是这样做:
if let vc = SLComposeViewController(forServiceType: SLServiceTypeTwitter) {
    vc.setInitialText("Tweet text")
    vc.add(image)
    present(vc, animated: true)
}

我在我的应用程序中已经做了这个很长时间了,以前可以正常工作,但最近我才意识到它不再起作用。现在,只有图像被显示出来,文本部分为空。那么...发生了什么事?怎么办?


3
你是否在iOS 11上观察到这种行为?看起来SLComposeViewController正在被TwitterKit逐步取代。他们可能已经关闭了设置初始文本的功能,API可能不再接受此文本以显示在推文中,或者最新版本的SLComposeViewController的文本已被Twitter劫持而不再显示。相关链接:https://dev.twitter.com/twitterkit/ios/migrate-social-framework - user3078856
1
是的,没错,我刚刚意识到了。使用 TwitterKit 可以正常工作。 - Rob
2个回答

3
尝试使用TWTRComposer代替SLComposeViewControllerTwitterKit提供了一个轻量级的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

1
这个问题的被接受答案已经过时,因为Twitter 停止支持 TwitterKit。

2018年10月31日后,我们将不再积极贡献iOS、Android和Unity的开源SDK,并将在GitHub上停止处理问题和拉取请求。此日期后,我们还将停止通过Cocoapods、Carthage和Bintray JCenter发布SDK。所有三个SDK的文档和源代码将以存档状态保留在GitHub上。

此外,使用Twitter kit需要您拥有一个Twitter应用程序并且用户授权您的Twitter应用程序访问其帐户信息。

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

简而言之

  1. 将分支SDK添加到您的项目中。
  2. 创建一个带有要分享的图像的URL和任何其他附加信息的分支URL。
  3. 在您的应用程序的info.plist LSApplicationQueriesSchemes中添加"twitter"。
  4. 使用默认深度链接在此答案中引用,将该链接分享到Twitter。示例:twitter://post?message=\(myBranchUrl)

您可以在这里找到有关将Branch集成到iOS项目中的更多信息。

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

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 应用程序并显示如下: 在此输入图像描述

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接