Contents

nuginurghani/calldetector

A lightweight Swift library to detect and block app interaction during phone calls on iOS.

Features

  • 🚫 Automatically blocks app interaction during phone calls
  • πŸ“ž Detects incoming, connected, and on-hold call states
  • βš™οΈ Fully configurable UI and messages
  • 🎯 Simple singleton API
  • πŸ”„ Handles multiple simultaneous calls
  • 🧡 Thread-safe implementation
  • πŸ“¦ Swift Package Manager support

Requirements

  • iOS 13.0+
  • Swift 5.9+
  • Xcode 15.0+

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/NugiNurGhani/CallDetector.git", from: "1.0.0")
]

Or add it through Xcode:

  1. File β†’ Add Package Dependencies
  2. Enter the repository URL
  3. Select version

Usage

Basic Setup

In your AppDelegate.swift:

import CallDetector

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        CallDetector.shared.start()
        return true
    }
}

That's it! The library will now automatically show a blocking alert during phone calls.

Custom Configuration

You can customize the alert messages and behavior:

let config = CallDetector.Configuration(
    title: "Call in Progress",
    messageFormat: "%@\n\nPlease end the call to continue using the app",
    statusIncoming: "Incoming Call",
    statusConnected: "Call Connected",
    statusOngoing: "Call in Progress",
    animatePresentation: true
)

CallDetector.shared.configure(config)
CallDetector.shared.start()

Stop Monitoring

To stop call detection:

CallDetector.shared.stop()

API Reference

CallDetector

Methods
  • static var shared: CallDetector - Singleton instance
  • func configure(_ configuration: Configuration) - Configure alert appearance
  • func start() - Start monitoring phone calls
  • func stop() - Stop monitoring and clean up

Configuration

public struct Configuration {
    var title: String
    var messageFormat: String
    var statusIncoming: String
    var statusConnected: String
    var statusOngoing: String
    var animatePresentation: Bool
}

How It Works

CallDetector uses Apple's CallKit framework to observe phone call states:

  1. Monitors all call state changes via CXCallObserver
  2. Shows a non-dismissible UIAlertController when a call is detected
  3. Updates the alert message based on call state (incoming, connected, on hold)
  4. Automatically dismisses the alert when all calls end
  5. Handles app lifecycle events to re-show the alert if needed

Edge Cases Handled

  • βœ… Multiple simultaneous calls
  • βœ… Call switching and merging
  • βœ… Call on hold scenarios
  • βœ… App switching during calls
  • βœ… Background/foreground transitions
  • βœ… Memory management and leaks

Permissions

Add this to your Info.plist:

<key>NSContactsUsageDescription</key>
<string>This app needs access to detect phone calls for security purposes.</string>

Note: CallKit doesn't require explicit permissions, but it's good practice to inform users.

License

MIT License - See LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Created by NugiNurGhani

Support

For issues and feature requests, please use the GitHub issue tracker.

Package Metadata

Repository: nuginurghani/calldetector

Default branch: main

README: README.md