【问题标题】:Is it possible to add a hyperlink to a UIAlertController?是否可以向 UIAlertController 添加超链接?
【发布时间】:2016-04-19 08:21:17
【问题描述】:

技术: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 只支持纯文本。
  • NSString *msg = [@"" stringByAppendingFormat:@"%@", myAnnotation.notes]; 的意义何在?为什么不NSString *msg = myAnnotation.notes;
  • 这只是一个例子。我正在向警报框中添加更多符号,所以我需要附加它。
  • 您不能添加超链接,但是,您可以将链接显示为纯文本并添加 UIAlertAction,单击它会重定向到链接。
  • @MeharoofNajeeb 嗯,好吧,是的,这听起来像是一条路。您是否碰巧知道添加自定义 AlertAction 的好示例

标签: ios objective-c uialertcontroller


【解决方案1】:

就这样。

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 控制器中打开链接。

【讨论】:

  • 我收到此代码的错误,指出不兼容的指针类型将“NSString *”发送到“NSURL * _Nonnull”类型的参数
  • 谢谢。此外,这会得到一个错误:"Assigning to 'id _Nullable' from in compatible type 'ViewController *const __strong'" on the safariViewController.delegate = self;行
  • 这可能是因为您的视图控制器(self),在这种情况下,不符合 SFSafariViewControllerDelegate 协议。您需要导入“SafariServices”并使视图控制器符合 SFSafariViewControllerDelegate。那么该错误将不会发生。如果您的应用程序适用于 iOS 8,那很好。你不需要这个.. 但如果它支持 ios 9,那么最好包含它
  • @Skywalker 是否可以让msg 可点击而不是defaultAction
  • @Keselme 我认为这不可能。可能有一种使用NSMutableAttributedString 的方法,但我不确定。您可以查看this
猜你喜欢
  • 2016-11-17
  • 2020-09-10
  • 2023-01-11
  • 2013-03-06
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 2021-12-09
  • 2019-12-02
相关资源
最近更新 更多