XIBLoader.swift
3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// XIBLoader.swift
// SwiftWarplyFramework
//
// Created for SPM XIB loading support
//
import UIKit
public class XIBLoader {
/// Safely load a view controller from XIB with proper bundle resolution
public static func loadViewController<T: UIViewController>(
_ type: T.Type,
nibName: String? = nil
) -> T? {
let actualNibName = nibName ?? String(describing: type)
guard Bundle.frameworkBundle.path(forResource: actualNibName, ofType: "nib") != nil else {
print("❌ [WarplySDK] XIB file '\(actualNibName).xib' not found")
return nil
}
return T(nibName: actualNibName, bundle: Bundle.frameworkBundle)
}
/// Safely load a table view cell from XIB
public static func loadTableViewCell<T: UITableViewCell>(
_ type: T.Type,
nibName: String? = nil
) -> T? {
let actualNibName = nibName ?? String(describing: type)
guard let nib = Bundle.safeLoadNib(named: actualNibName) else {
return nil
}
let objects = nib.instantiate(withOwner: nil, options: nil)
return objects.first as? T
}
/// Verify all required XIB files are present
public static func verifyXIBFiles() -> [String: Bool] {
let requiredXIBs = [
"ProfileViewController",
"CouponViewController",
"MyRewardsViewController",
"ProfileCouponFiltersTableViewCell",
"ProfileHeaderTableViewCell",
"ProfileQuestionnaireTableViewCell",
"MyRewardsOffersScrollTableViewCell",
"ProfileCouponTableViewCell",
"ProfileFilterCollectionViewCell",
"MyRewardsOfferCollectionViewCell",
"MyRewardsBannerOfferCollectionViewCell",
"MyRewardsBannerOffersScrollTableViewCell"
]
var results: [String: Bool] = [:]
let bundle = Bundle.frameworkBundle
for xibName in requiredXIBs {
let exists = bundle.path(forResource: xibName, ofType: "nib") != nil
results[xibName] = exists
if exists {
print("✅ [WarplySDK] XIB found: \(xibName)")
} else {
print("❌ [WarplySDK] XIB missing: \(xibName)")
}
}
return results
}
/// Safe registration of table view cells with XIB
public static func registerTableViewCell(
_ tableView: UITableView,
cellClass: AnyClass,
nibName: String,
identifier: String
) {
if let nib = Bundle.safeLoadNib(named: nibName) {
tableView.register(nib, forCellReuseIdentifier: identifier)
print("✅ [WarplySDK] Registered table view cell: \(nibName)")
} else {
print("❌ [WarplySDK] Failed to register table view cell: \(nibName)")
// Register a basic UITableViewCell as fallback
tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
}
}
/// Safe registration of collection view cells with XIB
public static func registerCollectionViewCell(
_ collectionView: UICollectionView,
cellClass: AnyClass,
nibName: String,
identifier: String
) {
if let nib = Bundle.safeLoadNib(named: nibName) {
collectionView.register(nib, forCellWithReuseIdentifier: identifier)
print("✅ [WarplySDK] Registered collection view cell: \(nibName)")
} else {
print("❌ [WarplySDK] Failed to register collection view cell: \(nibName)")
// Register basic UICollectionViewCell as fallback
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: identifier)
}
}
}