6

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

Trying to get a link to appear in the Alert Box, but having no luck. Just outputs as plain text.

Is this even possible? If so, using my example above, how would you implement it?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Michael Sebastian
  • 785
  • 3
  • 15
  • 33

1 Answers1

8

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.

Skywalker
  • 1,590
  • 1
  • 18
  • 36
  • I'm getting an error with this code that says Incompatible pointer types sending 'NSString *' to parameter of type 'NSURL * _Nonnull' – Michael Sebastian Jan 14 '16 at 06:16
  • Thanks. Also, this gets an error: "Assigning to 'id _Nullable' from incompatible type 'ViewController *const __strong'" on the safariViewController.delegate = self; line – Michael Sebastian Jan 14 '16 at 14:40
  • 1
    that is probably because your view controller (self), in this case, is not conforming to the SFSafariViewControllerDelegate protocol. You need to import "SafariServices" and make the view controller conform to SFSafariViewControllerDelegate. then that error will not occur. if your app is for iOS 8, then fine. you dont need this.. but if it supports ios 9, then its best to include that – Skywalker Jan 15 '16 at 03:26
  • @Skywalker is it possible to make `msg` clickable instead the `defaultAction`? – Keselme Nov 14 '19 at 08:27
  • @Keselme I don't think its possible. There might be a way of using `NSMutableAttributedString` but I'm not sure. You can check [this](https://stackoverflow.com/questions/21629784/how-can-i-make-a-clickable-link-in-an-nsattributedstring) out. – Skywalker Dec 20 '19 at 10:16
  • @Keselme sorry for the late reply – Skywalker Dec 20 '19 at 10:16