繁体   English   中英

在 Objective-c 的 UIAlerController 中显示消息中的超链接

[英]Show hyperlink in message in UIAlerController in Objective-c

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

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

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

没有直接的方法将其显示为超链接。 您可以添加自己的消息对话框,也可以在 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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM