Skip to content

Instantly share code, notes, and snippets.

@HelloNamiML
HelloNamiML / useArgumentResetStorage.swift
Created November 5, 2021 04:18
Use command line argument to reset app storage
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if ProcessInfo.processInfo.arguments.contains("CLEANSTART") {
// Call internal method to clear existing application storage
resetApplicationStorage()
}
}
@HelloNamiML
HelloNamiML / testLaunchArguments.swift
Created November 5, 2021 04:17
Add arguments to launch app with cleared storage
func testCallFlowFirstStart() {
// Re-launch the app with an argument to clear out storage
let app = XCUIApplication()
app.launchArguments.append("CLEANSTART")
app.launch()
// Rest of your test case follows....
}
@HelloNamiML
HelloNamiML / testPurchasePressed.swift
Created November 5, 2021 04:15
Move a unit test to a ui test
@IBAction func testPurchasePressed(_ sender: Any) {
NamiPurchaseManager.skusForSKUIDs(skuIDs: ["test_product_id"], productHandler: { (_, products, _, error) in
// We make sure StoreKit can load the product we intend to purchase.
if let products = products, let product = products.first {
// Tell the Nami SDK to initiate a purchase via StoreKit.
NamiPurchaseManager.buySKU(product, responseHandler: { (purchases, _, _) in
if purchases.count > 0 {
// Success
DispatchQueue.main.async {
// Make the result available in both the text of the label and the accessibilityValue
@HelloNamiML
HelloNamiML / testPurchaseLabel.swift
Created November 5, 2021 04:14
Display a button with a result of a ui test
override func viewDidLoad() {
super.viewDidLoad()
self.transactionSentLabel?.accessibilityIdentifier = "purchaseResultLabel"
// Make the result available in both the text of the label and the accessibilityValue
self.transactionSentLabel?.text = "none"
self.transactionSentLabel?.accessibilityValue = "none"
}
@HelloNamiML
HelloNamiML / testPurchase.swift
Created November 5, 2021 04:11
UI Test making a StoreKit purchase
class UnitTestingAppUITests: XCTestCase {
func testPurchase() {
// Make sure to use the StoreKit configuration file that Xcode supports for testing purchases.
if let session = try? SKTestSession(configurationFileNamed: "UnitTestStoreKitConfig.storekit") {
session.disableDialogs = true
session.clearTransactions()
}
@HelloNamiML
HelloNamiML / storekit2_verify_purchase.swift
Created June 11, 2021 21:34
StoreKit 2: Verify a purchase on-device
let result = await Transaction.latest(for: "myProductID")
switch result {
case .unverified:
// StoreKit has parsed the JWS but failed verification. Don't deliver content to the user.
throw StoreError.failedVerification
case .verified(let safe):
// If the transaction is verified, unwrap and return it.
return safe
}
@HelloNamiML
HelloNamiML / storekit2_purchase_product.swift
Created June 11, 2021 21:31
StoreKit 2: Purchase Product
let result = try await storeProducts.first.purchase()
@HelloNamiML
HelloNamiML / storekit2_get_product.swift
Created June 11, 2021 20:25
StoreKit 2: Get Product
let storeProducts = try await Product.request(with: Set("myProductID"))
@HelloNamiML
HelloNamiML / navigation_paid.swift
Last active March 22, 2021 19:39
Control Navigation Based on Purchase State
var body: some View {
NavigationView {
VStack(spacing: 30) {
Text("Select an option")
.padding()
if subscriptionDataSource.subscribed {
NavigationLink(destination: PaidView()) {
Text("Paid Content")
}
@HelloNamiML
HelloNamiML / conditional_view.swift
Last active March 22, 2021 19:39
Conditionally Display Content on a View based on Subscription State
var body: some View {
VStack(spacing: 30) {
if !subscriptionDataSource.subscribed {
// if the user is not subscribed, show a subscribe button
Button(action: {
// pushing button raises a paywall with Nami
NamiPaywallManager.raisePaywall(fromVC: nil)
}) {
Text("Subscribe Today!")
.background(Color.white)