技术: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];
尝试获取一个链接显示在警告框中,但没有运气。只输出纯文本。
这甚至可能吗?如果是这样,使用上面的示例,您将如何实现它?
答案 0 :(得分:4)
就像这样。
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浏览器打开,因为iOS 8中不存在SFSafariController。这样做是因为Apple现在认为它的用户体验不好退出应用程序以转到外部链接。因此,您可以在Web视图或Safari控制器中打开链接。