'How to open Facebook / Twitter app from my iOS app in Xamarin?
I'm developing an iOS app with Xamarin, in C#.
I have a "contact" UIViewController, with some buttons, including Facebook and Twitter.
I need to open the Facebook and Twitter App with a specific Facebook Page and Twitter profile from my app.
What is the best way to do this?
Solution 1:[1]
Try this :
Device.OpenUri(new Uri("fb://page/page_id"));
Device.OpenUri(new Uri("twitter://user?user_id=userid"));
For page_id right click on page and select view source page and find the page_id for facebook and same as for twitter find the userid
Solution 2:[2]
In addition to accepted answer,
following are my finding to open links in external applications.
To open link in browser:
Device.OpenUri(new Uri("http://example.in/index.html"));
To open group in facebook:
Device.OpenUri(new Uri("fb://group/groupname"));
To open page in facebook:
Device.OpenUri(new Uri("fb://page/page_id"));
To open user in instagram:
Device.OpenUri(new Uri("instagram://user?username=yourUserName"));
To open user in twitter:
Device.OpenUri(new Uri("twitter://userName?user_id=userId"));
To open mail app:
Device.OpenUri(new Uri("mailto:[email protected]"));
Solution 3:[3]
You also can use these methods if you are using Xamarin.Forms:
await Launcher.OpenAsync(new Uri("your-link");
await Launcher.TryOpenAsync(new Uri("your-link");
await Launcher.CanOpenAsync(new Uri("your-link");
await Browser.OpenAsync("your-link");
As far as I remember, Browser will try to start the url with it's default application, if installed on the phone when you use:
BrowserLaunchMode.SystemPreferred
Solution 4:[4]
For those arriving at this page after me, the Device class has been deprecated. You should now use the Launcher class from the Xamarin.Essentials package.
Solution 5:[5]
Here is code that worked for me in 2022, in both iOS and Android. It will open in the Twitter/Facebook apps if they're installed, and in the browser if they're not.
private async void OnTwitterClicked(object sender, EventArgs e)
{
if(!await Launcher.TryOpenAsync("twitter://user?user_id=762836470772097024"))
{
await Browser.OpenAsync("https://twitter.com/RingotanApp");
}
}
private async void OnFacebookClicked(object sender, EventArgs e)
{
if (!await Launcher.TryOpenAsync("fb://facewebmodal/f?href=https://www.facebook.com/RingotanApp"))
{
await Browser.OpenAsync("https://www.facebook.com/RingotanApp");
}
}
In iOS, you also need to add the following to Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
<string>fb</string>
</array>
I used this tool to find my numeric Twitter ID.
Another URI option for Facebook is "fb://page/1761091624175851", where the numeric ID comes from "About --> More Info" on Facebook. However, this URI was janky for me - it would first open the page in the app normally, then switch to the web modal used by facewebmodal. I recommend not using it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Himanshu Dwivedi |
| Solution 2 | abpatil |
| Solution 3 | viktormark92 |
| Solution 4 | ibebbs |
| Solution 5 |
