gpt4 book ai didi

ios - 如何从 UIAlertViewController 的处理程序正确打开 URL?

转载 作者:可可西里 更新时间:2023-11-01 00:52:56 24 4
gpt4 key购买 nike

我试图在单击警报按钮后打开 URL。我尝试了不同的方法,但都没有奏效。这是我的代码:

let alert = UIAlertController(title: "Warning", message: "Do you want to open the link?", preferredStyle: .Alert);

alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil));
let okAction = UIAlertAction(title: "Open in Safari", style: .Default) {(action) in
UIApplication.sharedApplication().openURL(NSURL(string: "www.google.com")!);
return;
});
alert.addAction(okAction);

self.presentViewController(alert, animated: true, completion: nil);

如果我输入一些像 println 这样的简单函数,它就可以正常工作。

最佳答案

我能够通过使用 url 字符串创建一个名为“link”的变量来让我的工作:

let link = 'http://google.com"

然后我将我的 addAction 设置为:

showAlert.addAction(UIAlertAction(title: "Download", 
style: UIAlertActionStyle.Default, handler: {
(action:UIAlertAction!) -> Void in
UIApplication.sharedApplication().openURL(NSURL(string: link)!)
print("something here... button click or action logging")
}))

希望这对某人有帮助...花了我很长时间才弄清楚如何让处理程序正常工作...

关于ios - 如何从 UIAlertViewController 的处理程序正确打开 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com