Friday, December 30, 2016

Web access in Swift 3

Xcode 8.2.1 is used for the development of web access below.

Use UIWebView to load a webpage or display html string. Let's define an IBOutlet for a UIWebView component as following.

    @IBOutlet weak var webView: UIWebView!

1. Use loadRequest to load a URL request.
        if let myURL = URL(string:"https://stackoverflow.com") {   
            /// Use loadRequest to load a URL
            webView.loadRequest(URLRequest(url: myURL))
        }

2. Use loadHTMLString to display HTML string.
        ///Use loadHTMLString to diplay HTML string in web view.
        webView.loadHTMLString("<h1>Hello</h1>", baseURL: nil)

Sometimes we need to get the contents of a URL to do some parsing. Below is an example to use the dataTask function of a URLSession to load the contents of a URL

        ///Use dataTask function to get contents for a URL
        if let myURL = URL(string:"https://stackoverflow.com") {
            let task = URLSession.shared.dataTask(with: myURL, completionHandler: {
                (data, respose, error) in
                
                if error != nil {
                    print(error as Any)
                } else {
                    if let unwrappedData = data {
                        let strData = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
                            as! String
                        print(strData)
                    }
                }
            })
            task.resume()
        }

We can also use URLRequest in dataTask function to get the contents of a URL, which is quite similar.
        ///Use dataTask function for a URL request
        if let myURL = URL(string:"https://stackoverflow.com") {
            let myRequest = URLRequest(url: myURL)
            let task = URLSession.shared.dataTask(with: myRequest, completionHandler: {
                (data, response, error) in
                
                if error != nil {
                    print(error as Any)
                } else {
                    if let unwrappedData = data {
                        let strData = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
                            as! String
                        print(strData)
                    }
                }
            })
            task.resume()
        }

The former seems to be more straightforward.

For security reasons, insecure http access is not allowed. Ref[1] talked about details on adding exception domains into.plist. An example to add stackoverflow.com and all its subdomains is below.

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <false/>
  <key>NSExceptionDomains</key>
  <dict>
    <key>stackoverflow.com</key>
    <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSExceptionAllowsInsecureHTTPLoads</key>
      <true/>
    </dict>
  </dict>
</dict>

References
-----------
1. How to Add App Transport Security Exception Domains, 
https://cocoacasts.com/how-to-add-app-transport-security-exception-domains/

No comments:

Post a Comment