Code Monkey home page Code Monkey logo

swifttipjar-swiftui-example's Introduction

SwiftUI tip jar example, using SwiftTipJar

This SwiftUI sample code demonstrates ease of use for SwiftTipJar - an open source package.

There is also UIKit sample code (same functionality).

Tip jars on Apple platforms are a concept of letting users make in-app purchases to show appreciation for the app/developer. These don't actually unlock any additional features.

Demo app screenshots

Choice of tips System UI for purchasing Successful purchase Thank you message
Choice of tips System UI for purchasing Successful purchase Thank you message

Usage

  1. Init TipJar with identifiers for In-App Purchases you wish to offer
  2. Make TipJar observe StoreKit's payment queue. It's best if observed during entire lifetime of the app.
  3. Let TipJar find out product information for identifiers you've supplied. It's either going to be fetched from Xcode (when using StoreKit Configuration file) or from App Store Connect over the network.
// #1
tipJar = SwiftTipJar(tipsIdentifiers: Set(["com.test.smallTip", "com.test.largeTip"]))
// #2
tipJar.startObservingPaymentQueue()
// #3
tipJar.productsRequest?.start()
  1. As soon as tipJar finds out about the products, it publishes tips, an array of Tip objects that supplies you with displayName and displayPrice, in local language and currency. Array has been sorted by price, ascending. Products that haven't been configured with a name and/or price are simply omitted from this array.
@Published public private(set) var tips: [Tip] = []
public final class Tip {
    public var identifier: String = ""
    public var displayName: String = ""
    public var displayPrice: String = ""

    /// TipJar uses this to quickly check if it should include this Tip in its published array.
    var isValid: Bool {
        return !identifier.isEmpty && !displayName.isEmpty && !displayPrice.isEmpty
    }
}
  1. Assign any code you want to run after a successful purchase to transactionSuccessfulBlock, and similarly any code you want to run after a failed purchase (user clicked Cancel instead of Purchase) to transactionFailedBlock. With tip jars, it's even natural to do nothing if a transaction fails.
.onAppear {
  tipJar.transactionSuccessfulBlock = {
    showingThankYou = true
  }
  tipJar.transactionFailedBlock = {
    // No need to do anything, user did tap cancel
  }
}
  1. Set up button(s) to initiate a purchase to trigger the action initiatePurchase(productIdentifier:)
Button {
  tipJar.initiatePurchase(productIdentifier: tip.identifier)
} label: {
  Text("\(tip.displayName) \(tip.displayPrice)")
}

Troubleshooting

Q: I've set up IAPs in a StoreKit Configuration file, but nothing is showing up in the demo app

A: Edit Scheme > Options, and check that you have the correct StoreKit Configuration selected

Q: I've set up IAPs in StoreKit Configuration file, some are showing up in demo app, but not all

A: Check that TipJar's init is called with all relevant identifiers; check identifiers for any spelling errors. Check that you've provided the name and price for every IAP you want to see in the demo app.

swifttipjar-swiftui-example's People

Contributors

dkasaj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

swifttipjar-swiftui-example's Issues

SPM Issues

Hi,

I've been trying to add the SPM to my project and had issues.
https://github.com/dkasaj/SwiftTipJar.git

So I tried the example SwiftUI project
https://github.com/dkasaj/SwiftTipJar-SwiftUI-Example

However, it has the same issues..

Package.resolved file is corrupted or malformed; fix or delete the file to continue: unsupported schema version 2

Also when I added the SPM to my project, the readme didn't show in the SPM dialog, I guess this is the same problem.

Please advise.

Thanks,

Jules.

Error in console in first run of sample code

When running the sample app for the first time after opening an instance of the simulator, I get multiple instances of the following error in the Xcode console:
"2022-10-08 12:58:25.505812-0500 SwiftTipJar SwiftUI Example[10428:160315] [SwiftUI] Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates."

Note that this does not happen when I run the sample app again without closing the simulator.

Do you have any idea what's causing this, and how to work around it? Is it possibly timing related?

Thanks! The code looks great if I can just get past this issue. :)

EDIT: I get this warning in the code: "Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates."
on the line:
@published public private(set) var tips: [Tip] = []

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.