Meme Type Error on UiWebView - ios

I have a iOS app which has a uiwebview where it opens a website, but any link clicked in the webview opens up in Safari. In my code bellow, it get a memetype error on the line "webView.load(request)". Can you give me some code so I can fix this? Thanks! (I need to use a UIWebView instead of a WKWebView)
class VideosViewController : UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView : UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://example.com") else { return }
let request = URLRequest(url: url)
webView.load(request)
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked {
guard let url = request.url else { return true }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
return false
}
return true
}
}

Try to call webView.loadRequest(request). This function has only one URLRequest parameter.

Related

UIWebview reload doesn't work

I use UIWebView to load the remote url, the page is blank because the network is unavailable. After network is available ,i reload the UIWebView, but the page is still blank.
How can i solve it?
Try this
func reloadRequest() {
if let url = URL(string: "https://stackoverflow.com") {
let request = URLRequest(url: url);
self.loadRequest(request);
}
}
Load web view with request again, like:
if let url = URL(string: "https://www.google.com") {
let request = URLRequest(url: url)
wvWebView.loadRequest(request)
}
Here is sample code.
#IBOutlet var wvWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
wvWebView.delegate = self
reloadWebview()
}
// call this function to reload web view.
func reloadWebview(){
if let url = URL(string: "https://www.google.com") {
let request = URLRequest(url: url)
wvWebView.loadRequest(request)
}
}
// Webview Delegates
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
print("\n\n**** webView:shouldStartLoadWithRequest: \(request.url) \(navigationType)")
return true
}
func webViewDidStartLoad(_ webView: UIWebView) {
print("webViewDidStartLoad")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("webViewDidFinishLoad")
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
print("error description - \(error.localizedDescription)")
}

How to open another View from links within UIWebview in Swift 3?

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
}

Xcode, Swift; Detect hyperlink click in UIWebView

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:

Swift: call function from another class to appear in same webview

I am trying to call loadAddressURL() into the Webview from callbackHandler() in the xWebAppJS class. I have no problem calling the callbackHandler() from the ViewController but the opposite does not hold true.
I am basically trying to reload the page or any other page in the Webview from another class.
Thanks in advance! Sorry if newbie question.
class ViewController: UIViewController, UIWebViewDelegate{
//... viewDidLoad()
//... viewDidAppear()
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let scheme = String(request.URL!)
NSLog(scheme)
//checks string if contains sequence
if scheme.containsString("ios:JS.") {
xWebAppJS.callbackHandler(scheme)
return false
}
NSLog("Link was not tapped")
return true
}
func loadAddressURL(URL: String) {
let requestURL = NSURL(string: URL)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}
This is the other class that contains the callbackHandler
class xWebAppJS: ViewController{
class func callbackHandler(oScheme: String) {
// I want to call the loadAddressURL() out of this function
}
}
Since xWebAppJS conforms to ViewController, you can just do xwebAppJS().loadAddressUrl(Url:String)
This will be the answer !
**inside the class**
let requestURL = NSURL(string: "string URL")
self.loadAddressUrl(requestURL)
**outside the class**
let xWebAppJS = xWebAppJS()
let requestURL = NSURL(string: "string URL")
xWebAppJS.loadAddressUrl(requestURL)
func loadAddressURL(requestURL: NSURL) {
let request = NSURLRequest(URL: requestURL)
webView.loadRequest(request)
}

Back button not working for my manually loaded UIWebView for external website

Technologies: Swift, iOS8.1.3, XCode 6, Maverick
I've added a "rewind" back button to my UIWebview via the main.storyboard and connected it here on my view with a backButton func:
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
var detailItem: NSURL?
var grabData = false
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
self.navigationController?.toolbarHidden = false;
}
#IBAction func backButton(sender: AnyObject) {
self.webView.goBack()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let url = self.detailItem {
webView.loadRequest(NSURLRequest(URL: url))
}
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request:
NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if grabData {
grabData = false
return true
} else {
grabData = true
manuallyLoadPage(request)
return false
}
}
func manuallyLoadPage(request: NSURLRequest) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(data, response, error) invar html = NSString(data: data, encoding: NSUTF8StringEncoding) as String
html = html.stringByReplacingOccurrencesOfString("</head>", withString: "<styles><link rel='stylesheet' type='text/css' href='link-to-styles.css' media='all'></styles></head>", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
self.webView.loadHTMLString(html, baseURL: response.URL!)
}
task.resume()
}
My back button doesn't do anything when clicked and I want it to show the previous webpage. I think it has something to do with how I am intercepting the session and manually loading the WebView. Maybe it doesn't know what the original base url is? Something else?

Resources