7

Is there a URL Scheme by which one can open the twitter app with a given piece of media pre-selected to post along with a pre-selected message?

I know that the following exists:

twitter://post?message=hello%20world

and you can also designate an account:

twitter://post?message=hello%20world&account=helloworld

I would like to be able to open twitter with a pre-selected image or video in the users camera roll along with a message.

Piotr Tomasik
  • 9,074
  • 4
  • 44
  • 57
  • would `SLComposeViewController` be close to what you're looking to do? – Louis Tur May 15 '15 at 00:59
  • @LouisTur Specifically looking to facilitate posting of videos natively to twitter. `SLComposeViewController` was created circa iOS 6 I believe... way before Twitter allowed videos. – Piotr Tomasik May 15 '15 at 07:59
  • @PiotrTomasik did you find a solution for this? – crooksy88 Jan 28 '16 at 11:32
  • This is can be done with the instagram app to post videos/images, I believe, so it's technically feasible, but I don't know if twitter implements. I suspect the only way is through their standard rest APIs. I can't even find docs for their twitter:// url scheme. – Bjorn Roche Jan 13 '17 at 20:42

1 Answers1

0

Copying my answer from this question

This sorta thing used to be done using Twitter Kit However, 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

DoesData
  • 6,594
  • 3
  • 39
  • 62