Use https://github.com/web2wave/web2wave_swift
import UIKit
import Singular
import Web2Wave
class ViewController: UIViewController {
var userId: String?
var subscriptionStatus: String?
override func viewDidLoad() {
super.viewDidLoad()
// Set up Singular SDK
if let config = getConfig() {
Singular.start(config)
}
// Configure Web2Wave
Web2Wave.shared.baseURL = URL(string: "<https://moongate.web2wave.com/>")
Web2Wave.shared.apiKey = "YOUR_API_KEY_TO_WEB2WAVE"
// Update subscription status on app launch
if let userId = userId {
Task {
await fetchSubscriptionStatus(userId: userId)
}
}
}
func getConfig() -> SingularConfig? {
// Create the config object with the SDK Key and SDK Secret
guard let config = SingularConfig(apiKey: "YOUR_SINGULAR_API_KEY", andSecret: "YOUR_SINGULAR_SECRET") else {
return nil
}
// If you are using App Tracking Transparency:
// Set a 300 sec delay before initialization to wait for
// the user's ATT response.
// (Remove this if you are not displaying an ATT prompt!)
config.waitForTrackingAuthorizationWithTimeoutInterval = 300
// Support custom ESP domains
config.espDomains = ["links.your-website-domain.com"]
// Set a handler method for deep links
config.singularLinksHandler = { params in
self.handleDeeplink(params: params)
}
return config
}
private func handleDeeplink(params: SingularLinkParams?) {
// Get Deeplink data from Singular Link
let deeplink = params?.getDeepLink()
let passthrough = params?.getPassthrough()
let isDeferredDeeplink = params?.isDeferred()
let urlParams = params?.getUrlParameters()
// Add deep link handling code here
if let deeplink = deeplink {
print("Deeplink: \\(deeplink)")
}
if let passthrough = passthrough {
print("Passthrough: \\(passthrough)")
}
if let isDeferred = isDeferredDeeplink {
print("Is Deferred: \\(isDeferred)")
}
if let urlParams = urlParams {
print("URL Parameters: \\(urlParams)")
}
// Example: Process deeplink to extract user_id and fetch subscription
if let userId = urlParams?["user_id"] as? String {
self.userId = userId
print("User ID from deep link: \\(userId)")
// Store user_id locally
UserDefaults.standard.set(userId, forKey: "userId")
// Fetch subscription status
Task {
await fetchSubscriptionStatus(userId: userId)
}
}
}
private func fetchSubscriptionStatus(userId: String) async {
do {
// Check if user has an active subscription using Web2Wave
let isActive = await Web2Wave.shared.hasActiveSubscription(userID: userId)
DispatchQueue.main.async {
if isActive {
// Provide paid content access
self.providePaidContent()
} else {
// Show paywall
self.showPaywall()
}
}
} catch {
print("Failed to fetch subscription status: \\(error.localizedDescription)")
}
}
private func providePaidContent() {
// Logic to grant user access to paid content
print("User has an active subscription. Providing access to paid content.")
}
private func showPaywall() {
// Logic to display paywall
print("No active subscription. Displaying paywall.")
}
}