'Detect airplane mode on iOS
How can I detect if the phone is in airplane mode? (It's not enough to detect there is no internet connection, I have to be able to distinguish these 2 cases)
Solution 1:[1]
Try using SCNetworkReachabilityGetFlags
(SystemConfiguration framework). If the flags variable handed back is 0 and the return value is YES, airplane mode is turned on.
Check out Apple's Reachability classes.
Solution 2:[2]
You can add the SBUsesNetwork boolean flag set to true in your Info.plist to display the popup used in Mail when in Airplane Mode.
Solution 3:[3]
Since iOS 12 and the Network Framework it's somehow possible to detect if airplane mode is active.
import Network
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.availableInterfaces.count == 0 { print("Flight mode") }
print(path.availableInterfaces)
}
let queue = DispatchQueue.global(qos: .background)
monitor.start(queue: queue)
path.availableInterfaces
is returning an array. For example [en0, pdp_ip0]
. If no interface is available is probably on flight mode.
WARNING
If airplane mode and wifi is active then path.availableInterfaces
is not empty, because it's returning [en0]
Solution 4:[4]
For jailbroken tweaks/apps:
@interface SBTelephonyManager : NSObject
+(id)sharedTelephonyManager;
-(BOOL)isInAirplaneMode;
@end
...
bool isInAirplaneMode = [[%c(SBTelephonyManager) sharedTelephonyManager] isInAirplaneMode];
Solution 5:[5]
We can not get this information without using private libraries. Here is some code but it will not work when carrier signal is not available.
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
double signalStrength = [[dataNetworkItemView valueForKey:@"signalStrengthRaw"] intValue];
if (signalStrength > 0) {
NSLog(@"Airplane mode or NO signal");
}
else{
NSLog(@"signal available");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Felix |
Solution 2 | Zac White |
Solution 3 | BilalReffas |
Solution 4 | Clawish |
Solution 5 | Vikash Rajput |