Swift UIWebView remove Edge - swift

As you can see in the Picture below there are edges.
When I embed the View with a NavigationView, than the edges appears. So how can I avoid this edges??
Thanks
import UIKit
class ShowSubmitViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
var URL : String = ""
var type : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadBrowser()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadBrowser(){
switch type {
case 5:
let htmlString:String! = URL
webView.loadHTMLString(htmlString, baseURL: nil)
default:
let url = NSURL (string: URL);
let requestObj = NSURLRequest(URL: url!);
webView.loadRequest(requestObj);
}
}
}
Picture WebView with Edge

Related

URL validation not working in swift

started learning swift two weeks ago, with no previous programming experience, and I can't for the life of me figure out why this wouldn't work to check for nil. it just crashes when trying to load a web page if the user enters an invalid URL. This is the ENTIRETY of the code.
import UIKit; import WebKit
class ViewController: UIViewController {
#IBOutlet weak var adressBar: UITextField!
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func returnPressed(_ sender: Any) {
if let adressBarText = adressBar.text {
if let myURL = URL(string: adressBarText) {
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
adressBar.resignFirstResponder()
print("EYYYYY")
} else {
print("BOOOO")
}
}
}
}
Try this method
func verifyUrl (urlString: String?) -> Bool {
//Check for nil
if let urlString = urlString {
// create NSURL instance
if let url = NSURL(string: urlString) {
// check if your application can open the NSURL instance
return UIApplication.sharedApplication().canOpenURL(url)
}
}
return false
}
https://stackoverflow.com/a/30130535/8069241

"Init(URL:)" has been renamed to "init(url:)" - swift 3 wkwebview error

First time using webkit alongside swift 3, and I keep getting this error regarding a web view load request. Why is Xcode announcing the renaming but maintaining the error?
var webView: WKWebView!
var websites = ["apple.com", "hackingwithswift.com"]
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://" + websites[0])!
webView.load(NSURLRequest(URL: url as URL) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
As suggested by #LeoDabus, here's the code
class Controller: UIViewController {
var webView: WKWebView!
var websites = ["apple.com", "hackingwithswift.com"]
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://" + websites[0])!
webView.load(URLRequest(url: url) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
}
}
I had the same issue which i solved using following lines of code
let url = NSURL (string: "http://www.sourcefreeze.com")
let requestObj = NSURLRequest(url: url! as URL)
webview.loadRequest(requestObj as URLRequest)

Changing the view color when comparing values

I created a view to use as background and I would like to change its color when label text is greater or less than variable number. The script is okay but the color is not changing.
Thanks in advance.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var localName: UITextField!
#IBOutlet weak var localNameLabel: UILabel!
#IBOutlet weak var localTemp: UILabel!
#IBAction func getData(sender: AnyObject) {
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=" + localName.text! + "")
}
#IBOutlet weak var fundo: UIView!
override func viewDidLoad() {
super.viewDidLoad()
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=London")
// 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.
}
func getWeatherData(urlString: String){
let url = NSURL (string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
self.setLabels(data!)
})
}
task.resume()
}
func setLabels(weatherData: NSData) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(weatherData, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
print(json)
//localNameLabel.text = json[("name")] as? String
if let name = json[("name")] as? String {
localNameLabel.text = name
}
if let main = json[("main")] as? NSDictionary {
if let temp = main[("temp")] as? Double {
//convert kelvin to celsius
let ft = (temp - 273.15)
let myString = ft.description
localTemp.text = myString
self.changeColor()
}
}
} catch let error as NSError {
print(error)
}
var number : Float
func changeColor(){
number = 19.0
if(Float(localTemp.text!) < number){
fundo.backgroundColor = .blueColor()
}else{
fundo.backgroundColor = .orangeColor()
}
}
}
}
Edited to post the entire script
In your view controller you need to add UITextFieldDelegate which will allow you to access methods related to your text field. The top of your view controller should look like this:
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
You then need to set the delegate of your text field to self in viewDidLoad and add a target for when the text field changes:
override func viewDidLoad() {
super.viewDidLoad()
localTemp.delegate = self //set delegate to this vc
localTemp.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
}
You can then implement this method which will run on every key press and you need to call your changeColor() method as above:
func textFieldDidChange(textField: UITextField) {
self.changeColor()
}

I want to create a function which can return the responseJson and use it to create some objects in init

import UIKit
import Alamofire
class ViewController: UIViewController {
#IBOutlet weak var uiNameSearch: UITextField!
#IBOutlet weak var uiGivenName: UITextField!
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.
}
#IBAction func searchForName(sender: UIButton) {
let header = ["Accept" : "application/json"]
let name = uiNameSearch.text!
let given = uiGivenName.text!
Alamofire.request(.GET, "https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/Patient?family=\(name)&given=\(given)", headers: header).responseJSON { response in
let patient = response.result.value!
if let entry = patient.objectForKey("entry") {
if let link = entry[0].objectForKey("link") {
if let url = link[0].objectForKey("url") {
let fullNameArr = url.componentsSeparatedByString("/")
print(fullNameArr[fullNameArr.count-1])
}
//print(link)
}
}
}
}
}
My Patient class is:
class Patient {
var name: String
var given: String
var id: String
init(name: String, given: String, id: String) {
}
}
I want to use the response in init to create objects, how can I do this?

Swift use of unresolved identifier 'webView'

I want to add a webview to my application, but i keep getting this freaking error for webview. Please help!
import UIKit
class Browse: UIViewController {
#IBOutlet weak var browseweb: UIWebView!
let browseurl = "http://google.com"
override func viewDidLoad() {
super.viewDidLoad()
let requestURL = NSURL(string:browseurl)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
// 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.
}
}
Your variable is called browseweb, but you call webView.loadRequest(request). You need to use the same name...