IOS iPhone/iPad – check call service capability of the device
sometimes you need to check the type of device that will use our application in order to identify whether the device is an iphone, ipod or iPad. To check this we can read the [UIDevice currentDevice].model such you are essentially comparing the device name with a hard-coded value. Example
if(![[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
UIAlertView *alertView = [UIAlertView alloc] initWithTitle:@"Error"
message:@"Call service not avalible on this device"
delegate:self
cancelButtonTitle:@"close"
otherButtonTitles: nil];
[alertView show];
[alertView release];
}
as alternative we can add a control if the device can openURL “tel:xxx-xxxx-xxxx” which is the protocol used to make a call on an iPhone device:
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:RISTOSTREGA_PHONE]]
more in deep we ca use to know exactly the generation of device the class UIDeviceHardware:
UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];
[h release];
the class UIDeviceHardware.h:
@interface UIDeviceHardware : NSObject
- (NSString *) platform;
- (NSString *) platformString;
@end
in the implementation file UIDeviceHardware.m:
#include
#include
@implementation UIDeviceHardware
- (NSString *) platform{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine];
free(machine);
return platform;
}
- (NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
return platform;
}
@end