ios 使按钮打开链接 - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31628246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:59:05  来源:igfitidea点击:

Make button open link - Swift

iosxcodeswift

提问by Thev

This is the code I have now, taken from an answer to a similar question.

这是我现在拥有的代码,取自对类似问题的回答。

@IBAction func GoogleButton(sender: AnyObject) {
    if let url = NSURL(string: "www.google.com"){
        UIApplication.sharedApplication().openURL(url)
    }
}

The button is called Google Button and its text is www.google.com

该按钮名为 Google Button,其文字为 www.google.com

How do I make it open the link when I press it?

当我按下它时,如何让它打开链接?

回答by Paul.s

What your code shows is the actionthat would occur once the button is tapped, rather than the actual button. You need to connect your buttonto that action.

您的代码显示的是点击按钮后会发生的操作,而不是实际的 button。您需要将您的按钮连接到该操作

(I've renamed the action because GoogleButtonis not a good name for an action)

(我重命名了动作,因为GoogleButton这不是一个动作的好名字)

In code:

在代码中:

override func  viewDidLoad() {
  super.viewDidLoad()

  googleButton.addTarget(self, action: "didTapGoogle", forControlEvents: .TouchUpInside)
}

@IBAction func didTapGoogle(sender: AnyObject) {
  UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!)
}

In IB:

在IB中:

enter image description here

在此处输入图片说明

Edit: in Swift 3, the code for opening a link in safari has changed. Use UIApplication.shared().openURL(URL(string: "http://www.stackoverflow.com")!)instead.

编辑:在 Swift 3 中,在 safari 中打开链接的代码已更改。使用UIApplication.shared().openURL(URL(string: "http://www.stackoverflow.com")!)来代替。

Edit: in Swift 4 UIApplication.shared.openURL(URL(string: "http://www.stackoverflow.com")!)

编辑:在 Swift 4 UIApplication.shared.openURL(URL(string: "http://www.stackoverflow.com")!)

回答by Fruity Geek

The string you are supplying for the NSURLdoes not include the protocol information. openURLuses the protocol to decide which app to open the URL.

您为 提供的字符串NSURL不包括协议信息。openURL使用协议来决定打开 URL 的应用程序。

Adding "http://" to your string will allow iOS to open Safari.

将“http://”添加到您的字符串将允许 iOS 打开 Safari。

@IBAction func GoogleButton(sender: AnyObject) {
    if let url = NSURL(string: "http://www.google.com"){
        UIApplication.sharedApplication().openURL(url)
    }
}

回答by Krishna Thakur

as openUrl method is deprecated in iOS 10, here is solution for iOS 10

由于 iOS 10 中不推荐使用 openUrl 方法,这里是 iOS 10 的解决方案

let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString) as! URL
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)

回答by Suresh Mano

  if let url = URL(string: "your URL") {
        if #available(iOS 10, *){
            UIApplication.shared.open(url)
        }else{
            UIApplication.shared.openURL(url)
        }

    }

回答by Adam Smaka

if iOS 9 or higher it's better to use SafariServices, so your user will not leave your app.

如果 iOS 9 或更高版本最好使用 SafariServices,这样您的用户就不会离开您的应用程序。

import SafariServices

let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)

回答by oscar castellon

In Swift 4

在斯威夫特 4

if let url = URL(string: "http://yourURL") {
            UIApplication.shared.open(url, options: [:])
        }

回答by S Yoshida

For Swift 3.0:

对于 Swift 3.0:

    if let url = URL(string: strURlToOpen) {
        UIApplication.shared.openURL(url)
    }

回答by FreeNickname

The code that you have should open the link just fine. I believe, that you probably just copy-pasted this code fragment into your code. The problem is that the UI component (button) in the interface (in storyboard, most likely) is not connected to the code. So the system doesn't know, that when you press the button, it should call this code.

您拥有的代码应该可以很好地打开链接。我相信,您可能只是将此代码片段复制粘贴到您的代码中。问题是界面中的 UI 组件(按钮)(在故事板中,很可能)没有连接到代码。所以系统不知道,当你按下按钮时,它应该调用这个代码。

In order to explain this fact to the system, open the storyboard file, where your Google Buttonis located, then in assistant editor open the file, where your func GoogleButtoncode fragment is located. Right-click on the button, and drag the line to the code fragment.

为了向系统解释这个事实,打开你所在的storyboard文件Google Button,然后在助手编辑器中打开你的func GoogleButton代码片段所在的文件。右键单击该按钮,然后将该行拖到代码片段上。

If you create this button programmatically, you should add target for some event, for instance, UITouchUpInside. There are plenty of examples on the web, so it shouldn't be a problem :)

如果以编程方式创建此按钮,则应为某些事件添加目标,例如 UITouchUpInside。网上有很多例子,所以这应该不是问题:)

UPDATE:As others noted already, you should also add a protocol to the link ("http://" or "https://"). It will do nothing otherwise.

更新:正如其他人已经指出的,您还应该向链接(“http://”或“https://”)添加一个协议。否则它不会做任何事情。

回答by Garry

For Swift3 , below code is working fine

对于 Swift3 ,下面的代码工作正常

@IBAction func Button(_ sender: Any) { 
   UIApplication.shared.open(urlStore1, options: [:], completionHandler: nil)     
 }

回答by Lisa

This code works with Xcode 11

此代码适用于 Xcode 11

if let url = URL(string: "http://www.google.com") {
     UIApplication.shared.open(url, options: [:])
 }