Make call from iPhone

UIAlertController, NSString, UIDevice

if in NSObject class and class Method import #import <UIKit/UIKit.h>

– (void)makeCall:(NSString*)phoneNumber
{
UIAlertController *anAlertController=[UIAlertController alertControllerWithTitle:phoneNumber message:nil preferredStyle:UIAlertControllerStyleAlert];
//…
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}

UIAlertAction *aAlertAction = [UIAlertAction actionWithTitle:@”Cancel” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@”Cancel”);

}];
UIAlertAction *anAlertAction = [UIAlertAction actionWithTitle:@”Call” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSString *aPhoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:
[[NSCharacterSet characterSetWithCharactersInString:@”+0123456789″]
invertedSet]]
componentsJoinedByString:@””];

NSLog(@”Call”);

aPhoneNumber = [@”tel://” stringByAppendingString:aPhoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:aPhoneNumber]];

}];

[anAlertController addAction:aAlertAction];
[anAlertController addAction:anAlertAction];

[rootViewController presentViewController:anAlertController animated:YES completion:nil];

}

Leave a comment