Display number for dialing with text for UIAlertView

Is there a way that part of the text displayed on the UIAlertView iphone will be the phone number that will be dialed when clicked? maybe using tel: somehow?

+3
source share
2 answers

If you like to implement dataDetectorType in the message body, it does not exist. The only way is to subclass UIAlertView and configure the init method as follows:

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    self = [super initWithTitle:title message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
    if (self) {
        CGRect alertFrame = [self frame];

        UITextView myTextView = [[UITextView alloc] initWithFrame:CGRectMake(alertFrame.origin.x + 10, alertFrame.origin.y + 44, 200, 44)];
        [myTextView setEditable:NO];
        [myTextView setBackgroundColor:[UIColor clearColor]];
        [myTextView setDataDetectorTypes:UIDataDetectorTypeAll];
        [myTextView setText:@"http://www.apple.com"]; // Use your original message string from init

        [self addSubview:myTextView];
        [myTextView release]
    }
    return self;
}

I tested it right now and it works, but you need to spend a bit to make it presentable: P

Perhaps using the method posted by Jhaliya is quick and clean.

+2
source

, , (UIAlertViewDelegate) UIAlertView. UIAlertView, .

@property(nonatomic, copy) NSString *message
@property(nonatomic, copy) NSString *title

, , .

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

tel:.


, .

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //Access `title` and `message` of your **UIAlertView**
    NSString* alertTitle = alertView.title;
    NSString* alertMessage = alertView.message;
    // Formatted the phone number and assign it to a string.
    NSString* myFormattedPhNumber = /*Use StringWithFormat function of NSString */;
    if (buttonIndex == 0)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:myFormattedPhNumber];
    }
}
0

All Articles