How can I open a Twitter tweet using the native Twitter app on iOS?

43,921

Solution 1

This is how you access other apps from your own. Just find the proper url to send for accessing status. I've included a list that should have most of the important ones. Including status finding.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://status?id=12345"]];

twitter://user?screen_name=lorenb

twitter://user?id=12345

twitter://status?id=12345

twitter://timeline

twitter://mentions

twitter://messages

twitter://list?screen_name=lorenb&slug=abcd

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

twitter://post?message=hello%20world&in_reply_to_status_id=12345

twitter://search?query=%23hashtag

Note: It can be important to make sure the user has twitter installed or this will cause a crash. So I recommend adding this in an if statement before you try to send them to twitter.

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]];

Solution 2

I would go with below approach...

NSURL *twitterURL = [NSURL URLWithString:@"fb://profile/<profile_id>"];
if ([[UIApplication sharedApplication] canOpenURL:twitterURL]) {
    [[UIApplication sharedApplication] openURL:twitterURL];
} else {
    WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"webinterface"];

    secondView.headerLabel = @"Facebook";
    secondView.webPath = @"https://www.facebook.com/pages/<link_for_page>";

    [self.navigationController pushViewController:secondView animated:YES];
}

in WebViewViewController I have webview and I am opening link there...

basically its like if you don't have Twitter on iPhone, it will open in WebView...

Share:
43,921
vietstone
Author by

vietstone

Updated on March 09, 2020

Comments

  • vietstone
    vietstone about 4 years

    A tweet can be opened by Safari with a link of this format:

    http://twitter.com/1Direction_X/statuses/197752514391715842
    

    On iOS 5, Twitter is built-in. How can I open the above tweet using the native Twitter app called from my app?

  • Smikey
    Smikey almost 12 years
    Any idea how you can search for a hash tag?? I've tried NSURL *twitterURL = [NSURL URLWithString:[NSString stringWithFormat:@"twitter://search?q=%@", hashTag]]; but no luck...
  • Ryan Poolos
    Ryan Poolos almost 12 years
    I don't know sorry. You may not be able to. THis isn't the entire Twitter API just a URLScheme for the app. They may not have built that function in.
  • Nicolas Miari
    Nicolas Miari almost 12 years
    If the check fails (user does not have Twitter installed) I would fall back to launching the corresponding profile/etc. on Safari
  • Ryan Poolos
    Ryan Poolos almost 12 years
    On binpress I have a Twitter Smart Link that if Twitter fails will open up the link in a UIWebView Modal within the app. binpress.com/app/in-app-twitter-smart-link/204
  • Hafthor
    Hafthor over 11 years
    twitter://search?query=%23hashtag
  • Ryan Poolos
    Ryan Poolos over 11 years
    Nice one @Hafthor I'll add it to the post incase someone else comes along.
  • khunshan
    khunshan about 9 years
    Can I attach a media (picture and|or video) along the message url scheme. (i.e. twitter://post?message=hello%20world)
  • Ryan Poolos
    Ryan Poolos about 9 years
    You could easily attach a link to a picture or video obviously. And Twitter also supports something called mediaIDs. I think it works such as ?message=hello%20world&media_ids= 471592142565957632 but to use mediaIDs you still need to manage uploading content directly to Twitter. I think the easiest way would be to use a more open service like imgur to upload the photo in the background, then grab the link, add it to the tweet, then activate the URL scheme. I don't think Twitter's image service is very open.
  • Ryan Poolos
    Ryan Poolos about 9 years
  • byte51
    byte51 about 9 years
    Is there a URL for sending direct message?
  • moffeltje
    moffeltje about 8 years
    Are you sure this is for opening Twitter..? ;)
  • Fahim Parkar
    Fahim Parkar about 8 years
    @moffeltje : Thanks for catching this... Atlast after 2.5 years... ;)
  • dhanasekar
    dhanasekar almost 8 years
    @RyanPoolos how do I send pre-filled text message as a direct message to my followers using nsurl e.g.: "twitter://messages=(msg) - not working" expecting similar to whatsapp nsurl "whatsapp://send?text=(msg)"
  • Ryan Poolos
    Ryan Poolos almost 8 years
    At the time of this post the Twitter URL scheme didn't support direct messages. Only twitter://post?message=XXX In today's world, its much better to trigger the iOS Twitter Share sheet instead and post without leaving the app.
  • Mr. Lance E Sloan
    Mr. Lance E Sloan almost 7 years
    This isn't actually a schema specific to hashtags. Any message to be sent should be encoded before using it in a URL. During the encoding, any hash signs will become %23.
  • Ricardo
    Ricardo about 5 years
    Is posible share an image? something like twitter://post?message=hello%20world&media_id=4324324
  • Ryan Poolos
    Ryan Poolos about 5 years
    I believe to upload media you'd need to use the Twitter API because you'd need to upload the media as well. However if you just want to include media, you could upload it yourself then pass a url as part of the message. Twitter is pretty good about expanding URLs into visuals.
  • Thomas Clowes
    Thomas Clowes almost 5 years
    I am also interested in passing a media_id to the Twitter app (having already uploaded it to Twitter using their API). Does anyone know if this is possible? media_id= and media_ids= does not seem to work..
  • wass rubleff
    wass rubleff almost 5 years
    Is there a similar URL for switching to one of my multiple accounts set up in the iOS Twitter app?