Swift lightweight wrapper for WKWebView to maintain persisted cookies through navigation, intercept requests and handle JavaScript messages
- iOS 11.0+
- Xcode 9.0+
Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks (10.9).
use_frameworks!
pod 'Oahu', :git => '[email protected]:elo7/oahu.git'
github "elo7/oahu" "master"
And add the path to the framework under “Input Files”, e.g.:
$(SRCROOT)/Carthage/Build/iOS/oahu.framework
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. To integrate using Apple's Swift package manager from xcode :
File -> Add Packages... -> Enter package URL : https://github.com/elo7/oahu, choose the latest release
import Oahu
let browser = Oahu(forView: view, allowsBackForwardNavigationGestures: true)
browser.loadRequest("http://www.elo7.com")
OahuDelegate responds to all WKNavigationDelegate methods
class ViewController: UIViewController, OahuDelegate
And then
browser.oahuDelegate = self
With Oahu you can intercept requests and perform some action. It is generally used to show some native content giving a determined request.
First you need to create a Evaluator:
let evaluator = OahuEvaluator(url: "tech") {
print("Tech request blocked")
}
The first parameter url is the term of the request that if appears, should stop the request.
The second parameter closure is the action that should be taken.
You should have an array of Evaluators and put all the Evaluators inside it.
var evalutors = [Evaluator]()
evalutors.append(evaluator)
Then you must create an Interceptor that uses all the evaluators inside the array.
let interceptor = Interceptor(evaluators: evalutors)
And finally, instantiate Oahu with a constructor that receives another argument.
browser = Oahu(forView: view, allowsBackForwardNavigationGestures: true, interceptor: interceptor)
Sometimes you need to talk with a JavaScript posted by your website and handle in a native way. To perform that task you should do this:
First you need to create a ScriptMessageHandler:
let messageHandler = ScriptMessageHandler(forEventName: "shippingCancel") { param in
print("Perform some native task when JavaScript trigger this event")
}
The first parameter forEventName is the event your JavaScript is triggering using WKWebKit convention.
window.webkit.messageHandlers.{NAME}.postMessage()
The second parameter handler is the action that should be taken.
You should have an array of ScriptMessageHandlers and put yours ScriptMessageHandler inside it.
var scriptMessages = [ScriptMessageHandlers]()
scriptMessages.append(messageHandler)
Then, you just need to assign Oahu's javaScriptHandlers with your array.
browser?.javaScriptHandlers = scriptMessages
To load HTML string content, you can use the same function of WKWebView
browser?.loadHTMLString("<html><body>Hi Oahu</body></html>")
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request
Oahu is released under the MIT license. See LICENSE for details.