我怎样才能给UIButton提供外部链接?有可能吗?

vin*_*nay 11 iphone

我的UIButton名称为"立即购买".如果任何人触摸按钮,外部链接应该在Safari浏览器中打开.我如何实现这一目标?

vod*_*ang 28

这很简单.你设置了targetselector按钮,然后在里面selector,你打电话给safari打开你的链接.

调用Safari的代码:

Objective-C的

- (void)buttonPressed {
    [[UIApplication sharedApplication] 
        openURL:[NSURL URLWithString: @"https://www.google.co.uk"]];
}
Run Code Online (Sandbox Code Playgroud)

Swift 2.x

UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.co.uk")!)
Run Code Online (Sandbox Code Playgroud)

Swift 3.x

UIApplication.shared.openURL(URL(string: "https://www.google.co.uk")!)
Run Code Online (Sandbox Code Playgroud)