Last active
August 5, 2018 22:11
-
-
Save topherPedersen/092f7d42f2d9fabf634c624f04e3fa74 to your computer and use it in GitHub Desktop.
Basic Networking in Swift/iOS (Simple HTTP GET Request)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This gist is comprised of two files demonstrating how to do simple networking | |
| // in swift using the HTTP GET method. The first file is ViewController.swift, | |
| // and the other is Info.plist. The Info.plist was added to this gist because | |
| // it must be modified in order to use HTTP instead of the more secure HTTPS. | |
| // | |
| // ViewController.swift | |
| // GetRequest | |
| // | |
| // Created by Christopher Pedersen on 8/3/18. | |
| // Copyright © 2018 Christopher Pedersen. All rights reserved. | |
| // | |
| import UIKit | |
| class ViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| // REFERENCE (UIAlertViewController): https://developer.apple.com/documentation/uikit/uialertcontroller | |
| func displayDialog(_ dialogTitle: String, _ dialogMessage: String) { | |
| let alert = UIAlertController(title: dialogTitle, message: dialogMessage, preferredStyle: .alert) | |
| alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in | |
| NSLog("The \"OK\" alert occured.") | |
| })) | |
| self.present(alert, animated: true, completion: nil) | |
| } | |
| // SWIFT NETWORKING REFERENCE: https://digitalleaves.com/complete-guide-networking-in-swift/ | |
| @IBAction func onMainButtonClick(_ sender: Any) { | |
| let httpURL = URL(string: "http://flashtri.com/get.php")! | |
| let httpTask = URLSession.shared.dataTask(with: httpURL) { | |
| (data, response, error) in | |
| guard let _ = data, error == nil else { | |
| DispatchQueue.main.async(execute: { | |
| print("ERROR: PC LOAD LETTER") }) | |
| return | |
| } | |
| let results = String(data: data!, encoding: String.Encoding.utf8) ?? "ERROR: PC LOAD LETTER" | |
| DispatchQueue.main.async(execute: { | |
| self.displayDialog("Data Fetched From Server:", results) | |
| }) | |
| } | |
| DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: { | |
| httpTask.resume() | |
| }) | |
| } | |
| } | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>CFBundleDevelopmentRegion</key> | |
| <string>$(DEVELOPMENT_LANGUAGE)</string> | |
| <key>CFBundleExecutable</key> | |
| <string>$(EXECUTABLE_NAME)</string> | |
| <key>CFBundleIdentifier</key> | |
| <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | |
| <key>CFBundleInfoDictionaryVersion</key> | |
| <string>6.0</string> | |
| <key>CFBundleName</key> | |
| <string>$(PRODUCT_NAME)</string> | |
| <key>CFBundlePackageType</key> | |
| <string>APPL</string> | |
| <key>CFBundleShortVersionString</key> | |
| <string>1.0</string> | |
| <key>CFBundleVersion</key> | |
| <string>1</string> | |
| <key>LSRequiresIPhoneOS</key> | |
| <true/> | |
| <key>UILaunchStoryboardName</key> | |
| <string>LaunchScreen</string> | |
| <key>UIMainStoryboardFile</key> | |
| <string>Main</string> | |
| <key>UIRequiredDeviceCapabilities</key> | |
| <array> | |
| <string>armv7</string> | |
| </array> | |
| <key>UISupportedInterfaceOrientations</key> | |
| <array> | |
| <string>UIInterfaceOrientationPortrait</string> | |
| <string>UIInterfaceOrientationLandscapeLeft</string> | |
| <string>UIInterfaceOrientationLandscapeRight</string> | |
| </array> | |
| <key>UISupportedInterfaceOrientations~ipad</key> | |
| <array> | |
| <string>UIInterfaceOrientationPortrait</string> | |
| <string>UIInterfaceOrientationPortraitUpsideDown</string> | |
| <string>UIInterfaceOrientationLandscapeLeft</string> | |
| <string>UIInterfaceOrientationLandscapeRight</string> | |
| </array> | |
| <!-- REFERENCE (ALLOW HTTP REQUESTS): https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http --> | |
| <key>NSAppTransportSecurity</key> | |
| <dict> | |
| <key>NSExceptionDomains</key> | |
| <dict> | |
| <key>flashtri.com</key> | |
| <dict> | |
| <!--Include to allow subdomains--> | |
| <key>NSIncludesSubdomains</key> | |
| <true/> | |
| <!--Include to allow HTTP requests--> | |
| <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> | |
| <true/> | |
| </dict> | |
| </dict> | |
| </dict> | |
| </dict> | |
| </plist> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment