Skip to content

Commit a0227f4

Browse files
committed
first commit
1 parent 93762d0 commit a0227f4

13 files changed

+724
-0
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PackageDescription
55

66
let package = Package(
77
name: "PamSDKSwift",
8+
platforms: [.iOS(.v14)],
89
products: [
910
// Products define the executables and libraries a package produces, and make them visible to other packages.
1011
.library(

Sources/PamSDKSwift/HttpClient.swift

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
10+
enum HttpClient {
11+
typealias OnSuccess = ([String: Any]?) -> Void
12+
13+
static func get(url: String, queryString: [String: String]?, headers: [String: String]?, onSuccess: OnSuccess?) {
14+
guard var url = URLComponents(string: url) else { return }
15+
url.queryItems = []
16+
queryString?.forEach {
17+
url.queryItems?.append(URLQueryItem(name: $0.key, value: $0.value))
18+
}
19+
20+
guard let reqURL = url.url else { return }
21+
22+
var request = URLRequest(url: reqURL)
23+
request.httpMethod = "GET"
24+
request.addValue("application/json", forHTTPHeaderField: "Accept")
25+
26+
headers?.forEach {
27+
request.addValue($0.value, forHTTPHeaderField: $0.key)
28+
}
29+
30+
let session = URLSession.shared
31+
session.dataTask(with: request) { data, _, error in
32+
if error == nil, let data = data {
33+
34+
if Pam.shared.isEnableLog {
35+
print("🛺 PAM", String(data: data, encoding: .utf8) ?? "" )
36+
}
37+
38+
let resultDictionay = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
39+
onSuccess?(resultDictionay)
40+
}
41+
}.resume()
42+
}
43+
44+
static func post(url: String, queryString: [String: String]?, headers: [String: String]?, json: [String: Any]?, onSuccess: OnSuccess?) {
45+
guard var url = URLComponents(string: url) else { return }
46+
url.queryItems = []
47+
queryString?.forEach {
48+
url.queryItems?.append(URLQueryItem(name: $0.key, value: $0.value))
49+
}
50+
51+
guard let reqURL = url.url else { return }
52+
53+
var request = URLRequest(url: reqURL)
54+
request.httpMethod = "POST"
55+
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
56+
request.addValue("application/json", forHTTPHeaderField: "Accept")
57+
58+
headers?.forEach {
59+
request.addValue($0.value, forHTTPHeaderField: $0.key)
60+
}
61+
62+
if let json = json {
63+
request.httpBody = try? JSONSerialization.data(withJSONObject: json, options: [])
64+
}
65+
66+
let session = URLSession.shared
67+
session.dataTask(with: request) { data, _, error in
68+
if error == nil, let data = data {
69+
70+
if Pam.shared.isEnableLog {
71+
print("🛺 PAM", String(data: data, encoding: .utf8) ?? "" )
72+
}
73+
74+
let resultDictionay = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
75+
onSuccess?(resultDictionay)
76+
}
77+
}.resume()
78+
}
79+
}

Sources/PamSDKSwift/PAMHelper.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
10+
11+
class PAMHelper {
12+
static func prettify(dict: [String: Any]) -> String {
13+
let jsonData = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
14+
return String(data: jsonData, encoding: .utf8) ?? ""
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
10+
extension Pam {
11+
var appVersion: String {
12+
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
13+
}
14+
15+
var appBuild: String {
16+
return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
17+
}
18+
19+
var versionBuild: String {
20+
let version = appVersion, build = appBuild
21+
return version == build ? "v\(version)" : "v\(version)(\(build))"
22+
}
23+
24+
var osVersion: String {
25+
let version = ProcessInfo().operatingSystemVersion
26+
return "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
27+
}
28+
29+
var bundleID:String{
30+
return Bundle.main.bundleIdentifier ?? ""
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
10+
extension Pam {
11+
class StandardEvent {
12+
struct PageView: PamEvent {
13+
14+
let pageURL:String?
15+
16+
func getPayload() -> [String : Any] {
17+
return [:]
18+
}
19+
20+
func getEvent() -> String {
21+
return ""
22+
}
23+
}
24+
}
25+
}

Sources/PamSDKSwift/Pam+Static.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
extension Pam {
12+
13+
public static func track(event: String, payload: [String: Any]? = nil) {
14+
Pam.shared.track(event: event, payload: payload)
15+
}
16+
17+
public static func userLogin() {
18+
Pam.shared.updateCustomerID()
19+
}
20+
21+
public static func userLogout() {
22+
Pam.shared.userLogout()
23+
}
24+
25+
public static func initialize(launchOptions: [UIApplication.LaunchOptionsKey: Any]?, enableLog: Bool = false) throws {
26+
try Pam.shared.initialize(launchOptions: launchOptions, enableLog: enableLog)
27+
}
28+
29+
public static func getCustomerID(){
30+
31+
}
32+
33+
public static func listen(_ event: String, callBack: @escaping ListenerCallBack) {
34+
Pam.shared.listen(event, callBack: callBack)
35+
}
36+
37+
public static func setDeviceToken(deviceToken: Data) {
38+
Pam.shared.setDeviceToken(deviceToken: deviceToken)
39+
}
40+
41+
public static func didReceiveRemoteNotification(userInfo: [AnyHashable: Any], fetchCompletionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> Bool {
42+
return Pam.shared.didReceiveRemoteNotification(userInfo: userInfo, fetchCompletionHandler: fetchCompletionHandler)
43+
}
44+
45+
public static func askNotificationPermission() {
46+
Pam.shared.askNotificationPermission()
47+
}
48+
49+
public static func appReady() {
50+
Pam.shared.appReady()
51+
}
52+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
import UserNotifications
10+
11+
extension Pam: UNUserNotificationCenterDelegate {
12+
public func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
13+
completionHandler([.sound, .badge, .banner])
14+
}
15+
16+
public func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
17+
18+
if isAppReady {
19+
dispatch("onPushNotification", data: response.notification.request.content.userInfo)
20+
} else {
21+
pendingNotification.append(response.notification.request.content.userInfo)
22+
}
23+
24+
completionHandler()
25+
}
26+
27+
}

0 commit comments

Comments
 (0)