Updated 27 April 2023
In this article, we will discuss about deep linking in flutter via flutter_share plugin which provides you a web link that point you to a specific screen of an application which is already installed in your phone. These web browser links can help you navigate to any specific with required data like any news feed, post or promo codes etc.
For an example if somebody want to share a facebook post then a url is shared with the other user and if that user clicks on it, it navigates to that specific facebook post. You may also send any data using query parameters with this url.
Read more about Flutter app development services from mobikul
Create a new flutter project and add latest version of flutter_share plugin under dependencies in pubspec.yaml file of your project as following example and run flutter pub get run command to install this package in your project.
1 2 3 |
dependencies: flutter_share: ^2.0.0 |
Add following line in your class to import the package.
1 |
import 'package:flutter_share/flutter_share.dart'; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<application> <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:host="abc.webkul.com" android:pathPrefix="add path here" android:scheme="https" /> </intent-filter> <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> </application> |
Add following code into your MainActivity class in Android folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
class MainActivity : FlutterActivity() { private val CHANNEL = "com.mlkit" var methodChannelResult: MethodChannel.Result? = null @Override override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> try { methodChannelResult = result; if (call.method.equals("fileviewer")) { var path: String = call.arguments() var file = File(path) val extension = FilenameUtils.getExtension(path) val photoURI: Uri = FileProvider.getUriForFile( context, context.applicationContext.packageName.toString(), file ) val intent = Intent(Intent.ACTION_VIEW) intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.setDataAndType(photoURI, "application/" + extension) if (intent.resolveActivity(getPackageManager()) != null) { //if device have requested extension app then respective app will open println("File extension app found in the device") startActivity(intent); } else { //if device don't have requested extension app then all app option will be visible. println("File extension app Not found in the device") intent.setDataAndType(photoURI, "*/*") startActivity(intent); } result.success(true) } else if (call.method == "initialLink") { val initialUrl = initialLink() if (initialUrl != null) { result.success(initialLink()) } else { result.error("UNAVAILABLE", "No deep link found", null) } } } catch (e: Exception) { print(e) } } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == 101 && resultCode == Activity.RESULT_OK) { methodChannelResult?.success(data?.getStringExtra(CameraSearchActivity.CAMERA_SEARCH_HELPER)) } } fun initialLink(): String? { val uri = intent.data Log.d("adasdasda", uri.toString()) return if (uri != null) { uri.toString(); } else { null } } } |
2. iOS:
Add if not exists one row to the ios/podfile after target runner in iOS folder
1 2 3 4 5 6 |
... target 'Runner' <strong>do</strong> use_frameworks! ... |
Add following code in your App deligate class into iOS folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
extension AppDelegate: FlutterStreamHandler { func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { _eventSink = events return nil } func onCancel(withArguments arguments: Any?) -> FlutterError? { _eventSink = nil return nil } func setLatestLink(_ latestLink: String?) { self.latestLink = latestLink if (_eventSink != nil) { _eventSink?(self.latestLink) } } } |
4. Implementation:
You can use following code to perform share functionality.
1 2 3 4 5 6 7 8 |
Future<void> share() async { await FlutterShare.share( title: 'Webkul share', text: '<a href="https://webkul.com/">Mobile app development company</a>', linkUrl: 'https://webkul.com/', chooserTitle: 'Mobikul: <a href="https://mobikul.com/">eCommerce Mobile App Builder for Android and iOS</a>'); } |
In this article, we have learned about deep linking in flutter with flutter_share plugin.
Thanks for reading this article, for more such techie posts please visit our mobikul blog site .
Happy Coding 🙂
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.