"Expected parameter name followed by ':'" 's error happen many times.
I wanna delete cache from my UIWebView system.
I wrote ViewController like
import UIKit
class ViewController: UIViewController,UIWebViewDelegate {
#IBOutlet weak var webBrowser: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string:"http://127.0.0.1:8000/admin/accounts/")
let request = NSURLRequest(url:url! as URL)
self.webBrowser.delegate = self
self.webBrowser.loadRequest(request as URLRequest)
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
}
func webView(_webBrowser,;: UIWebView,
shouldStartLoadWith, request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But in this part
func webView(_webBrowser,;: UIWebView,
shouldStartLoadWith, request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool
many errors happen like
Xcode suggested me how to fix these error, for example "Fix it insert "_:" ",but if I ordered these the way to fix these errors, other errors happened.So,I cannot fix them.
How can I fix them?I think I made a mistake in a point of syntax, but I do not know where it is.
There is two mistakes in your code:
You didn't put the { and } for the method and the method expected to return a bool
And most important issue is the function signature you've written is entirely wrong
So the correct way to use that function is like:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
return true
}
cause of syntax error.update your delegate method to like below
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
return true
}
Related
As i implemented the code in which it perfectly work on Browser that is open the safari in ios and put myapp:// on address bar it open my app but i want when click on link which is inside UIWebView
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let setURL=URL(string:"https://myapp.com/")
let setURLRequest=URLRequest(url:setURL!)
webView.loadRequest(setURLRequest)
}
this is my code for loading website into webview and added URL TYPE >item0->myapp
item0->URL Scheme->myapp
when i add to link eg < a href="myapp://"></a> it does not launch the another view getting errors
Appdelegate
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// UIApplication.shared.open(request.url!, options: [:], //completionHandler: nil)
let myVC: MyAnotherController = MyAnotherController()
self.present(myVC, animated: true) { }
return false
}
return true
}
getting this error Value of type 'AppDelegate' has no member 'present'"
From inside if block, you need to return false, and it should work.
By returning false the delegate method tells that the webView should not start loading the url.
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// do your work here
UIApplication.shared.open(request.url!, options: [:], completionHandler: nil)
//Add the below line of code to the existing code
return false
}
return true
}
You can use UIWebView delegate method optional public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
Example
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// do your work here
UIApplication.shared.open(request.url, options: [:], completionHandler: nil)
}
return true
}
But you have to mention all the desire schemes in your app plist too..
Open other apps using url schemes
if let url = URL.init(string: "myapp://") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
print("scheme is missing in info.plist or invalid scheme")
}
} else {
print("invalid url")
}
Use this code (It will work if the URL's last character contains "#"). "#" is added because if we navigate back to the last screen, it does not open.
link(www.google.com) in web view.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool {
// request.url?.absoluteString = "www.google.com/#"
let last = request.url?.absoluteString.last
if last == "#" {
//do stuff here
}
return true
}
I made an iOS app with Xcode and Swift.
I want to open specific hyperlinks in Safari browser, there others in the WebView itself.
To reach this, I'll have to check when an hyperlinks gets clicked.
This is the code I have so far:
//
// PushViewController.swift
// App
//
//
import UIKit
class PushViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var openpushmessage: UIWebView!
var weburl:String = "http://www.example.com"
override func viewDidLoad() {
super.viewDidLoad()
let url: NSURL = NSURL(string: weburl)!
let requestURL: NSURLRequest = NSURLRequest(URL: url)
openpushmessage.loadRequest(requestURL)
}
override func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked
{
print("You clicked a hypelink!")
}
return true
}
}
The code in viewDidLoad() opens the loads the main URL (http://www.example.com) from my server. That works fine.
override fun webView[…] should detect if a hyperlink is clicked and then print("You clicked a hypelink!").
But unfortunately I'm getting the following errors:
What am I doing wrong? How to get rid of these errors?
please try
for swift 3.0
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
if navigationType == .linkClicked
{
}
return true;
}
swift 2.2
internal func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
if navigationType == .LinkClicked
{
}
return true;
}
This is the working solution (Xcode 7.3.1 and Swift 2.2):
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.URL?.query?.rangeOfString("target=external") != nil {
if let url = request.URL {
UIApplication.sharedApplication().openURL(url)
return false
}
}
return true
}
Many thanks to danhhgl from freelancer.com!
I think you can solve with this code:
function reportBackToObjectiveC(string)
{
var iframe = document.createElement("iframe");
iframe.setAttribute("src", "callback://" + string);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
var links = document.getElementsByTagName("a");
for (var i=0; i<links.length; i++) {
links[i].addEventListener("click", function() {
reportBackToObjectiveC("link-clicked");
}, true);
}
in ios code:
if ([[[request URL] scheme] isEqualToString:#"callback"]) {
[self setNavigationLeavingCurrentPage:YES];
return NO;
}
More details you can check from here:
I'm trying to figure out how to shouldOverrideUrlLoading (Android) in Swift for IOS. I want to capture the URL from a webview and not load that URL. Is this possible with swift? How would it be implemented?
if you are using webview then use web view delegate method shouldStartLoadWithRequest to return false for not loading the URL in webview. For eg.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.absoluteString == "https://www.google.com" {
return false
}
return true
}
Check out
func webView(UIWebView, shouldStartLoadWith: URLRequest, navigationType: UIWebViewNavigationType)
in UIWebViewDelegate (UIWebViewDelegate Documentation)
Set your view controller as the web view's delegate and return false in the method above to stop a url from loading in the web view.
Example:
class MyWebViewController: UIViewController, UIWebViewDelegate {
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.absoluteString.contains("dontLoadMe") {
return false
}
return true
}
}
I would like to access to a Sharepoint site, but the UIWebView is blocked when Office365 try redirect me to my auth organization page. On desktop, I see a prompt with 2 text fields, but, nothing here! Here is my code, and a screenshot:
class ViewController: UIViewController, NSURLConnectionDelegate {
#IBOutlet var webView: UIWebView!
#IBOutlet var refresh: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if let theURL = URL(string: "https://expertime365.sharepoint.com/sites/retail-portal-v8/Pages/default.aspx") {
let request = URLRequest(url: theURL)
webView.loadRequest(request)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is a screenshot:
Have did check url which are load in your webView ?
if you not then check following link and compare with that url which are loaded in Desktop browser.
You can check with below code :
func webView(webview: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let currentURL = request.URL.absoluteString
print("\(currentURL)")
}
I'm trying to learn IOS programming, really first steps, and can't find Swift Examples for my issue with a complete class code.
The Web page is being loaded, but I never get a response.
It's obviously something with "extension" part and I probably don't need delegate
import UIKit
class VkLoginViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
var delegate:UIWebViewDelegate!
override func viewDidLoad() {
super.viewDidLoad()
let link = String.localizedStringWithFormat("http://api.vk.com/oauth/authorize?client_id=%#&scope=email,photos,offline&redirect_uri=http://api.vk.com/blank.html&display=touch&response_type=token", Setup.VK_AUTH_SCHEME)
NSLog(link)
delegate = self
UIWebView.loadRequest(webView)(NSURLRequest(URL: NSURL(string: link)!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension VkLoginViewController : UIWebViewDelegate{
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let str = request.URL?.absoluteString
NSLog(str!)
return true
}
}
I never get to second NSLog's message
Modify your code to
class VkLoginViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let link = String.localizedStringWithFormat("http://api.vk.com/oauth/authorize?client_id=%#&scope=email,photos,offline&redirect_uri=http://api.vk.com/blank.html&display=touch&response_type=token", Setup.VK_AUTH_SCHEME)
NSLog(link)
webView.delegate = self
webView.loadRequest(webView)(NSURLRequest(URL: NSURL(string: link)!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension VkLoginViewController : UIWebViewDelegate{
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let str = request.URL?.absoluteString
NSLog(str!)
return true
}
}
Hope this help