IOS - Check Other App Installed or not in your IOS Device in Flutter Using Native code and without packages

Here you can check other app installed in your IOS device using below flutter code without packages.

Check App Availability in iOS

Certainly! Here's how you can implement the equivalent functionality in Swift for iOS, along with the Dart code to interface with it


Swift (iOS):


import UIKit

import Flutter


@UIApplicationMain

@objc class AppDelegate: FlutterAppDelegate {


    override func application(

        _ application: UIApplication,

        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?

    ) -> Bool {

        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController

        let channel = FlutterMethodChannel(name: "app_availability", binaryMessenger: controller.binaryMessenger)

        channel.setMethodCallHandler({

            [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in

            if call.method == "isAppInstalled" {

                guard let bundleId = call.arguments as? String else {

                    result(FlutterError(code: "INVALID_ARGUMENT", message: "Missing or invalid arguments", details: nil))

                    return

                }

                let isInstalled = self?.isAppInstalled(bundleId: bundleId) ?? false

                result(isInstalled)

            } else {

                result(FlutterMethodNotImplemented)

            }

        })

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)

    }


    private func isAppInstalled(bundleId: String) -> Bool {

        if let url = URL(string: "\(bundleId)://") {

            return UIApplication.shared.canOpenURL(url)

        }

        return false

    }

}


Here you can call IOS native code for getting value using dart code

Dart:


import 'package:flutter/services.dart';


final MethodChannel _channel = MethodChannel('app_availability');


Future<bool> isAppInstalled(String bundleId) async {

  try {

    final bool result = await _channel.invokeMethod('isAppInstalled', bundleId);

    return result;

  } on PlatformException catch (e) {

    print("Failed to check app availability: '${e.message}'.");

    return false;

  }

}

Flutter screen call isAppInstalled() method using Elevated button 

In this method, you would need the app's package name/your.bundle.id (e.g., com.example.yourapp).


Dart:


import 'package:flutter/material.dart';

import 'package:flutter/services.dart';


void main() {

  runApp(MyApp());

}


class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      home: Scaffold(

        appBar: AppBar(

          title: Text('App Availability Checker'),

        ),

        body: Center(

          child: ElevatedButton(

            onPressed: () async {

              bool isInstalled = await isAppInstalled("your.bundle.id"); // Replace "your.bundle.id" with the bundle ID of the app you want to check

              if (isInstalled) {

                print("App is installed on the device");

              } else {

                print("App is not installed on the device");

              }

            },

            child: Text('Check App Availability'),

          ),

        ),

      ),

    );

  }


  Future<bool> isAppInstalled(String bundleId) async {

    try {

      return await MethodChannel('app_availability').invokeMethod('isAppInstalled', bundleId);

    } catch (e) {

      print("Error checking app availability: $e");

      return false;

    }

  }

}



In this Swift code


    We import necessary modules and override the `application(_:didFinishLaunchingWithOptions:)` method in the `AppDelegate` class to handle method calls from Flutter.


    We set up a method channel named `"app_availability"` to communicate with Flutter. The `isAppInstalled(bundleId:)` function checks if the app with the specified bundle ID is installed on the device by attempting to open its URL scheme. If the app can be opened, it means it's installed.


The Dart code remains the same as before, utilizing the `MethodChannel` to invoke the native method for checking app availability.

With Packages


To check if an app is available or installed on a device (specifically iOS) using Flutter, you can use the url_launcher or device_apps package.

Option 1: Using url_launcher

url_launcher is commonly used for opening apps using custom URL schemes. You can check if an app can be opened by checking if the URL scheme is available.

Steps:

  1. Add url_launcher to your pubspec.yaml:

    dependencies:
      url_launcher: ^6.0.9
    
  2. Use the following code to check if the app can be launched:

    import 'package:url_launcher/url_launcher.dart';
    
    Future<bool> isAppInstalled(String urlScheme) async {
      final Uri uri = Uri.parse(urlScheme);
      return await canLaunch(uri.toString());
    }
    
    void checkApp() async {
      String appUrlScheme = 'your-app-scheme://'; // Replace with the URL scheme of the app
      bool isInstalled = await isAppInstalled(appUrlScheme);
      if (isInstalled) {
        print("App is installed");
      } else {
        print("App is not installed");
      }
    }
    

In this method, you'd need to know the URL scheme of the app you want to check. This works well if the app supports custom URL schemes.

Option 2: Using device_apps (More comprehensive method)

The device_apps package can be used to check if any app is installed on the device. It allows you to check by package name or app ID.

Steps:

  1. Add device_apps to your pubspec.yaml:

    dependencies:
      device_apps: ^2.1.1
    
  2. Use the following code to check if an app is installed:

    import 'package:device_apps/device_apps.dart';
    
    void checkApp() async {
      bool isInstalled = await DeviceApps.isAppInstalled('com.example.yourapp'); // Replace with your app's package name
      if (isInstalled) {
        print("App is installed");
      } else {
        print("App is not installed");
      }
    }
    

In this method, you would need the app's package name (e.g., com.example.yourapp).

Notes:

  • Custom URL Scheme: Some apps have custom URL schemes (like whatsapp://, fb://, etc.). If you know the URL scheme of the app you want to check, this can be a simpler solution.
  • App Package Name: For iOS, using package names is more difficult due to restrictions, and it's not as direct as it is on Android. The device_apps package is more useful on Android, but for iOS, you may rely on url_launcher or check App Store availability using their App Store links.

Thank You

Previous Post Next Post