Is it possible to add a hyperlink to a UIAlertController?

浪尽此生 提交于 2019-12-07 07:06:57

问题


Technology: Objective-C, xCode 7

@property (nonatomic, copy) NSString *notes;
@synthesize notes;

example.notes = @"Click <a href='http://www.google.com'>here</a>";

NSString *msg = [@"" stringByAppendingFormat:@"%@", myAnnotation.notes];

UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle: @"Title of Alert Box" message: msg preferredStyle: UIAlertControllerStyleAlert];
[alertViewController addAction: [UIAlertAction actionWithTitle: @"Close" style: UIAlertActionStyleCancel handler: nil]];
[self presentViewController: alertViewController animated: YES completion: nil];

Trying to get a link to appear in the Alert Box, but having no luck. Just outputs as plain text.

Is this even possible? If so, using my example above, how would you implement it?


回答1:


Just like this.

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert Title"
                               message:msg
                               preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"GO" 
                                   style:UIAlertActionStyleDefault    
                                   handler:^(UIAlertAction * action) 
{
    NSString *urlString = @"someurl.com";
    NSURL *url = [NSURL URLWithString:urlString];
    if (NSClassFromString(@"SFSafariViewController"))
    {
        SFSafariViewController *safariViewController = [[SFSafariViewController alloc]initWithURL:url];
        safariViewController.delegate = self;
        [self presentViewController:safariViewController animated:YES completion:nil];
    }
    else
    {
        if ([[UIApplication sharedApplication] canOpenURL:url])
        {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

I've added code for opening link in SFSafariController only for iOS 9. In iOS 8 devices, it will open with safari browser as SFSafariController does not exist in iOS 8. This is done because apple now considers it bad user experience to exit the app to go to external links. So you can either open links in a web view or Safari Controller.



来源:https://stackoverflow.com/questions/34782288/is-it-possible-to-add-a-hyperlink-to-a-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!