Cannot share both image and text on Twitter using SLComposeViewController
Asked Answered
G

2

5

I'm simply doing so:

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

I've done this in my app for a long time now and it used to work fine, but I just realized recently it doesn't work anymore. Now, only the image is being displayed, the text section is empty. So... what happened, what to do?

Gael answered 10/11, 2017 at 8:33 Comment(2)
Are you observing this behavior on iOS 11? It looks like SLComposeViewController is being phased out in favour of TwitterKit. They may have turned off the ability to setInitialText, the API may no longer accept this text to show in tweets or the newest version of SLComposeViewController is having its text hijacked by Twitter to not show anymore. Relevant link: dev.twitter.com/twitterkit/ios/migrate-social-frameworkThermionic
Yeah that's right, I just realized it. Using TwitterKit instead works fine.Gael
D
3

Try using TWTRComposer instead of SLComposeViewController. TwitterKit provides a lightweight TWTRComposer with the same capabilities and encapsulates authorising users if your application doesn't have explicit log in functionality.

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)

You can download TwitterKit here: https://dev.twitter.com/twitterkit/ios/overview.

Dodson answered 10/11, 2017 at 8:55 Comment(0)
F
2

The accepted answer to this question is no longer valid since Twitter dropped support for TwitterKit

On October 31, 2018, we will no longer actively contribute to, or accept issues and pull requests on, the open sourced SDKs (iOS, Android, Unity) on GitHub. After this date we will also stop releasing the SDKs through Cocoapods, Carthage, and Bintray JCenter. Documentation and source code for all three SDKs on GitHub will remain available for consumption in an archived state.

Additionally, the use of Twitter kit requires that you have a Twitter application and that the user grants your Twitter application access to their account information.

I was able to solve this problem using Branch.io deep-links.

TLDR

  1. Add branch SDK to your project.
  2. Create a branch url with the url to the image you want to share and any other additional information.
  3. Add "twitter" to your apps info.plist LSApplicationQueriesSchemes
  4. Share that link to Twitter using the default deep links referenced in this answer. Example: twitter://post?message=\(myBranchUrl)

You can find more information on integrating Branch into your iOS project here

You can also checkout some sample code below:

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
    }
}

This open the Twitter app and looks like this: enter image description here

Footworn answered 30/3, 2019 at 14:55 Comment(1)
Hello! Could we add hashtags? ThanksLongrange

© 2022 - 2024 — McMap. All rights reserved.