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:
- File β Add Package Dependencies
- Enter the repository URL
- 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 instancefunc configure(_ configuration: Configuration)- Configure alert appearancefunc start()- Start monitoring phone callsfunc 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:
- Monitors all call state changes via
CXCallObserver - Shows a non-dismissible
UIAlertControllerwhen a call is detected - Updates the alert message based on call state (incoming, connected, on hold)
- Automatically dismisses the alert when all calls end
- 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.
Support
For issues and feature requests, please use the GitHub issue tracker.
Package Metadata
Repository: nuginurghani/calldetector
Default branch: main
README: README.md