iOS WebView - Open link in SFSafariViewController - ios

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

Related

Delete cache of app

I want to delete the cache of my app.
I want to show a web page by using UIWebView.
MY current code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webBrowser: UIWebView!
webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
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.loadRequest(request as URLRequest)
// 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.
}
}
When I added this code
webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
to ViewController,so "Expected declaration" error happen.
What is wrong in my code?
Should I connect my Storyboard and this code in some point?
How can i fix this?
To delete cache try below code
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
Else you can also change the cache policy of the NSURLRequest
let url = URL(string: "http://127.0.0.1:8000/admin/accounts/")
let request = URLRequest(url: url!,
cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
self.webBrowser.loadRequest(day_url_request)
it's Objective C code.Update your code like below
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)
// Do any additional setup after loading the view, typically from a nib.
}
func webView(_ webView: UIWebView,
shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool
// do your stuffs
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You are using Objective-C code shouldStartLoadWithRequest:
Here is the swift version code to load UIWebView
class ViewController: UIViewController,UIWebViewDelegate {
override func viewDidLoad() {
var webBrowser: UIWebView!
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webBrowser = UIWebView(frame: UIScreen.main.bounds)
webBrowser.delegate = self
view.addSubview(webBrowser)
if let url = URL(string: "http://127.0.0.1:8000/admin/accounts/") {
let request = URLRequest(url: url)
webBrowser.loadRequest(request)
}
}
}

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

iOS Swift : can't Retrieve current url loaded in UIwebView

i am working in webview application iOS swift :
problem i am facing is i want to get each url which webview displays :
upon urls i have to perform some if else checks . but i could not get the urls which webview loads.
webview is displaying fine results and loading urls upon clicking . i want to fetch all urls which webview navigates on..
import Foundation
import UIKit
class seconVC: UIViewController {
var toPass:String!
var touser:String!
var toPassing:String!
#IBOutlet weak var webV: UIWebView!
#IBOutlet weak var L_back: UIBarButtonItem!
#IBOutlet weak var R_frwrd: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var domurl = toPass;
var username = touser;
var passwing = toPassing;
var sendurl = stringA+username+"/"+passwing;
//exchange login with auth/username/password
println(sendurl);
let url = NSURL (string: sendurl);
let requestObj = NSURLRequest(URL: url!);
webV.userInteractionEnabled = true
webV.loadRequest(requestObj);
}
#IBAction func L_back(sender: UIBarButtonItem) {
if (webV.canGoBack){
webV.goBack()
}
}
#IBAction func R_frwrd(sender: UIBarButtonItem) {
if (webV.canGoForward){
webV.goForward()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
all i want is to get string of current url and navigation buttons for webview :please help ? i referred all the internet no solution is working in my case .
There are couple of UIWebViewDelegate such as shouldStartLoadWithRequest or webViewDidStartLoad or webViewDidFinishLoad or didFailLoadWithError that help you to accomplish your goal. If you want to perform operation after view did finished then implement this delegate method
Swift 3
if let currentURL = webView.request?.url?.absoluteString{
print(currentURL)
}
as it mentioned here.
option-1
let currentURL = webV.request.URL.absoluteString
option-2
get URL in delegate method
func webViewDidFinishLoad(webView: UIWebView){
print(WebView.request?.mainDocumentURL)
}
option-3
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL = webView.request().URL()
print("\(currentURL.description)")
}
or use like
func webViewDidFinishLoad(webView: UIWebView){
let currentURL = webView.request?.URL
}
option-4
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL: = webView.stringByEvaluatingJavaScriptFromString("window.location.href")!
print("\(currentURL)")
}
option-5
func webView(webview: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let currentURL = request.URL.absoluteString
print("\(currentURL)")
}
option-6
func webViewDidFinishLoad(WebVie: UIWebView) {
let currentURL = WebVie.request.mainDocumentURL
print("\(currentURL)")
}
First you have to define the delegate for WebView class. "WebView.delegate = self". Add this code beside one of the function above:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self }
and for exmaple:
func webViewDidFinishLoad(_ webView: UIWebView) {
if let currentURL = WebView.request?.url?.absoluteString{
print(currentURL)
}
}

Get - response from URL via UIWebView SWIFT

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

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.

Resources