How to Find and Highlight Text in WebView using Swift - swift

I'm working on my first app, and it involves searching for a specific string of text from a large amount of text on a website. I want to be able to search for a specific string and highlight all instances, and as the user types more, it will automatically update the highlighted occurrences. Similar to a find function in any browser.
I found this question on here where they reference using UIWebViewSearch.js to implement this, but I'm not sure how to add that file to my project, or how to use it.
I've added a search bar to my app so far, but haven't done anything with it. Want to use this function along with the search bar to search for the text.
Here is my ViewController code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
} // end if
func searchBarSearchButtonClicked() {
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(str)')"
self.newsWebView .stringByEvaluatingJavaScript(from: startSearch)
}
} // end of viewDidLoad
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // end of didReceiveMemoryWarning
} // end of class ViewController

As #jon-saw says: Welcome to StackOverflow :)
I'll just answer how you can add the JavaScript file to your project...that should get things started right :)
Add a File
You can add a file to Xcode in several ways, but here's one.
In Xcode, in your project, navigate in the left hand Project navigator to where you would like to add your file.
Right click and select "New File..."
Select "Empty" and click "Next"
Name your file UIWebViewSearch.js and click "Create"
You now have an empty file called UIWebViewSearch.js in your project in which you can paste in content.
Use a File
Now you have the file on your project, so now you just need to refer to it. To do so you use Bundle which can load resources in your project for you.
If you look at the answer you referred to, you can see these lines of ObjC code in - (NSInteger)highlightAllOccurencesOfString:(NSString*)str
NSString *path = [[NSBundle mainBundle] pathForResource:#"UIWebViewSearch" ofType:#"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
Translated to Swift that would look like this:
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
}
And that should give you a String containing all your JS code, ready to add to a WebView for instance.
Hope that gives you something to start with.
Update (after you added a comment to this post)
OK, so you say:
I added my ViewController code to better communicate where I'm at. Kind of confused on how to implement the second part of his code you're referring to. Also not sure how to call the search from the searchBar. Is there a method I need to call within 'searchBarSearchButtonClicked()' method? Just found that method on the apple developer documentation, don't know if it's the correct one, though.
Lets break it down into pieces, I'll start with your ViewController, as there are some problems in your viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
} // end if
func searchBarSearchButtonClicked() {
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(str)')"
self.newsWebView .stringByEvaluatingJavaScript(from: startSearch)
}
}
You've added your searchBarSearchButtonClicked inside viewDidLoad, but it should be declared as a function by itself (we'll get back to it later).
Furthermore, as I wrote in the comments below:
...One part that is run when the view is loaded and which loads the JavaScript from the bundle.
So lets fix your viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
//Use your jsString on the web view here.
newsWebView.stringByEvaluatingJavaScript(from: jsString)
}
}
As you can see, the searchBarSearchButtonClicked function has been removed and we now use out jsString on the newsWebView.
OK, next part:
Also not sure how to call the search from the searchBar. Is there a method I need to call within 'searchBarSearchButtonClicked()' method? Just found that method on the apple developer documentation, don't know if it's the correct one, though.
You have your searchBarSearchButtonClicked() method which you have found in the Apple Developer documentation, I'm guessing you're meaning this one
As you can see in the documentation (top bar), this function is from the UISearchBarDelete class.
Delegation is a design pattern used in many of Apples frameworks. It allows you to write some generic components and let "someone else" (the delegate) decide what to do with the information from the component (bad explanation I know).
Think of your UISearchBar for instance. If you were to implement a component that other developers could use as a search bar, how would you handle when the user searches for something? I'm thinking, how should your "customers" (the developers using your components) handle this? One way of doing this could be that the developers should subclass UISearchBar and override a "user did search method". You could also use NSNotifications and have the developer register for these notifications. Both of these methods are not pretty or clever.
Enter...the delegate.
UISearchBar has a UISearchBarDelegate and UISearchBarDelegate is just a protocol as you can see. Now the UISearchBar can ask its delegate for certain things, or inform the delegate about important events. So for instance, when the user taps the "Search" button UISearchBar can call delegate.searchBarSearchButtonClicked(self) which means "hey delegate, the user has clicked the search button on me...just thought you should know". And the delegate can then respond as it sees fit.
This means that any class can conform to UISearchBarDelegate and handle the various tasks as it suits in their current situation.
OK, long story, hope you get the gist of it and I think you should read a bit more on delegation as it is a pattern used all over in Apples frameworks :)
So, in your case, you have a UISearchBar, you should give that a delegate like so:
searchBar.delegate = self
And you need to tell the compiler that you intend to implement the UISearchBarDelegate protocol. The Swift convention is to do this in an extension after your ViewController, just to keep things separated:
extension ViewController: UISearchBarDelegate {
}
And then you must also implement the methods of UISearchBarDelegate that you are interested in listening to (note that some protocols have required methods, meaning that you MUST implement them but I don't think UISearchBar has (otherwise you'll find out when the app crashes the first time you run it :)).
So in your case, you mentioned searchBarSearchButtonPressed. As you can see it needs a UISearchBar as a parameter. So your function ends out looking like this:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let currentSearchText = searchBar.text else {
return
}
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(currentSearchText)')"
newsWebView.stringByEvaluatingJavaScript(from: startSearch)
}
So... you fetch the current text from your searchBar, add it as a parameter to your startSearch string and hand that startSearch string to your newsWebView.
The entire extensions ends out looking like this.
.... last part of your ViewController class
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // end of didReceiveMemoryWarning
} // end of class ViewController
extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let currentSearchText = searchBar.text else {
return
}
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(currentSearchText)')"
newsWebView.stringByEvaluatingJavaScript(from: startSearch)
}
}
And your viewDidLoad ends out looking like this (you must add yourself as a delegate to the searchBar)
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self //delegate set up
// Do any additional setup after loading the view, typically from a nib.
// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
//Use your jsString on the web view here.
newsWebView.stringByEvaluatingJavaScript(from: jsString)
}
}
Wow...that was a lot of writing...hope it helps you.

