Why UIWebView does not open my link in Safari? - ios

I wrote this code:
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
and it works for all normal URLs. But when I click on the image/gif with the next code:
<a href="https://cdn-images-1.medium.com/max/800/1*IR7oti7mEsYunzp_HhcNig.gif"
target="_blank" class="c"
style="background-image: url('//cdn.iframe.ly/pj?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1*IR7oti7mEsYunzp_HhcNig.gif&width=1280&
key=9b0e65e32a5412cf736c430972d914c33f46250b&cache=temp');">
1*IR7oti7mEsYunzp_HhcNig.gif </a>
it opens this gif/URL in my application, inside of UIWebView.
How can I force that it will open that gif/URL in Safari, too?

Something must be broken somewhere else in your code.
Working fine like this:
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.webView?.loadHTMLString("<a href='https://cdn-images-1.medium.com/max/800/1*IR7oti7mEsYunzp_HhcNig.gif' target='_blank' class='c' style='background-image: url('//cdn.iframe.ly/pj?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1*IR7oti7mEsYunzp_HhcNig.gif&width=1280& key=9b0e65e32a5412cf736c430972d914c33f46250b&cache=temp');'> 1*IR7oti7mEsYunzp_HhcNig.gif </a>", baseURL: NSURL(string: "testurl.com"))
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
return true
}
}

Related

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:

Meme Type Error on UiWebView

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.

Display Auth Prompt in UIWebView

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)")
}

open links in safari not uiwebview

i try to open links from my local html but i don't know what is wrong in my code. I added UIWebViewDelegate to the class and the delegate to the View.
class TableWebViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var infoWebView: UIWebView!
var htmlName:String!
override func viewDidLoad() {
super.viewDidLoad()
self.infoWebView.delegate = self
self.loadHtmltoWebview(htmlName)
self.infoWebView.backgroundColor = UIColor.whiteColor()
self.infoWebView.opaque = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadHtmltoWebview(name: String) {
let htmlFile = NSBundle.mainBundle().pathForResource(name, ofType: "html")
let url = NSURL(string: htmlFile!)
let request = NSURLRequest(URL: url!)
infoWebView.loadRequest(request)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
//UIApplication.sharedApplication().openURL(request.URL!)
print(request.URL)
return false
}
return true
}
can anybody help me? Thanks!
edit: print(request.URL) is shown in the output, so he jumps into the function, but safari don't open
Make sure your UIWebViews delegate property in IB is connected to your ViewController. Otherwise the method you implemented will never be called. Go to the outlets pane on IB for the WebView and connect the delegate outlet with your ViewController that implements the UIWebViewDelegate protocol.

iOS WebView - Open link in SFSafariViewController

So I am trying to detect a link click within a webView and then present this url in the new SFSafariViewController however am having some issues.
Anyone got any ideas?
This is what I am currently doing:
import UIKit
import SafariServices
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let localfilePath = NSBundle.mainBundle().URLForResource("index", withExtension: "html");
let myRequest = NSURLRequest(URL: localfilePath!);
webView.loadRequest(myRequest);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadInView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) {
if (navigationType == UIWebViewNavigationType.LinkClicked){
let url = request.URL;
let sfViewController = SFSafariViewController(URL:url!, entersReaderIfAvailable: true)
self.presentViewController(sfViewController, animated: true, completion: { () -> Void in
print("May the force be with you!")
})
}
}
}

Resources