是否可以将超链接添加到UIAlertController? [英] Is it possible to add a hyperlink to a UIAlertController?

查看:492
本文介绍了是否可以将超链接添加到UIAlertController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

技术:Objective-C,xCode 7

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?

推荐答案

就是这样.

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];

我添加了仅在iOS 9的SFSafariController中打开链接的代码.在iOS 8设备中,它将在Safari浏览器中打开,因为SFSafariController在iOS 8中不存在.之所以这样做,是因为苹果现在认为这样做不利于用户体验,退出应用程序以转到外部链接.因此,您可以在Web视图或Safari控制器中打开链接.

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.

这篇关于是否可以将超链接添加到UIAlertController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