简体   繁体   English

在Swift中,如何在UISearchbar中添加带有字符串的URL,并在Safari中自动将其打开?

[英]In Swift, how can I append a URL with a string from a UISearchbar and automatically open it in Safari?

I'm working on an app that will have a search bar where you enter text. 我正在开发一个带有搜索栏的应用程序,您可以在其中输入文本。

I'd like the app then to open "www.example.com/"entered text from search bar" but the only thing that seams to happen is it actually attempts to open www.example.com/dncode (which is not a real URL). 我希望该应用程序然后打开“ www.example.com /”(从搜索栏中输入文字),但是唯一发生的缝隙是它实际上试图打开www.example.com/dncode(这不是真正的网址)。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {

//connection that ties search bar in view to input for viewcontroller

@IBOutlet weak var searchbar: UISearchBar!


override func viewDidLoad() {
    super.viewDidLoad()
    searchbar.delegate = self
}
//activates keyboard etc when searchbar clicked
func searchBarSearchButtonClicked(_ searchbar: UISearchBar) {
    searchbar.resignFirstResponder()
    //(dncode) is string that will equal text as entered into search     bar

    let dncode = String()

    searchbar.text! = dncode

    if let url = URL (string: "https://www.example.com/(dncode)")
    {


        //this section to check and auto open URL in default browser     "Safari"
    if #available(iOS 10.0, *)
    {

        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}
}
}

You are setting the text of search bar equal to your dncode variable and not viceversa. 您正在将搜索栏的文本设置为等于dncode变量,而不是相反。 You have to change your implementation: 您必须更改实现:

func searchBarSearchButtonClicked(_ searchbar: UISearchBar) {
    searchbar.resignFirstResponder()
    //(dncode) is string that will equal text as entered into search bar

    guard let dncode = searchbar.text else { return }

    if let url = URL (string: "https://www.example.com/(dncode)") {
        //this section to check and auto open URL in default browser     "Safari"
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}

You must replace: 您必须更换:

let dncode = String()
searchbar.text! = dncode
if let url = URL (string: "https://www.example.com/(dncode)") 

by 通过

 guard let dncode = searchbar.text else { return }   
 if let url = URL (string: "https://www.example.com/\(dncode)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 3 - 在 Safari 中从 JSON 打开 URL - Swift 3 - Open URL from JSON in Safari 如何打开文件并在其中附加一个字符串,swift - How to open file and append a string in it, swift 如何从野生动物园(Swift)打开应用 - How to open app from safari (swift) 我如何从单个json值中提取字符串并将其快速附加到我的标签中(使用alamofire) - how can I fetch a string from a single json value and append it to my label in swift (using alamofire) 如何在Swift 3中更改UISearchBar“取消”按钮的颜色? - How can I change the color of UISearchBar “cancel” button in Swift 3? 如何使用UISearchBar使用Swift查询字符串列表 - How can I query a list of strings with Swift using the UISearchBar 如何使用Swift在我的应用程序的“今日扩展”中的Safari中打开url - How to open a url in Safari from my Today Extension within my app using Swift 如何从独立的 web 应用程序打开 safari 链接? - How can I open a safari link from standalone webapp? 在searchDisplayController的结果中,如何突出显示在UISearchBar中键入的字符串? - in a searchDisplayController 's results, how can I highlight the string typed in in the UISearchBar? 如何防止iOS自动在Swift中强调数字? - How can I prevent iOS from automatically underlining numbers in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM