Is it possible to add a hyperlink to a UIAlertController?

前端 未结 1 1813
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 00:03

Technology: Objective-C, xCode 7

@property (nonatomic, copy) NSString *notes;
@synthesize notes;

example.notes = @\"Click 

        
相关标签:
1条回答
  • 2021-01-13 00:11

    Just like this.

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

    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.

    0 讨论(0)
提交回复
热议问题