Injecting a New Stylesheet into a Website via Uiwebview Using iOS8 Swift Xcode 6

Load UIWebView with a CSS local file

This stackoverflow question appears to have one or two answers that may help you.

You need to load the local CSS (using a method not unlike @Shrey uses, but looking for your CSS file), and somehow inject it into the page, and the only way appears to be to use:

[webview stringByEvaluatingJavaScriptFromString:someJavascriptToInjectCSS];

and some clever Javascript to modify the page to add the CSS in.

Hope this helps point you in the right direction. I have used this method to inject stuff into pages, so it does work, but I don't know Javascript well enough to write the code to inject your CSS into the page.

Swift & UIWebView element to hide

I finally found another way, I load my html in a variable and, i replace some div with a "display:none" exemple :

 let urlDeBase = "http://mywebsite/section/"    
if let url = NSURL (string: urlDeBase){
var error: NSError?
let myHTML = String(contentsOfURL: url, encoding: NSUTF8StringEncoding, error: &error)

and I replace what i want

 let mystring = myHTML?.stringByReplacingOccurrencesOfString("<h3 class=\"myclass\">Menu</h3>", withString: "<h3 class=\"myclass\" style=\"display:none\">Menu</h3>")
webvieww.loadHTMLString(mystring, baseURL: nil)

Best way to download an external webpage, modify it with js and css, then load it in the view

I'm not sure how complicated your use of the UIWebView is but, the quickest implementation I can think of (aside from the evaluateJS route you've already done):

Create a property to decide if a request has been hijacked yet (by you).

var hijacked = false

provide the UIWebViewDelegate protocol method.

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//if true it must be our version, reset hijack and allow to load
if hijacked {
hijacked = false
return true
}
//original request. Don't let it load, instead trigger manual loader.
else {
hijacked = true
manuallyLoadPage(request)
return false
}
}

Then you just need a method to fetch the page, get the text, manipulate the text, and then load the page with your version.

func manuallyLoadPage(request: NSURLRequest) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(data, response, error) in
var html = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
html = html.stringByReplacingOccurrencesOfString("</head>", withString: "<script>alert(\"I added this\")</script></head>", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
self.webView.loadHTMLString(html, baseURL: response.URL!)
}
task.resume()
}

This is just a quick and dirty approach, you may want to do a more thorough job of tracking which requests are hijacked requests etc... This one just assumes an even odd kind of approach. You could obviously manipulate the html however you want, I just added the JS alert as a proof of concept.

Using custom font in a UIWebView

After some Try and Error I have found a reliable way to load custom Fonts with a local CSS.

1. Add your Font to the App...make sure that the file is targeted
properly to the Application

Sample Image
Sample Image

2. Then add your Font to yourApp-Info.plist

Sample Image

3. Run NSLog(@"Available fonts: %@", [UIFont familyNames]); To check which name the font/fontfamily has for the System...

Sample Image

4. Copy that name and use them in your CSS...@font-face is not needed

body {
font-family:"Liberation Serif";
}


Related Topics



Leave a reply



Submit