UIAlertController是否可以添加超链接?

6

技术: 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];

试图让链接显示在警报框中,但是没有成功。只输出为纯文本。

这是否可能?如果可能,那么如何实现上述示例呢?


不,不可能。UIAlertController仅支持纯文本。 - rmaddy
NSString *msg = [@"" stringByAppendingFormat:@"%@", myAnnotation.notes]; 的意义是什么?为什么不直接使用 NSString *msg = myAnnotation.notes; - rmaddy
这只是一个例子。我正在向警告框中添加更多的注释,因此需要将其附加。 - Michael Sebastian
1
您无法添加超链接,但是可以将链接显示为纯文本,并添加UIAlertAction,以便在单击时重定向到该链接。 - Skywalker
或者更确切地说,我是指添加自定义链接到一个警告操作(添加新操作很容易,只是让超链接正常工作有点麻烦)。 - Michael Sebastian
显示剩余4条评论
1个回答

8

就像这样。

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 Controller中打开链接。


我在这段代码中遇到了一个错误,它说“不兼容的指针类型,将 'NSString *' 发送到类型为 'NSURL * _Nonnull' 的参数”。 - Michael Sebastian
谢谢。此外,这会出现错误:“将'incompatible type'ViewController *const __strong'分配给'id<SFSafariViewControllerDelegate> _Nullable'”在safariViewController.delegate = self;行上。 - Michael Sebastian
1
这可能是因为您的视图控制器(self)没有遵守SFSafariViewControllerDelegate协议。您需要导入“SafariServices”并使视图控制器符合SFSafariViewControllerDelegate。然后该错误将不会发生。如果您的应用程序适用于iOS 8,则可以忽略此步骤。但是,如果支持iOS 9,则最好包括它。 - Skywalker
@Skywalker ,是否有可能使 msg 能够被点击,而不是 defaultAction - Keselme
@Keselme 我不认为这是可能的。也许可以使用 NSMutableAttributedString 的方法,但我不确定。你可以查看这个 - Skywalker
@Keselme 抱歉回复晚了。 - Skywalker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接