Skip to content

Commit d06ed39

Browse files
committed
Adds ServiceStatus (phone) inquiry for iOS
1 parent 2f0369e commit d06ed39

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

example/lib/main.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class MyApp extends StatelessWidget {
2828
.where((PermissionGroup permission) {
2929
if (Platform.isIOS) {
3030
return permission != PermissionGroup.unknown &&
31-
permission != PermissionGroup.phone &&
3231
permission != PermissionGroup.sms &&
3332
permission != PermissionGroup.storage;
3433
} else {

ios/Classes/PermissionManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "LocationPermissionStrategy.h"
1616
#import "MediaLibraryPermissionStrategy.h"
1717
#import "PermissionStrategy.h"
18+
#import "PhonePermissionStrategy.h"
1819
#import "PhotoPermissionStrategy.h"
1920
#import "SensorPermissionStrategy.h"
2021
#import "SpeechPermissionStrategy.h"

ios/Classes/PermissionManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ + (id)createPermissionStrategy:(PermissionGroup)permission {
8989
return [MediaLibraryPermissionStrategy new];
9090
case PermissionGroupMicrophone:
9191
return [AudioVideoPermissionStrategy new];
92+
case PermissionGroupPhone:
93+
return [PhonePermissionStrategy new];
9294
case PermissionGroupPhotos:
9395
return [PhotoPermissionStrategy new];
9496
case PermissionGroupReminders:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// PhonePermissionStrategy.h
3+
// permission_handler
4+
//
5+
// Created by Sebastian Roth on 5/20/19.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#import "PermissionStrategy.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface PhonePermissionStrategy : NSObject<PermissionStrategy>
14+
15+
@end
16+
17+
NS_ASSUME_NONNULL_END
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// PhonePermissionStrategy.m
3+
// permission_handler
4+
//
5+
// Created by Sebastian Roth on 5/20/19.
6+
//
7+
8+
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
9+
#import <CoreTelephony/CTCarrier.h>
10+
11+
#import "PhonePermissionStrategy.h"
12+
13+
@implementation PhonePermissionStrategy
14+
15+
- (PermissionStatus)checkPermissionStatus:(PermissionGroup)permission {
16+
return PermissionStatusUnknown;
17+
}
18+
19+
- (ServiceStatus)checkServiceStatus:(PermissionGroup)permission {
20+
// https://stackoverflow.com/a/5095058
21+
if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
22+
return ServiceStatusNotApplicable;
23+
}
24+
25+
return [self canDevicePlaceAPhoneCall] ? ServiceStatusEnabled : ServiceStatusDisabled;
26+
}
27+
28+
- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler {
29+
completionHandler(PermissionStatusUnknown);
30+
}
31+
32+
33+
// https://stackoverflow.com/a/11595365
34+
-(bool) canDevicePlaceAPhoneCall {
35+
/*
36+
* Returns YES if the device can place a phone call
37+
*/
38+
39+
// Check if the device can place a phone call
40+
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
41+
// Device supports phone calls, lets confirm it can place one right now
42+
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
43+
CTCarrier *carrier = [netInfo subscriberCellularProvider];
44+
NSString *mnc = [carrier mobileNetworkCode];
45+
if (([mnc length] == 0) || ([mnc isEqualToString:@"65535"])) {
46+
// Device cannot place a call at this time. SIM might be removed.
47+
return NO;
48+
} else {
49+
// Device can place a phone call
50+
return YES;
51+
}
52+
} else {
53+
// Device does not support phone calls
54+
return NO;
55+
}
56+
}
57+
58+
@end

0 commit comments

Comments
 (0)