【问题标题】:Show hyperlink in message in UIAlerController in Objective-c在 Objective-c 的 UIAlerController 中的消息中显示超链接
【发布时间】:2021-07-22 10:53:53
【问题描述】:

我想在 Objective-c 的 UIAlertController 中显示一些带有超链接的文本作为消息。 如果我单击该超链接,它应该重定向我以在 Safari 中打开该链接。

我不想在第三方库中使用。

任何想法我怎样才能做到这一点?

【问题讨论】:

  • 来自 Apple 的文档:The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified. 您应该创建自己的控制器(以模态方式呈现)。

标签: ios objective-c uialertcontroller


【解决方案1】:

没有直接的方式将其显示为超链接。您可以添加自己的消息对话框,也可以在 UIAlertController 中添加一个按钮来打开超链接。

步骤 1. 从您的消息字符串中获取 URL

NSURL *url;

NSString *string = @"This is a sample of a http://example.com/index.html sentence with a URL within it.";
    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    for (NSTextCheckingResult *match in matches) {
      if ([match resultType] == NSTextCheckingTypeLink) {
        url = [match URL];
        NSLog(@"found URL: %@", url);
      }
    }

第 2 步。显示您的警报消息

UIAlertController * alert = [UIAlertController
                                                 alertControllerWithTitle:@“Message”
                                                 message:@"Would you like to open link?”
                                                 preferredStyle:UIAlertControllerStyleAlert];
                    
                    
                    
                    UIAlertAction* yesButton = [UIAlertAction
                                                actionWithTitle:@“Open using Safari” Link
                                                style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * action) {
                                                        UIApplication *app = [UIApplication sharedApplication];
                                                            if ([app canOpenURL:url])
                                                            {               

                                                    [[UIApplication sharedApplication] openURL: url options:@{} completionHandler:^(BOOL success) {
                                                        //completion codes here
                                                    }];
                                                            }



                      
                          }];
                    
                    UIAlertAction* noButton = [UIAlertAction
                                               actionWithTitle:@"No"
                                               style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   //Handle no, thanks button
                                               }];
                    
                    [alert addAction:yesButton];
                    [alert addAction:noButton];
                    
                    [self presentViewController:alert animated:YES completion:nil];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 2016-11-28
    • 2013-07-14
    • 2011-09-30
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多