Opening other apps (Facebook, Twitter, Instagram)

6,063 views
Skip to first unread message

Jonah Greenthal

unread,
Sep 9, 2018, 11:41:09 PM9/9/18
to Flutter Dev
Is it possible to open third-party apps (specifically Facebook, Twitter, and Instagram), assuming they're installed, from a Flutter app?

Ideally I would be able to open to a post composer, maybe even with a post started with text my app specifies. I know that's asking a lot; I'd settle for just opening the app.

I'd also like to fall back to opening a web page (as with url_launcher) if the app is not installed, but if that's not possible I'd settle for doing nothing if the app is not installed.

Ryan Gonzalez

unread,
Sep 10, 2018, 4:02:58 PM9/10/18
to Jonah Greenthal, Flutter Dev
There are a couple of different ways to approach this:

Option 1. Sharing

The share package (https://pub.dartlang.org/packages/share) allows you to share arbitrary text to the platform's native sharing system. 

Pros:
- Cross-platform. 
- The "right" way to do it. 

Cons:
- Can't share to specific apps. 
- Related to the above, there's no way to fall back onto the web. 

Option 2. URIs

You can use the url_launcher package to open a URI. If the app is installed, it'll take over. If not, it'll just go to the web browser. 

Pros:
- Works on Android *and* iOS. 
- Magically falls back to the browser. 
- The next-best way to go if you want to use the "official" methods. 

Cons:
- Only works with Twitter. 


Option 3. Use intents

You can use the android_intent package (https://pub.dartlang.org/packages/android_intent) to directly send the data to the desired app. 

Pros:
- Can fall back to web by catching any errors that come up when spawning the intent. 

Cons:
- Doesn't support Facebook. 
- Android-only. 
- For Twitter, this is basically a more complicated version of option 2. 

Instagram: Something like:

await AndroidIntent(
  action: 'action_send',
  arguments: {'android.intent.extra.STREAM': 'file://path-to-image'},
  package: 'com.instagram.android',
).launch();

Twitter: 

await AndroidIntent(
  action: 'action_view',
  data: 'TWITTER_URI_LIKE_IN_OPTION_2',
  // package: can be used, but omitting it will allow third-party Twitter apps to work
  // package: 'com.twitter.android',
).launch();

Option 4. Using the social network SDK. 

Pros:
- Supports Facebook. 

Cons:
- Would require manually creating custom Flutter bindings. 
- A lot of platform-specific and network-specific code. 
- Only supports Facebook. 

--

Ryan (ライアン)
Yoko Shimomura, ryo (supercell/EGOIST), Hiroyuki Sawano >> everyone else
https://refi64.com/

Jonah Greenthal

unread,
Sep 10, 2018, 4:06:42 PM9/10/18
to Flutter Dev
Ryan, thanks so much for the detailed answer!

Any idea if something better is likely to come down the pipe soon (whether as part of Flutter or third-party) and if so what I should subscribe to to stay in the loop?  ("Something better" would include, e.g., a modification of url_launcher to support Facebook and Instagram.)

Ryan Gonzalez

unread,
Sep 10, 2018, 11:22:00 PM9/10/18
to Jonah Greenthal, Flutter Dev
The ultimate problem is that Flutter can't really do anything about this (well, other than bindings for option 4; fbsdk already exists but is still experimental). Heck, there's not even anything Android can really do about this! Intents would by the target app explicitly receiving them, and special links (like the Twitter one) are just turned into intents by Android. If Facebook won't take any intents, then nothing can be done about. 

That being said, I just checked again, and it turns out you can *sort of* apply option 3 to Facebook, with one gimmick: you can only open a new post screen either empty or with a URL. If it's not empty or a URL, it won't work. (No clue why.)

The code would be like this:

await AndroidIntent(
  action: 'action_send',
  // If you don't want to share a URL, omit this argument *or* just pass an empty mapping. 
  arguments: {'android.intent.extra.TEXT': 'SOME-URL-TO-SHARE'},
  package: 'com.facebook.katana',
).launch();

If you want to fall back to a URL, you could open this in the user's browser via url_launcher: https://www.facebook.com/sharer/sharer.php

Daniel

unread,
Feb 24, 2020, 10:51:00 AM2/24/20
to Flutter Development (flutter-dev)
Hi Ryan,
just saw your awesome answer from back in 2018. I thought maybe you could help me out too.
I try to open the Instagram/Twitter/....profils in the specific App if possible. So your solution 3 sounds like the right one.
Is it possible to implement this (for example open the Instagram Profil in the Instagram app) in flutter?
In Android the intent would look like this: intent://instagram.com/_u//INSERTUSERNAME;package=com.instagram.android;scheme=https;end

Ryan

unread,
Feb 24, 2020, 12:04:08 PM2/24/20
to Daniel, Flutter Development (flutter-dev)
You can use this package to send an intent: https://pub.dev/packages/android_intent

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/180d8814-08b8-4abd-9baf-eed028fe31dd%40googlegroups.com.

Daniel

unread,
Feb 25, 2020, 4:08:25 AM2/25/20
to Flutter Development (flutter-dev)
Yes I saw that. But I wondered how I have to implement the Intent (sorry I am new to coding). 
In the example above you suggested this:
 await AndroidIntent(
  action: 'action_send',
  arguments: {'android.intent.extra.STREAM': 'file://path-to-image'},
  package: 'com.instagram.android',
).launch();
Have I change that to something like this?
await AndroidIntent(
  action: 'action_view',
  arguments: {"http://instagram.com/_u/" + username}, 
  package: 'com.instagram.android',
).launch();
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages