-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eeac947
Showing
8 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
.build | ||
.swiftpm/xcode/package.xcworkspace/xcuserdata |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
.swiftpm/xcode/xcuserdata/theodor.greholm.xcuserdatad/xcschemes/xcschememanagement.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>SchemeUserState</key> | ||
<dict> | ||
<key>TrustlyIosSdk.xcscheme_^#shared#^_</key> | ||
<dict> | ||
<key>orderHint</key> | ||
<integer>0</integer> | ||
</dict> | ||
</dict> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "TrustlyIosSdk", | ||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "TrustlyIosSdk", | ||
targets: ["TrustlyIosSdk"]), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "TrustlyIosSdk", | ||
dependencies: []), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# TrustlyIosSdk | ||
|
||
A description of this package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Trustly Group AB | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
|
||
#import <UIKit/UIKit.h> | ||
|
||
//! Project version number for trustly-framework-ios. | ||
FOUNDATION_EXPORT double trustly_framework_iosVersionNumber; | ||
|
||
//! Project version string for trustly-framework-ios. | ||
FOUNDATION_EXPORT const unsigned char trustly_framework_iosVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <trustly_framework_ios/PublicHeader.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Trustly Group AB | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import Foundation | ||
import WebKit | ||
|
||
/** | ||
Will try to open the URL, then return result in callback | ||
:param: JSON | ||
*/ | ||
public class TrustlyWKScriptOpenURLScheme: NSObject, WKScriptMessageHandler { | ||
|
||
public static let NAME = "trustlyOpenURLScheme" | ||
var webView: WKWebView | ||
|
||
public init(webView: WKWebView) { | ||
self.webView = webView | ||
} | ||
|
||
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | ||
if let parsed = getParsedJSON(object: message.body as AnyObject), | ||
let callback: String = parsed.object(forKey: "callback") as? String, | ||
let urlscheme: String = parsed.object(forKey: "urlscheme") as? String | ||
{ | ||
UIApplication.shared.openURL(NSURL(string: urlscheme)! as URL) | ||
let js: String = String(format: "%@", [callback, urlscheme]) | ||
webView.evaluateJavaScript(js, completionHandler: nil) | ||
} | ||
} | ||
|
||
/** | ||
Helper function that will try to parse AnyObject to JSON and return as NSDictionary | ||
:param: AnyObject | ||
:returns: JSON object as NSDictionary if parsing is successful, otherwise nil | ||
*/ | ||
func getParsedJSON(object: AnyObject) -> NSDictionary? { | ||
do { | ||
let jsonString: String = object as! String | ||
let jsonData = jsonString.data(using: String.Encoding.utf8)! | ||
let parsed = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary | ||
return parsed | ||
} catch let error as NSError { | ||
print("A JSON parsing error occurred:\n \(error)") | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Trustly Group AB | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import UIKit | ||
import WebKit | ||
import SafariServices | ||
|
||
public class TrustlyWKWebView: UIView, WKNavigationDelegate, WKUIDelegate, SFSafariViewControllerDelegate { | ||
var trustlyView: WKWebView? | ||
|
||
public init?(checkoutUrl: String, frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
let userContentController: WKUserContentController = WKUserContentController() | ||
let configuration: WKWebViewConfiguration = WKWebViewConfiguration() | ||
configuration.userContentController = userContentController | ||
configuration.preferences.javaScriptCanOpenWindowsAutomatically = true | ||
|
||
trustlyView = WKWebView(frame: frame, configuration: configuration) | ||
guard let trustlyView = trustlyView else { return nil } | ||
|
||
trustlyView.navigationDelegate = self | ||
trustlyView.uiDelegate = self | ||
trustlyView.navigationDelegate = self | ||
trustlyView.uiDelegate = self | ||
|
||
userContentController.add( | ||
TrustlyWKScriptOpenURLScheme(webView: trustlyView), name: TrustlyWKScriptOpenURLScheme.NAME) | ||
if let url = URL(string: checkoutUrl) { | ||
trustlyView.load(URLRequest(url: url)) | ||
trustlyView.allowsBackForwardNavigationGestures = true | ||
} | ||
|
||
addSubview(trustlyView) | ||
} | ||
|
||
public required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { | ||
|
||
if navigationAction.targetFrame == nil { | ||
if let parentViewController: UIViewController = UIApplication.shared.keyWindow?.rootViewController, | ||
let url = navigationAction.request.url { | ||
let safariView = SFSafariViewController(url: url) | ||
parentViewController.present(safariView, animated: true, completion: nil) | ||
safariView.delegate = self | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} |