Related

iOS Swift: How to access selected text in WKWebView

I would like to be able to use a menu button to copy selected text from a web page in WKWebView to the pasteboard. I would like to get the text from the pasteboard into a text view in a second view controller. How do I access and copy the selected text in the WKWebView?
Swift 4
You can access the general pasteboard with the following line:
let generalPasteboard = UIPasteboard.general
In the view controller, you can add an observer to observe when something is copied to the pasteboard.
override func viewDidLoad() {
super.viewDidLoad()
// https://stackoverflow.com/questions/35711080/how-can-i-edit-the-text-copied-into-uipasteboard
NotificationCenter.default.addObserver(self, selector: #selector(pasteboardChanged(_:)), name: UIPasteboard.changedNotification, object: generalPasteboard)
}
override func viewDidDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(UIPasteboard.changedNotification)
super.viewDidDisappear(animated)
}
#objc
func pasteboardChanged(_ notification: Notification) {
print("Pasteboard has been changed")
if let data = generalPasteboard.data(forPasteboardType: kUTTypeHTML as String) {
let dataStr = String(data: data, encoding: .ascii)!
print("data str = \(dataStr)")
}
}
In the above pasteboardChanged function, I get the data as HTML in order to display the copied as formatted text in a second controller in a WKWebView. You must import MobileCoreServices in order to reference the UTI kUTTypeHTML. To see other UTI's, please see the following link: Apple Developer - UTI Text Types
import MobileCoreServices
In your original question, you mentioned you want to put the copied content into a second textview. If you want to keep the formatting, you will need to get the copied data as RTFD then convert it to an attributed string. Then set the textview to display the attributed string.
let rtfdStringType = "com.apple.flat-rtfd"
// Get the last copied data in the pasteboard as RTFD
if let data = pasteboard.data(forPasteboardType: rtfdStringType) {
do {
print("rtfd data str = \(String(data: data, encoding: .ascii) ?? "")")
// Convert rtfd data to attributedString
let attStr = try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtfd], documentAttributes: nil)
// Insert it into textview
print("attr str = \(attStr)")
copiedTextView.attributedText = attStr
}
catch {
print("Couldn't convert pasted rtfd")
}
}
Because I don't know your exact project or use case so you may need to alter the code a little but I hope I provided you with pieces you need for project. Please comment if there's anything I missed.

Appending Data To URL In Swift

This may be a rudimentary question, but I am trying to figure out how to "append" strings to a URL, and have them maintain, through View Controllers. More specifically;
I am building a basic File Browser app that is getting its data from a web service (in XML) form. Each time the user taps on a "folder" (which is being displayed as a list of folders in a Table View), another request is made via a NSURL session to get the contents of that folder.
My issue is that the URL string is only appending the name of the row the user tapped on, but I am unsure how to have it populate all rows the user tapped.
let urlString = "http://myurl/"
After the user taps the desired folder...
let urlString = "http://myurl/\(tappedRow)"
// This prints as http://myurl/firstfoldername/
After the user taps the next desired folder...
let urlString = "http://myurl/\(tappedRow)"
// This prints as http://myurl/secondfoldername/
// But I want http://myurl/firstfoldername/secondfoldername/
Since I am just segueing from the TableViewController to itself, and reloading the table, I assume this is working as its supposed to, but I seek to have the url string keep appending the tapped rows to the end, rather than forgetting each time. I was thinking of using NSUserDefaults to keep the last URL, but I realized this must be a common occurrence and perhaps there's a better way. Thanks!
override func viewDidLoad() {
super.viewDidLoad()
let baseUrlString = "http://myUrl/"
var currentUrl = baseUrlString
}
func cellTapped() {
currentUrl += tappedRow
}
You could use a Stack of Strings. I prepared this singleton class.
class StackFolder {
private var path = [String]()
static let sharedInstance = StackFolder()
private init() {}
static private let basePath = "http://myurl"
var baseURL = NSURL(string: basePath + "/")!
func push(folder: String) {
path.append(folder)
}
func pop() {
path.removeLast()
}
func toURL() -> NSURL {
let folders = path.reduce("/") { $0 + $1 + "/" }
let adddress = Stack.basePath + folders
return NSURL(string: adddress)!
}
}
You just need to retrieve the shared instance of Stack and:
invoke push when you want to add a folder to the path
invoke pop when you want to remove the last added folder.
Here it is an example:
StackFolder.sharedInstance.push("folder1")
StackFolder.sharedInstance.toURL().absoluteString // "http://myurl/folder1/"
StackFolder.sharedInstance.push("folder2")
StackFolder.sharedInstance.toURL().absoluteString // "http://myurl/folder1/folder2/"
StackFolder.sharedInstance.pop()
StackFolder.sharedInstance.toURL().absoluteString // "http://myurl/folder1/"
Since Stack is a Singleton you don't need to pass a reference to the same Stack object from one view controller to the other. Each time you write Stack.sharedInstance in your app you automatically get the reference to the only instance of Stack.
Hope this helps.
Declare urlString as instance variable outside any method
var urlString = "http://myurl"
Define a function addPathComponent() and call it every time a row is tapped passing the folder name as the parameter. Using the method stringByAppendingPathComponent avoids confusion with the slash path separators.
func addPathComponent(component : String)
{
urlString = urlString.stringByAppendingPathComponent(component)
// do something with the new urlString value
}

Injecting a new stylesheet into a website via uiwebview using iOS8 Swift XCode 6

I've seen a few options on here using just Objective-C, but I'm having trouble doing this with Swift iOS8 in XCode 6. I'm using the uiwebview to load a website. For sake of example, let's say its google.com.
#IBOutlet var website: UIWebView!
var url = "http://www.google.com"
func loadUrl() {
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
website.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
website.delegate = self
loadUrl()
}
func webViewDidFinishLoad(website: UIWebView) {
var jsscript = "some script here"
website.stringByEvaluatingJavaScriptFromString(jsscript)
}
Using Swift, how would I inject a whole new stylesheet into the website so I can overwrite many of the styles?
Also, I'd like the new stylesheet to exist in one of my app's directories. Where is the best directory for that to be? Under "Supporting files?" And, how would I call that path in my code?
It would also be nice if the website wouldn't load until the styles had been applied.
So, I think I found at least one way to accomplish appending styles to the webpage being loaded using Swift:
var loadStyles = "var script =
document.createElement('link');
script.type = 'text/css';
script.rel = 'stylesheet';
script.href = 'http://fake-url/styles.css';
document.getElementsByTagName('body')[0].appendChild(script);"
website.stringByEvaluatingJavaScriptFromString(loadStyles)
Using the now preferred WKWebView you can put the external css file, say tweaks.css in the root of your project or in a sub-folder, then you can inject it into your page like this:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
guard let path = Bundle.main.path(forResource: "tweaks", ofType: "css") else { return }
let css = try! String(contentsOfFile: path).replacingOccurrences(of: "\\n", with: "", options: .regularExpression)
let js = "var style = document.createElement('style'); style.innerHTML = '\(css)'; document.head.appendChild(style);"
webView.evaluateJavaScript(js)
}
To make the file available:
Click your project
Click your target
Select Build Phases
Expand Copy Bundle Resources
Click '+' and select your file.
Also make sure that your controller implements WKNavigationDelegate:
class ViewController: UIViewController, WKNavigationDelegate

Swift-passing global variables to the #IBAction function?

I am trying to allow the user to select a .png file to open by clicking file on the menu bar of the application, and then open a Microsoft Word file in the same way.
The problem is it appears that #IBAction func SelectFileToOpen(sender: NSMenuItem) {} cannot access global variables, or set them, and seems completely independent from the rest of the code
here is my code designed to demonstrate how the method can't read global variables:
//
// AppDelegate.swift
// Swift class based
//
// Created by ethan sanford on 2015-01-16.
// Copyright (c) 2015 ethan D sanford. All rights reserved.
//
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
var myURL=NSURL(fileURLWithPath: "")
#IBAction func btnConcat(sender: NSButton) {
myURL = NSURL(fileURLWithPath: "///Users/ethansanford/Desktop/BigWriting.png")
var say_something = "set URL button clicked"
print(say_something);
print(myURL)
}
#IBAction func SelectFileToOpen(sender: NSMenuItem) {
var say_something = "Menu bar, file-open clicked:"
print(say_something);
print(myURL);
}
#IBAction func communicate(sender: AnyObject) {
var say_something = "communicate button clicked:"
print(say_something);
print(myURL);
}
}
Here is the NSlog produced from this code. Notice that the URL button and the commincate button methods can share the myURL variable, but the file open button seems unable to:
URL button clickedOptional(file://///Users/ethansanford/Desktop/BigWriting.png)
communicate button clicked:Optional(file://///Users/ethansanford/Desktop/BigWriting.png)Menu bar
file-open clicked:nil
communicate button clicked:Optional(file://///Users/ethansanford/Desktop/BigWriting.png)
I need the myURL variable to be able to be used in all three methods. This is necessary for later when I need these methods to communicates so I can take the users selection and display it in an image well. Thanks for any help you can provide. I believe the problem is something specific to the file button in the menu bar.
Can anyone explain to me how to get around this problem?
In your code myURL is an instance variable that will be created within the app delegate. I wonder if you have oversimplified your code sample.
Having said that it should be accessible from the instance methods of the app delegate but having IBAction methods in the AppDelegate rather than in UI code seems like an odd choice (I've never tried it).

How send image to WhatsApp from my application?

In July 2013 WhatsApp opened their URL schemes for our apps. I have sent text to Whatsapp from my application, but now I would like send a image. How send a image to Whatsapp?
I'm not sure how do it.
Thank you.
Per their documentation, you need to use UIDocumentInteractionController. To selectively display only Whatsapp in the document controller (it is presented to the user, at which point they can select Whatsapp to share to), follow their instructions:
Alternatively, if you want to show only WhatsApp in the application list (instead of WhatsApp plus any other public/*-conforming apps) you can specify a file of one of aforementioned types saved with the extension that is exclusive to WhatsApp:
images - «.wai» which is of type net.whatsapp.image
videos - «.wam» which is of type net.whatsapp.movie
audio files - «.waa» which is of type net.whatsapp.audio
You need to save the image to disk, and then create a UIDocumentInteractionController with that file URL.
Here is some example code:
_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL];
_documentController.delegate = self;
_documentController.UTI = #"net.whatsapp.image";
[_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]
Here is a finally working solution for Swift. The method is triggered by a button. you can find some more explanations here
import UIKit
class ShareToWhatsappViewController: UIViewController, UIDocumentInteractionControllerDelegate {
var documentController: UIDocumentInteractionController!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func shareToWhatsapp(sender: UIButton) {
let image = UIImage(named: "my_image") // replace that with your UIImage
let filename = "myimage.wai"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as! NSString
let destinationPath = documentsPath.stringByAppendingString("/" + filename).stringByExpandingTildeInPath
UIImagePNGRepresentation(image).writeToFile(destinationPath, atomically: false)
let fileUrl = NSURL(fileURLWithPath: destinationPath)! as NSURL
documentController = UIDocumentInteractionController(URL: fileUrl)
documentController.delegate = self
documentController.UTI = "net.whatsapp.image"
documentController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: false)
}
}