Swift 3 Firebase数据库获取URLString

时间:2017-01-02 09:10:46

标签: ios swift firebase firebase-realtime-database

通过使用Firebase数据库我正在尝试通过RealTime更新这个旧代码的URL String,因为url有时会变坏,我必须尽快更新它。我尝试了一些理论,没有一个理论正确处理。除了下面的代码..如果链接被破坏id没有实时访问权限更新它..我需要帮助制作一个代码,将更新它RealTime。

<a target="_blank" href="https://imageshack.com/i/plyd7Ztkp"><img src="http://imagizer.imageshack.us/v2/640x480q90/921/yd7Ztk.png" border="0"></a>

<a target="_blank" href="https://imageshack.com/i/plMFPoMRp"><img src="http://imagizer.imageshack.us/v2/640x480q90/921/MFPoMR.png" border="0"></a>

从上图中,如果我要从“www.google.com”更改为任何其他网址,它也应该更新webView。

我希望我对我正在努力完成的事情足够清楚

@IBAction func zhwandoontv(_ sender: Any) {

    webView.frame=self.view.bounds;
    webView.allowsInlineMediaPlayback = true;
    webView.mediaPlaybackRequiresUserAction = false;

    let tolourl = URL(string: "http://38.99.146.180:7777/Zhwandoon.m3u8?user=sgls128196012&session=a9a822ced3f0211c85d67487a252d6497bef40a6bd64e0f01c332de5c9369")
    let toloURLRequest = URLRequest(url: tolourl!)
    webView.loadRequest(toloURLRequest)

}

更新

}

@IBOutlet weak var webView: UIWebView!

var ref:FIRDatabaseReference?
var databaseHandle:FIRDatabaseHandle?

var postData = [String]()


@IBAction func Bahartv(_ sender: Any) {

    ref = FIRDatabase.database().reference()


    databaseHandle = ref?.child("realtimeURL").observe(.childAdded, with: { (snapshot) in
        let post = snapshot.value as? String

        if let actualPost = post {
            self.postData.append(actualPost)

        }
    })


    webView.frame=self.view.bounds;
    webView.allowsInlineMediaPlayback = true;
    webView.mediaPlaybackRequiresUserAction = false;

    let tolourl = self.postData.append(actualPost)
    let toloURLRequest = URLRequest(url: tolourl!)
    webView.loadRequest(tolourl)

}


override func viewDidLoad() {
    super.viewDidLoad()


    ref = FIRDatabase.database().reference()

    databaseHandle = ref?.child("realtimeURL").observe(.childAdded, with: { (snapshot) in
        let post = snapshot.value as? String

        if let actualPost = post {
            self.postData.append(actualPost)

        }
    })

    // Do any additional setup after loading the view.

}

1 个答案:

答案 0 :(得分:0)

好的,我收了你的回复。 现在,我假设问题是您希望在实时关闭时更改您的网址,并且您不知道如何使用Firebase。

为此,我建议您从Firebase阅读此文档:https://firebase.google.com/docs/database/ios/read-and-write

您也可以像这样一步一步地完成:

  1. 在Firebase上创建项目,将其导入iOS项目。
  2. 在Firebase实时数据库上创建一个名为realtimeURL的节点。
  3. 转到zhwandoontv函数,听取realtimeURL更改,然后将其传递给请求。
  4. 刷新请求以及您的webView。干得好。
  5. <强>更新 根据您的代码,我可以像这样调整代码:

    @IBOutlet weak var webView: UIWebView!
    
    var ref:FIRDatabaseReference?
    var databaseHandle:FIRDatabaseHandle?
    
    func loadWebViewWithURL(url: String) {
        webView.frame=self.view.bounds;
        webView.allowsInlineMediaPlayback = true;
        webView.mediaPlaybackRequiresUserAction = false;
    
        let toloURLRequest = URLRequest(url: url)
        webView.loadRequest(tolourl)
    }
    
    func initDatabaseHandle() {
        if ref == nil {
            ref = FIRDatabase.database().reference()
        }
    
        databaseHandle = ref?.child("realtimeURL").observe(.value, with: { (snapshot) in
            if let post = snapshot.value as? String
            {
                self.loadWebViewWithURL(post)
            }
        })
    }
    
    @IBAction func Bahartv(_ sender: Any) {
        if databaseHandle == nil {
            // re-init the handle
            initDatabaseHandle()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        initDatabaseHandle()
    }