Swift 3 and JSON – Updating the database by runnin

2019-06-02 07:51发布

1. Clicking the link causes a database update.

There is a certain link I have access to (let's pretend it's www.google.com), such that when I open it up in my browser, it updates a certain section of the JSON code in my database. Based on the numbers that make up a portion of the link, it adjusts a certain value in the data.


2. How do I run this link in the background of my iOS app?

I need to be able to "open" this link within the app, without actually opening up a UIWebview and visually visiting the site. I just need this JSON data inside the database to update on its own (with the user unaware that it even happened).

The problem I'm having here is that I simply don't know how this is done. How do I cause this link to be visited without opening up a Safari browser?

2条回答
\"骚年 ilove
2楼-- · 2019-06-02 08:19

You could also use the UIWebView without ever showing it, like this (Swift 3):

    func webView() {

    let theWebView: UIWebView

    theWebView = UIWebView(frame: UIScreen.main.bounds)

    theWebView.delegate = self

    if let theURL = URL(string: "your URL") {
        let request = URLRequest(url: theURL)
        theWebView.loadRequest(request)
    }
}

Just don't add it to the view.

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-02 08:24

The best approach I've found for such functions is to treat them as if they were "AJAX" (or "REST", "API", etc.) - while these terms are often used (and for more seasoned programmers instantly give a certain thought), the end result is that they take information from your 'originator' and send to the 'server' for processing, which then replies with a 'response'. Once you get that concept in your head, this becomes a fairly simple activity.

(for our example, I will call this "API", as that does really suit {as @Mortiz suggested} this question best)

For Swift 3, there are several ways to do this, I'll show you two I've found and use for various functions:

  1. DispatchQueue

    For a 'one-time shot to a url that I know exists and will connect reliability', this is a good one to use (think of it as a 'quick-n-dirty' if you like....!)

               DispatchQueue.global().async {
                    let data = try? Data(contentsOf: theURL!) //make sure your url does exist, otherwise unwrap in a if let check / try-catch
                    DispatchQueue.main.async {
                        // do stuff here with the data if you need
                        // you can get the response from the server and parse it out, set buttons in the app, etc.
                    }
                }
    
  2. Alamofire

    For Swift 3, Alamofire is extremely popular and does a lot of great stuff. Check it out if you haven't already!

    Alamofire.request("\(theURL!)").responseJSON { response in
    print("result is ", response.result)
    switch response.result {
    case .success(let value):
        // do stuff with the returned data
        // like updating your internal database, etc.
        print(value)
    case .failure(let error):
        print("There was an error")
        // you can see the error response in various ways....
        print(requested)
        print(error)
        print(response)
        print(response.result)
    }
    

    }

Once you have your buttons in place (from your description it sounds like that is what your #1 is about), then in the function you call when it is clicked, drop in the code from above and 'do stuff' as you need.

This will make the update to the server automatically in the background (answering your #2) - the user won't notice anything unless there are connection issues to the internet, etc. (much too complex to get into here, though if you expect to have much of it, Alamofire is a great choice as it automatically retries, etc. (part of the great features you should check out)

A key piece of this is that you can take the response from the URL (send various bits of JSON data back from the server, then break it back down in the phone) and do 'whatever' with it.

Some things you can do (to hopefully give you more ideas - which is just about anything.....):

  • update data in the app (local storage, local variables, etc.)
  • update text (color, background) inside Buttons or Labels
  • process Alerts to the user (not your case, but sometimes you want to let them know what went on - certainly if it was an error in updating your server)
  • change Images (various things)
  • switch Views

Well, the list is as long as "things you can do in an app", so decide for yourself what you need to mod/update - this is "the" way to do it!

查看更多
登录 后发表回答