So I've got a button which I'm trying to use as a means for the user to navigate to a specific URL (which it does), however the button is opening the url in safari, and i want it to be presented in my web view.
Here is my view controller.swift file:
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var myProgressView: UIProgressView!
#IBOutlet weak var Home_button: UIButton!
var theBool: Bool = false
#IBOutlet weak var refresh_button: UIButton!
#IBAction func go_home(_ sender: UIButton) {
UIApplication.shared.openURL(NSURL(string:"my url")! as URL)
}
var myTimer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
myTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(webViewDidStartLoad(_:)), userInfo: nil, repeats: true)
let websiteurl = URL(string: "another url")
let websiteurlrequest = URLRequest(url: websiteurl!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
webView.loadRequest(websiteurlrequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
//if let myProgressView.progress is not nil{
myProgressView?.progress += 0.045
//}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
let loadinglabel = self.view.viewWithTag(100)
loadinglabel?.removeFromSuperview()
myProgressView?.progress = 100
myProgressView?.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func go() {
myProgressView?.progress += 0.005
}
}
How do I open the url in the web view on the button press, rather than in safari?
Thanks in advance!
Try this. Should work in Swift 3
#IBAction func go_home(_ sender: UIButton) {
yourWebView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")!))
}
Change your button action method like this:
#IBAction func go_home(_ sender: UIButton) {
webView.loadRequest(websiteurlrequest)
}
and remove webView.loadRequest(websiteurlrequest) method from viewDidLoad. Then your webView load content when click on button.
Add the following code to the button action area
self.openWebURL(url: webUrl)
Function to load URL
func openWebURL(url : URL)
{
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
}
SFSafariViewControllerDelegate should be added to class header like thise
class YourClassName: UIViewController, SFSafariViewControllerDelegate
Hope this helps...
Related
before posting this I was looking on the internet how to implement from cocoa pods MBCircularProgressBarView in iOS Swift WKWebKit to track progress of loading website.
I have tried with combination of some other progress bar codes but it didn't work.
I have tried to implement self.progressView.value = 0 in viewDidLoad
and
UIView.animate(withDuration: 1.0){
self.progressView.value = 100
}
in
didFinish navigation
which shows circular bar and animate but doesn't show proper loading progress.
Any idea how to make this work.
Make a class class WebpageViewController: UIViewController, UIWebViewDelegate containing:
#IBOutlet weak var activitySpinner: UIActivityIndicatorView!
#IBOutlet weak var activityLabel: UILabel!
override func viewDidAppear(_ animated: Bool)
{
activitySpinner.tintColor = R.color.YumaRed
activitySpinner.hidesWhenStopped = true
activityLabel.text = "Loading..."
webView.loadRequest(URLRequest(url: URL(string: "http://...")!))
}
func webViewDidStartLoad(_ webView: UIWebView)
{
activitySpinner.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
activitySpinner.stopAnimating()
activityLabel.isHidden = true
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
activitySpinner.stopAnimating()
}
override func viewDidLoad()
{
super.viewDidLoad()
webView.delegate = self
}
Using swift3 with Xcode8
Below is mu view controller.swift
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
YahooWebview.delegate = self
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(YahooWebview: UIWebView) {
print("show indicator")
activity.startAnimating()
}
func webViewDidFinishLoad(YahooWebview: UIWebView) {
activity.stopAnimating()
}
}
From my log in Xcode, it print "show indicator" then stop and then show something like the picture below.
Seems like something wrong with code below
activity.startAnimating()
Can anyone help?
Try below code for load site in web view
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
YahooWebview.delegate = self
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
activity.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activity.stopAnimating()
}
}
I have loaded a webpage to xcode WebView. But only the upper portion of the page is loaded. I can not scroll down to the bottom of the page. Same fact for pdf. Only upper 2 pages can be scrolled. What can i do ?Here is my code.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://www.archetapp.com")
webView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//for pdf
import UIKit
class ViewController: UIViewController {
#IBOutlet var webViews: UIWebView!
var path = ""
override func viewDidLoad() {
super.viewDidLoad()
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
self.webViews.loadRequest(NSURLRequest(URL: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Try this!
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView
override func viewDidLoad() {
super.viewDidLoad()
//load initial
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
var req = NSURLRequest(URL : url)
webView.delegate = self // <---
webView.loadRequest(req)
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("webViewDidStartLoad")
}
func webViewDidFinishLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
println("webViewDidFinishLoad")
}
}
I had the same issue just today and at least in my case it was caused by having the "Scale Pages to Fit" option checked in the WebView properties. I'm assuming Carlos' reply regarding the zoom scale fixes it anyways but I didn't need the option enabled in the first place so that was my easy fix.
I Have a UIWebView strictly for viewing PDF's, and I don't want any link detection in the view, Like this:
So I added the following code to negate the detection of links and the presentation of that view:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
However it still picks up links, and presents that modal view. How do I correctly implement code to stop this?
Here is my updated code:
class PdfViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var myWebView: UIWebView!
var contentUrlPassedOn: String!
var allowLoad = true
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
let url: NSURL! = NSURL(string: contentUrlPassedOn)
myWebView.loadRequest(NSURLRequest(URL: url))
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return allowLoad
}
func webViewDidStartLoad(webView: UIWebView) {
var hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
self!.allowLoad = false
}
}
}
If your webview was added by the storyboard, You can disable this in the attributes inspector.
[1
It's an objective-c code, but u can do the same in Swift. It will fix the interaction with links. The con is that they will be still detected.
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
return NO;
}
return YES;
}
Finely Solve the problem for you,
Working Demo
Cannot disable long press link menu, Which is Bug in iOS, That's solve in iOS 8.2.
According to bug, when press long on link then its open Action Sheet, Now in iOS 8.2 its solve as below.
Load PDF using WKWebView as below.
import UIKit
import WebKit
class ViewController: UIViewController , WKUIDelegate {
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
var url = NSURL(string:"http://www.nursing.umich.edu/studentresources/documentation/CreatingBookmarksPDFdocsV7.pdf")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
}
Now you Click on your long Press link then its doesn't open ActionSheet PopUp.
Enjoy..!!
Output :
I have been trying to make a web browser in Swift and it has not been going well.
I was wondering why I got errors whenever I typed webView.delegate = self.
import UIKit
class MapViewController: UIViewController {
#IBOutlet weak var searchBar: UISearchBar!
#IBAction func back(sender: AnyObject) {
webView.goBack()
}
#IBOutlet weak var menuButton:UIBarButtonItem!
#IBOutlet weak var webView: UIWebView!
func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
searchBar.resignFirstResponder()
var text = searchBar.text
var url = NSURL(string: text) //type "http://www.apple.com"
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
override func viewDidLoad() {
super.viewDidLoad()
//problem here
webView.delegate = self;
webView.keyboardDisplayRequiresUserAction = true
//problem down here
webView.frame = self.view.frame;
let url = NSBundle.mainBundle().URLForResource("index", withExtension:"html")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
// var url = NSURL(string:"http://www.google.com")
// var req = NSURLRequest(URL:url!)
// self.webView!.loadRequest(req)
self.searchBar.delegate = self
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
You are assigning self as the delegate, but your class does not implement the UIWebViewDelegate protocol.
class MapViewController: UIViewController, UIWebViewDelegate {