I am trying to build a Unity plugin that can detect if the Phone is in a phone call or not. I want to prevent the Game Video recorder during a phone call.
I write this Objective-C code and a corresponding C# class in Unity like this:
Objective - C plugIn
PhoneCall.h
#import <Foundation/Foundation.h>
#import <CallKit/CallKit.h>
@interface PhoneCallDelegate : NSObject
{
}
- (BOOL) isOnPhoneCall;
@end
the Implementation :
#import "PhoneCall.h"
@implementation PhoneCallDelegate
- (id)init
{
}
-(BOOL) isOnPhoneCall {
CXCallObserver *callObserver = [[CXCallObserver alloc] init];
for (CXCall *call in callObserver.calls)
{
if (call.isOnHold || call.isOutgoing || call.hasConnected) {
return true;
}
}
return false;
}
@end
static PhoneCallDelegate* delegateObject = nil;
// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
BOOL _InPhoneCall()
{
return ([delegateObject isOnPhoneCall]);
}
}
the Unity C# Class PhoneCallController.cs
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class PhonCallPlugin
{
/* Interface to native implementation */
[DllImport("__Internal")]
private static extern bool _InPhoneCall();
public static bool IsDuringPhoneCall()
{
return _InPhoneCall();
}
}
then I am trying to call IsDuringPhoneCall()
Method threw this Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhoneCallController : MonoBehaviour
{
void Update()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
Debug.Log(" During a Phone Call : " + PhonCallPlugin.IsDuringPhoneCall());
}
}
}
And i tried to call the Device then Launch the app , but the Method always return False.
I dig more into the code and return the count of Calls and it turns out that it always returns 0
.
Why does that Happen? And How can i know if the Phone is During a phone Call in Unity?
Thanks in Advance