Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorgreholmtrustly committed Oct 8, 2020
0 parents commit eeac947
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.build
.swiftpm/xcode/package.xcworkspace/xcuserdata
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>
21 changes: 21 additions & 0 deletions Package.swift
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: []),
]
)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# TrustlyIosSdk

A description of this package.
34 changes: 34 additions & 0 deletions Sources/TrustlyIosSdk/TrustlyIosSdk.h
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>
68 changes: 68 additions & 0 deletions Sources/TrustlyIosSdk/TrustlyOpenURLScheme.swift
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
}
}
75 changes: 75 additions & 0 deletions Sources/TrustlyIosSdk/TrustlyWKWebView.swift
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
}
}

0 comments on commit eeac947

Please sign in to comment.