In order to load urls, I tried to connect .loadRequest with webview, but there is not such loadRequest. Is there anyone who knows why this is occurring to me?
func configurationView(){
if let webData: Web = self.detailItem {
if let webView = self.WebView {
let url = NSURL(string: webData.url)
let urlRequest = NSURLRequest(URL: url!)
(here I am trying to dp WebView.loadRequest(urlRequest), but I cannot!)
}
}
}
Related
I'm new to Swift. I want to create a simple browser that behaves similar to Safari.
My problem is I can't search just a single word instead of typing two words in searchBar using Google.
So what is wrong?
Here's my code:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
SearchBar.resignFirstResponder()
if let url = URL (string: "https://\(SearchBar.text!)") {
let request = URLRequest(url: url)
webView.load(request)
} else if let url = URL (string: "https://www.\(SearchBar.text!)") {
let request = URLRequest(url: url)
webView.load(request)
} else {
let textComponent = SearchBar.text!.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: "https://www.google.com/search?q=" + textComponent!)
let urlRequest = URLRequest(url: url!)
webView.load(urlRequest)
}
}
1 word
2 words
Link
I'm sorry for broken English.
I'm trying to replace the map on the carplay app of this sample project by a carplay webview.
Here the function that returns the root template, to be show on CarPlay :
The problem, is that the setRootTemplate function needs a CPTemplate, and I don't know how to create a template and put a uiview on that ?
func application(_ application: UIApplication, didConnectCarInterfaceController interfaceController: CPInterfaceController, to window: CPWindow) {
let webViewClass : AnyObject.Type = NSClassFromString("UIWebView")!
let webViewObject : NSObject.Type = webViewClass as! NSObject.Type
let webview: AnyObject = webViewObject.init()
let url = NSURL(string: "https://www.google.com")
let request = NSURLRequest(url: url! as URL)
webview.loadRequest(request as URLRequest)
let uiview = webview as! UIView
// tried that too... let myCPTemplateHere = webview as! CPTemplate
// Here
interfaceController.setRootTemplate(myCPTemplateHere, animated: true)
}
I tried with WKWebView too
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
Thanks a lot !
PS: This app is for my own usage, I will not send it to the app store.
Hi I open one url in my app
UIApplication.shared.open(webURL! as URL, options: [:])
I want when finished work in browser I get response , and get last URL of webpage
how to do that?
swift
Put below code in your viewDidLoad.
let webView = UIWebView()
webView.scalesPageToFit = true
webView.delegate = self
view.addSubview(webView)
webView.fillOnSupperView()
webView.delegate = self
let request = URLRequest(url:
URL(string:"https://www.google.com"))
webView.loadRequest(request) // load your url here
I'm new here, so please bear with me. I have a Master-Detail app, and in the DetailViewController.swift file, here's the configureView func where it calls a web view to open a web page.
It's complaining on the let request = URLRequest(url:url) line because url variable is defined as a string. I've tried everything but it won't work.
By the way, MasterViewController.MyVariables.urlString is an array of strings.
func configureView() {
// Update the user interface for the detail item.
if let detail: AnyObject = detailItem {
if let myWebview = webView {
let url = MasterViewController.MyVariables.urlString
let request = URLRequest(url: url)
myWebview.scalesPageToFit = true
myWebview.loadRequest(request)
}
}
}
You can either pass an URL object or create an URL object from the string that you´re passing. Anyhow you need to convert your string into an URL.
if let url = URL(string: MasterViewController.MyVariables.urlString) {
// use url in here
}
Update:
Use this for your example:
if let url = URL(string: MasterViewController.MyVariables.urlString) {
// use url in here
let request = URLRequest(url: url)
}
Update 2:
You have your Struct which is an array of strings. Then you need to do this to get the value you want:
struct MyVariables {
static var urlString: [String]? = ["something"]
}
if let str = MyVariables.urlString?.first, let url = URL(string: str) {
// use url in here
let request = URLRequest(url: url)
print(url)
}
Right now I´m using MyVariables.urlString?.first, in the future if you want another index then you need to get that instead.
This is what I did to make it work:
let stringRepresentation = MasterViewController.MyVariables.urlString?.joined(separator:"")
print ("urlString", MasterViewController.MyVariables.urlString)
print ("sR",stringRepresentation)
let url = NSURL(string: stringRepresentation as! String)
let request = URLRequest(url: url! as URL)
myWebview.scalesPageToFit = true
myWebview.loadRequest(request)
I am trying to learn iOS following a course and they ask to do the following:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//var string1 = "http://www.google.com"
//var string1 = "https://www.weather-forecast.com/locations/San-Antonio/forecasts/latest"
//var url = NSURL(string: string1)
var url = NSURL(string: "https://google.com")
print(url)
if url != nil {
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
var urlError = false
if error == nil {
var urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(urlContent)
} else {
urlError = true
}
if urlError == true {
self.showError()
}
})
task.resume()
} else {
showError()
}
}
the app doesn't show any web page content and when debugging I find that the object for the url says that it is "unable to read data"
I have tried with http and https. I have tried with different web sites.
I have tried the address in the safari of the simulator and it loads.
Can someone tell me why is this not working
Thanks in advance.
gariva
You're using wrong encoding. The webpage you're trying to fetch (http://www.google.com/) uses ISO-8859-1.
I was able to reproduce your issue. Fetch worked when I changed encoding. Try this:
var urlContent = NSString(data: data!, encoding: NSISOLatin1StringEncoding)
For display web page you should use UIWebView element. Something like this:
let url = NSURL(string: "https://google.com")
let webView = UIWebView(frame: self.view.frame)
self.view.addSubview(webView)
webView.loadRequest(NSURLRequest(URL: url!))