从this question复制我的答案
这种事情曾经使用Twitter Kit 来完成,但是,Twitterdropped support 用于 TwitterKit。
从 2018 年 10 月 31 日起,我们将不再积极参与 GitHub 上的开源 SDK(iOS、Android、Unity),或接受问题和拉取请求。在此日期之后,我们还将停止通过 Cocoapods、Carthage 和 Bintray JCenter 发布 SDK。 GitHub 上所有三个 SDK 的文档和源代码将在存档状态下继续使用。
此外,使用 Twitter 工具包需要您拥有 Twitter 应用程序并且用户授予您的 Twitter 应用程序访问其帐户信息的权限。
我能够使用Branch.io deep-links 解决这个问题。
TLDR
- 将分支 SDK 添加到您的项目中。
- 使用要共享的图像的 url 和任何其他附加信息创建一个分支 url。
- 将“twitter”添加到您的应用程序
info.plist LSApplicationQueriesSchemes
- 使用引用 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 应用程序,如下所示: