Platform | Minimum Swift Version | Installation | Status |
---|---|---|---|
iOS 13.0+ / macOS 11.0+ | 5.0 | Swift Package Manager, Manual | Fully Tested |
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding MKWebViewBridge as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/vincent-k-sm/MKWebviewBridge", .upToNextMajor(from: "1.0.0"))
]
If you prefer not to use any of the aforementioned dependency managers, you can integrate MKWebViewBridge into your project manually.
Open up Terminal, cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
Add MKWebView as a git submodule by running the following command:
$ git submodule add https://github.com/vincent-k-sm/MKWebviewBridge
After download We Suggest Remove
Example
Folder When Embed
Click on the +
button under th Frameworks, Libraries, and Embedded Content
Click on the Add Other
and Select in MKFoundation
folder
Drag the MKFoundation
folder into the Project Navigator of your application's Xcode project.
It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
And that's it!
Open up Terminal, cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
Add MKWebView as a git submodule by running the following command:
$ git submodule add https://github.com/vincent-k-sm/MKWebviewBridge
$ cd MKWebviewBridgeExample
https://github.com/vincent-k-sm/MKWebviewBridge/tree/master/MKWebviewBridgeExample
/// Webview๋ฅผ ์ธํ
ํ๋ MKWebViewConfiguration ์
๋๋ค
/// - Parameters:
/// - host: deeplink ์ ํ์ฉ๋๋ host๊ฐ ์ ์ฉ๋ฉ๋๋ค (plist ๋ด ๋ฑ๋ก๋ url Types๋ฅผ ๋ชจ๋ ์ฒดํฌํฉ๋๋ค)
/// - urlString: landing url ์ ์ค์ ํฉ๋๋ค
/// - title: ๋ค๋น๊ฒ์ด์
๋ฐ ๋ด ํ์ด์ง ํ์ดํ์ ์ค์ ํฉ๋๋ค
/// - leftBtn: ์ข์ธก ๋ฒํผ (<-) ์ ํ์ฑํ ํฉ๋๋ค
/// - rightBtn: ์ฐ์ธก ๋ฒํผ (x) ์ ํ์ฑํ ํฉ๋๋ค
/// - navigationColor: ๋ค๋น๊ฒ์ด์
๋ฐ์ ์์์ ์ง์ ํฉ๋๋ค (hexString)
/// - statusBarColor: ์๋จ ์คํ
์ดํฐ์ค ๋ฐ์ ์์์ ์ง์ ํฉ๋๋ค (hexString)
/// - tintColor: ๋ค๋น๊ฒ์ด์
๋ฐ ๋ด ๋ฒํผ, ํ์ดํ ๋ฑ์ ์์์ ์ง์ ํฉ๋๋ค (hexString)
/// - removeStack:๊ธฐ์กด stack ์ ์์ฌ์๋ ๋ชจ๋ controller๋ฅผ ์ ๊ฑฐํฉ๋๋ค
/// - navigationBarIsEnable:
/// -> host, urlString, navigationBarIsEnable, removeStack ๊ฐ์ ๋ฐ๋ผ ๋ค๋น๊ฒ์ด์
๋ฐ ๋
ธ์ถ ์ฌ๋ถ๋ฅผ ์ค์ ํฉ๋๋ค
/// - important:
/// -> host, urlString, navigationBarIsEnable, removeStack ์ ์ ์ธํ๊ณ ๋ชจ๋ value๊ฐ nil ์ธ ๊ฒฝ์ฐ ๋ค๋น๊ฒ์ด์
๋ฐ๊ฐ ๋
ธ์ถ๋์ง ์์ต๋๋ค
public struct MKWebViewConfiguration: Codable {
public var host: String = ""
public var urlString: String = ""
public var title: String? = nil
public var leftBtn: Bool? = nil
public var rightBtn: Bool? = nil
public var navigationColor: String? = nil
public var statusBarColor: String? = nil
public var tintColor: String? = nil
public var removeStack: Bool? = nil
public var navigationBarIsEnable: Bool = false
}
mkwebview://webview?url=https://smbh.kr/mk_bridge/sample
&title=ํ
์คํธ
&navigationColor=FFFFFF
&tintColor=000000
&statusBarColor=FFFFFF
/// create option
let configure = MKWebViewConfiguration(
host: "webview",
urlString: "https://smbh.kr/mk_bridge/sample",
title: <#T##String?#>,
leftBtn: <#T##Bool?#>,
rightBtn: <#T##Bool?#>,
navigationColor: <#T##String?#>,
statusBarColor: <#T##String?#>,
tintColor: <#T##String?#>,
removeStack: <#T##Bool?#>
)
/// set configuration
let vc = MKWebViewController()
vc.configuration = configure
self.pushViewController(vc, animated: true)
open func headers() -> [String: String] {
return [:]
}
private var headerInfos: [String: String] = [
"Content-Type": "application/json",
"app-device-uuid": UUID().uuidString,
"app-device-os-version": UIDevice.current.systemVersion,
"app-version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
"access-token": "access-token",
"refresh-token": "refresh-token-value"
]
----
override func headers() -> [String: String] {
return self.headerInfos
}
open func cookies() -> [HTTPCookie] {
return []
}
override func cookies() -> [HTTPCookie] {
var cookies: [HTTPCookie] = []
if let uuidCookie = HTTPCookie(properties: [.domain: "smbh.kr",
.path: "/",
.name: "CID",
.value: "\(UUID().uuidString)",
.secure: "TRUE"]) { cookies.append(uuidCookie) }
return cookies
}
WebkitManager.shared.removeCookies {
//
}
or
MKWebview.shared.clearCookies(completion: nil)
open func loadURLString() -> String? {
return nil
}
override func loadURLString() -> String? {
return "https://smbh.kr/mk_bridge/sample"
}
open func loadLocalFile() -> URL? {
return nil
}
override func loadLocalFile() -> URL? {
guard let url = Bundle.main.url(forResource: "sampleScheme", withExtension: "html") else { return nil }
return url
}
can handle in
addPostMessageHandler(##String##)
ononAddPostMessage
open func onAddUserScript() -> String? {
return nil
}
eg. It can Called
window.CustomScripts.showToast('msg';)
in JavaScript
//// Add Script
override func onAddUserScript() -> String? {
return """
CustomScripts = {
showToast(s) {
window.webkit.messageHandlers.showToast.postMessage(s);
},
}
"""
/// Handle
override func onAddPostMessage() {
addPostMessageHandler("showToast") { (res) in
if let res = res as? String {
Toast.shared.makeToast(res)
}
}
}
}
open func onAddPostMessage() {
//
}
eg. It can Called
window.webkit.messageHandlers.showToast.postMessage('msg';)
in JavaScript
override func onAddPostMessage() {
super.onAddPostMessage()
addPostMessageHandler("showToast") { (res) in
// do stuff
if let res = res as? String {
Toast.shared.makeToast(res)
}
}
}
(iOS 14.0 *)
when call Javascript with promiseeg. It can Called
window.webkit.messageHandlers.showToast.postMessage('msg';)
in JavaScript
///--- javaScript ---
var promise = window.webkit.messageHandlers.testWithPromise.postMessage( inputInfo );
promise.then(
function(result) {
console.log(result); // "Stuff worked!"
successFunc( result )
},
function(err) {
console.log(err); // Error: "It broke"
errorFunc( err )
});
///--- override func onAddPostMessage() ---
if #available(iOS 14.0, *) {
let promiseResult: ReplyHandler = ("Return Data", nil)
addPostMessageReplyHandler("testWithPromise", handler: promiseResult, result: { result in
print("Called")
})
}
func evaluateJavascript(_ function: String, result: ((Bool, Any?) -> Void)?)
let value = "javascript:setToken('NEW_ACSESS_TOKEN');"
self.evaluateJavascript(value) { (result, _ ) in
Debug.print(result)
}
public func reloadWebview()
self.reloadWebview()
link |
Stars: 0 |
Last commit: 1 week ago |
Full Changelog: https://github.com/vincent-k-sm/MKWebviewBridge/commits/1.0.0
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics