ios - 点击 swift 时 tableviewcell 打开 url

标签 ios swift url webview uitableview

当我在我的 tableview programatticaly 中单击一个单元格时,我一直在尝试打开一个 url,而不必制作一个 webview Controller 并弄乱 seques。关于如何完成这项工作的任何帮助。下面是我试过的代码

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



    if indexPath.row == 0 {
        NSURL(string: "App Store Link")!
    }
    else if indexPath.row == 1 {
        NSURL(string: "Send Us Feedback - Contact On Website")!
    }  else if indexPath.row == 2 {
        NSURL(string: "https://www.instagram.com/prs_app/")!
    }  else if indexPath.row == 3 {
        NSURL(string: "Snapchat")!
    }

}

感谢您的帮助。谢谢

最佳答案

你在找

UIApplication.sharedApplication().openURL(NSURL(string: "http://www.example.com")!)

这将为您打开 safari 浏览器

在评论中编辑对问题的解释
如果你添加枚举或其他常量会更好,但这样做会:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let url : NSURL?

    switch indexPath.section{
    case 0:
        switch indexPath.row{
        case 0:
            url = NSURL(string: "http://section0.row0.com")
        case 1:
            url = NSURL(string: "http://section0.row1.com")
        default:
            return;
        }

    case 1:
        switch indexPath.row{
        case 0:
            url = NSURL(string: "http://section1.row0.com")
        case 1:
            url = NSURL(string: "http://section1.row1.com")
        default:
            return;
        }
    default:
        return;
    }

    if url != nil{
        UIApplication.sharedApplication().openURL(url!)
    }
}

关于ios - 点击 swift 时 tableviewcell 打开 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231463/

相关文章:

ios - 如何在 iOS 上与 XMPP 断开连接?

ios - bundle 标识符(应用程序 ID 后缀)

ios - 快速在 UIButton 内的图像上动画

ios - 使用按钮添加和删除 subview

ios - 展开/折叠最后一个单元格后奇怪的 UITableView 行为

ios - 转换为 sksprite 节点

apache - htaccess 页面到页面重定向和 seo 友好的 url

java - 将字符串数据分配给 URL 对象

URL 上的 Swift Nil 异常

ios - UINavigationControllerPaletteClippingView 覆盖 UIImageView 并接收点击手势