Reputation: 124
In my Flutter app, I am implementing feature like- There will be a button, and if user hit that button, it will take user to specific facebook profile for communicating through Facebook. I have implemented through url_launche, but it's not working. It's showing the error - Could not launch the URL.
here you go with my code:
Future<void> launchFacebookProfile(String profileId) async {
final Uri fbUri = Uri.parse('fb://profile/$profileId'); // Facebook app
final Uri webUri = Uri.parse('https://www.facebook.com/$profileId'); // Fallback web URL
try {
if (await canLaunchUrl(fbUri)) {
// Open the Facebook app
await launchUrl(fbUri, mode: LaunchMode.externalApplication);
} else if (await canLaunchUrl(webUri)) {
// Fallback to opening in a web browser
await launchUrl(webUri, mode: LaunchMode.externalApplication);
} else {
throw 'Could not launch Facebook profile';
}
} catch (e) {
CustomSnack.warningSnack(e.toString());
}
}
Here you go with the provided link- https://www.facebook.com/userID
Here you go with AndroidMenifext.xm file intent:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="whatsapp" android:host="send" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent-filter>
Please help me to solve the issue and implement the feature. Thanks in advance.
Upvotes: -1
Views: 51
Reputation: 996
I implemented it natively with MethodChannel
without using url_launcher
.
androi - kotlin
IOS - swift
Connect native code with MethodChannel in flutter.
final platform = const MethodChannel("your_method_channel_key");
openBrowser(String url) async {
Map<String, String> args = {
"url": url //your url
};
await platform.invokeMethod("openBrowser", args);
}
Now let's open the URL in native code.
kotlin
if(methodCall.method == "openBrowser"){
val arguments = methodCall.arguments as? Map<String, String>
val url = arguments?.get("url")
if (url != null) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(browserIntent)
result.success(true)
} else {
result.error("INVALID_ARGUMENTS", "URL is required", null)
}
}
swift
if methodCall.method == "openBrowser" {
if let args = methodCall.arguments as? [String: String],
let urlString = args["url"],
let url = URL(string: urlString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
result(true)
} else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "Invalid URL", details: nil))
}
}
Please refer to the method channel documentation.
Upvotes: 0