UIWebView third party website with custom stylesheet - swift

How can I add custom local stylesheet to UIWebView which is requested a third party website such as facebook.com.
import UIKit
class ViewController: UIViewController {
#IBOutlet var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let URL = NSURL(string: "www.any3rdpartywebsite.com" )
WebView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

webViewDidStartLoad and webViewDidFinishLoad not working

I am trying to check if the web view is loading and if its finish loading and when I try to search on it this is what I have gotten however it seems like it's not working is it because I have done something wrong or they change the code for it? I did set the delegate for the web view so I don't think the issue is with the delegate.
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView){
print("Webview started Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Webview did finish load")
}
You need to conform your class to UIWebViewDelegate and delegate managing of webView to your class:
class YourClass: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
//!!!!!!!
webView.delegate = self
//!!!!!!!
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView){
print("Webview started Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Webview did finish load")
}
}
You need to add "delegate" in your UIWebView
webView.delegate = self

How to put override func before viewDidLoad()?

Open ViewController.swift for editing, and before viewDidLoad() put the code below the question into the default code of ViewController.Swift?
How do I put these 4 lines of code below using the directions above into the default code for ViewController.Swift?
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
Default code ViewController.Swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I'm guessing you have a storyboard in which ViewController is defined. If so, you must not override loadView():
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
(from UIViewController class reference)
Instead edit the class of the view in the storyboard and set it to WKWebView. To get the webView reference, create it as an outlet:
#IBOutlet weak var webView: WKWebView!
And link it as a referencing outlet to the view in the storyboard.
(Or you could just put the WKWebView inside the default UIView; would be simpler. You could do this either programmatically in viewDidLoad() or via the storyboard.)
Or if you don't have a storyboard, then just copypaste the code into the editor inside the class but outside the existing functions?
import UIKit
class ViewController: UIViewController {
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
However, it matters not where the func goes in the class:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
}

Linking View Controllers through button

I'm a beginner at all of this...Having said that I've come across a point in my app where I've stalled and don't know what to do or fix next. So any answers would be appreciated!
So in my Home View Controller, I have four buttons with four different categories.
Each of these categories has its own question list, but they have a common "General Question" list. The general question list has its own view controller.
When you click on any of the four buttons, it brings you to the General Question view. At the bottom of this view, I have a "Next" button.
Goal: Configure the Next button to continue to one of the category's question list based on what is initially pressed in the Home View Controller.
I've connected the buttons via outlet and action in the View Controller.
However, the Next button will not connect when I control + drag into the View Controller. I'm not sure where I need to put the code for this...
I was thinking that the code for the Next button might need to have some kind of conditional statement, but since it won't connect I can't even get that far.
Help!
(This is what I have) Sample Code:
import UIKit
import AddressBookUI
import AddressBook
import Foundation
import CoreData
import CoreGraphics
import EventKit
import EventKitUI
import CoreFoundation
class ViewController: UIViewController {
#IBOutlet var ColorButton: UIButton!
#IBOutlet var StyleButton: UIButton!
#IBOutlet var CutButton: UIButton!
#IBOutlet var MakeupButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// 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.
}
var eventstore: EKEventStore!
var event: EKEvent!
weak var editViewDelegate: EKEventEditViewDelegate!
#IBAction func ColorButtonPressed(sender: UIButton) {
}
#IBAction func StyleButtonPressed(sender: UIButton) {
}
#IBAction func HaircutButtonPressed(sender: UIButton) {
}
#IBAction func MakeupButtonPressed(sender: UIButton) {
}
}
Here is a suggested approach as shown in the code below for 2 controllers (instead of 4) for brevity. Use appropriate named segues to each of the "next processing" controllers from the common processing controller and set up a chain. Here is a link to the project file: Project file
import UIKit
class ViewController: UIViewController {
var nextVcId = 0 // defines the button that is pressed
#IBAction func unwindFromOtherControllers(segue: UIStoryboardSegue) {
// In case you want to get back to the main VC
}
#IBAction func btn2Action(sender: UIButton) {
nextVcId = 0
self.performSegueWithIdentifier("commonSegue", sender: sender)
}
#IBAction func btn1Action(sender: UIButton) {
nextVcId = 1
self.performSegueWithIdentifier("commonSegue", sender: sender)
}
override func viewDidLoad() {
super.viewDidLoad()
// 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.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc = segue.destinationViewController as! CommonViewController
vc.nextControllerId = nextVcId
}
}
import UIKit
class CommonViewController: UIViewController {
var nextControllerId = 0
#IBOutlet weak var StatusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.StatusLabel.text = "Common"
commonProcessing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func commonProcessing() {
// do your common processing
if nextControllerId == 0 {
performSegueWithIdentifier("next1Segue", sender: self)
} else {
performSegueWithIdentifier("next2Segue", sender: self)
}
}
}
import UIKit
class Next1ViewController: UIViewController {
#IBOutlet weak var statusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.statusLabel.text = "Next1"
next1Processing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func next1Processing() {
println("Next 1 Processing")
}
}
import UIKit
class Next2ViewController: UIViewController {
#IBOutlet weak var statusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
statusLabel.text = "Next 2"
next2Processing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func next2Processing() {
println("Next 2 Processing")
}
}
processing

SWIFT Can't Open Second URL in UIWebView with Second Button

Boy am I a noob. Can't get a button to open a second URL in SWIFT. App opens, loads URL in WebView upon open. Now I want a button to open another url in the WebView when clicked. (Back button, go forward button and reload no prob)... Please help, thank you
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
var URLPath = "http://bandersnatchorlando.com/x19559"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL:
requestURL)
webView.loadRequest(request)
}
#IBOutlet weak var reLoad: UIButton!
}
Assuming you already have the other button in your Storyboard and it is linked to an #IBAction then inside the action:
let url = NSURL(string:"http://myotherurl.com")
let request = NSURLRequest(URL:url)
webView.loadRequest(request)

UIWebView centring page and allow navigation

I managed to have a website loaded in my UIWebView but I can't manage to center it.
I also would like to be able to "navigate" into the website. What am I doing wrong ?
here's the code :
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var WebView: UIWebView!
var URLPath = "http://google.fr"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
WebView.scalesPageToFit = true
WebView.keyboardDisplayRequiresUserAction = true
loadAdressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAdressURL () {
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL)
WebView.loadRequest(request)
}
}
And here's the IOS Simulator :
Rob.
Add this
override func viewDidAppear(animated: Bool) {
WebView.frame = UIScreen.mainScreen().bounds
WebView.center = self.view.center
}