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...
Related
I want to change the language of the page indicator, it should be german and not english. If you scroll inside of a pdf file it shows you "1 of 6" that's what i want to change.
Any ideas?
import UIKit
import WebKit
class PDFViewController: UIViewController {
var pdfName: String?
#IBOutlet weak var wkWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let pdfFilePath = Bundle.main.url(forResource: pdfName!, withExtension: "")
let urlRequest = URLRequest.init(url: pdfFilePath!)
wkWebView.load(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Xcode Project -> Info -> Localizations add German
was the answer!
Thanks #Retterdesdialogs
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
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)
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
I want to touch a button in ViewController and run a simple function in a custom class. No parameters needed. Code is basically:
class MyClass: UIView {
//init function is here
//other setup stuff
...
func SetFocus() {
//I want to set the camera focus to infinity.
var error: NSErrorPointer = nil
var pos: CFloat = 1.0
captureDevice!.lockForConfiguration(error)
captureDevice!.setFocusModeLockedWithLensPosition(pos, completionHandler: nil)
captureDevice!.unlockForConfiguration()
}
}
and in ViewController
import UIKit
class ViewController: UIViewController {
#IBOutlet var myClass: MyClass!
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 myButton(sender: UIButton) {
myClass.SetFocus()
}
}
I've tried everything I can think of (new at Swift) and the app either crashes or the function I'm calling doesn't run. Everything else in MyClass works OK.
Try to delete the #IBOutlet from before MyClass.
var myClass : MyClass!
You can also try this:
var myClass : MyClass! = MyClass()
From the looks of it, however, everything you are doing in setFocus() in MyClass, can easily be recreated in the ViewController.
Try this
#IBAction func myButton(sender: UIButton) {
var ClassViewController = self.storyboard.instantiateViewControllerWithIdentifier("StoryboardID") as! UIViewController
ClassViewController.MyFunction()
}