1

I'm unable to click hyperlink in UIAlertController. I want to open the external browser on hyperlink click.

-(void)viewDidAppear:(BOOL) animated {
    NSString *htmlString = @"Welcom to AlertView for more information <A href=\"https://www.google.com\"> Click Here </A>";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
                                                                     message:htmlString
                                                              preferredStyle:UIAlertControllerStyleAlert];
    NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                         NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
    documentAttributes:nil error:nil];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        // OK button tappped.
        [self dismissViewControllerAnimated:YES completion:^{ }];
    }];
    [alert setValue:attributedStr forKey: @"attributedMessage"];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:true completion:nil];
}

on click on "Click Here" then should open the external browser. Is possible to get hyperlink click action in UIAlertView?

enter image description here

Yalamandarao
  • 3,852
  • 3
  • 20
  • 27
  • Does this answer your question? [Swift UIAlertController with url in text](https://stackoverflow.com/questions/49754343/swift-uialertcontroller-with-url-in-text) – Roman Podymov Feb 14 '20 at 17:22

2 Answers2

0

I think it's possible but I think it's not recommanded by the Apple guidelines. Instead, you should set the title with "Welcome to this AlertView, for more information check our website" with two buttons "Ok" and "No thanks" for example. The user used to click on the native button instead of a text inside an alert controller. Hope it helps.

Afx
  • 1
  • 1
0

I think it would be better to add a default action button, maybe with the title "More info", that will take care of opening the url, but you can take a look at this same question.

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"Welcome to AlertView" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *linkAction = [UIAlertAction actionWithTitle:@"More info" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString: yourUrl]];
    }];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        // OK button tappped.
        [self dismissViewControllerAnimated:YES completion:^{ }];
    }];
    [alert addAction:linkAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:true completion:nil];
Elvereth
  • 85
  • 8
  • Have no idea why someone downvotes this answer. It's good and easy way to have "More Info" on alert but still follow Apple guidelines. – trungduc Feb 14 '20 at 08:48
  • 2
    @trungduc: Answer is not related to my Question. Adding more info option can be done easily. That is basic one. – Yalamandarao Feb 14 '20 at 08:52
  • @Yalamandarao If it's the case, you should a comment to let him know. For your question, it's possible but there is a high chance that your app will be rejected due to modifying system component. Another way is creating a component which looks exactly like system alert. It will take a while. – trungduc Feb 14 '20 at 09:04
  • @Yalamandarao as I said take a look at the [link](https://stackoverflow.com/questions/34782288/is-it-possible-to-add-a-hyperlink-to-a-uialertcontroller) I posted in my answer, they tried to achieve the same thing as you, so you have **both options** here. – Elvereth Feb 14 '20 at 09:30