Requirements

The WinWinKit SDK is available in native Swift and supports iOS 16 and macOS 13 or later.

Installation

Swift Package Manager

You can use Swift Package Manager to add WinWinKit to your Xcode project.

Select “File” -> “Add Packages Dependencies”, enter the repository URL and click “Add Package”. The library should be added to the Package Dependencies section and you should now be able to import WinWinKit in your source files.

Repository URL:

https://github.com/winwinkit/winwinkit-swift.git

Configuration

Configure the SDK with the API key and set the referral user’s App User Id.

import WinWinKit

Referrals.configure(apiKey: "your-api-key")
// Singleton can be used after configure(apiKey:) method has been called.
Referrals.shared.set(appUserId: "your-app-user-id")

Set the First Seen At

Recommended to set first seen at which is the date when the user was first seen in your app. This is used to evaluate user’s eligibility to claim code of another user. By default it is 7 days since the first seen at date.

If not set, the first seen at is the date the user was created in WinWinKit.

Referrals.shared.set(firstSeenAt: Date())

Update referral user properties

Is Premium

Update is premium property to track referring user’s stats and enable rewards activation on conversion.

Referrals.shared.set(isPremium: true)

Last Seen At

Tracked automatically by the SDK. To disable auto tracking, set the shouldAutoUpdateLastSeenAt property to false before setting the appUserId property.

Referrals.shared.set(lastSeenAt: Date())

Metadata

Optionally set metadata to save user’s additional properties.

Referrals.shared.set(metadata: ["key": "value"])

Referral user

Referral user object contains all necessary information needed to display in the user interface.

Access the latest referral user object.

let referralUser = Referrals.shared.referralUser

Also available as a property of ReferralUserObservableObject (see below).

Claim code

Claim code object contains all necessary information needed to claim a code.

Access the latest claim code object.

Referrals.shared.claim(code: referralCode) { result in
    switch result {
        case .success(let (referralUser, grantedRewards)):
            // grant access to rewards
        case .failure(let error):
            // handle error
    }
}

Another way to claim code is to call the claim method of ReferralClaimCodeObservableObject.

Delegate

Set a delegate to receive events from the SDK.

let delegate = Delegate()
Referrals.shared.delegate = delegate

// Retain the delegate object to keep receiving events.

...


final class Delegate: ReferralsDelegate {
    
    func referrals(_ referrals: Referrals, receivedUpdated referralUser: ReferralUser?) {
        // Called every time the referral user is updated.
    }
    
    func referrals(_ referrals: Referrals, receivedError error: any Error) {
        // Received error when creating, updating or fetching the referral user.
    }
    
    func referrals(_ referrals: Referrals, isRefreshingChanged isRefreshing: Bool) {
        // Called when internal refreshing state changes.
    }
}

Setting the delegate is optional when using observable objects described below.

Observable objects for SwiftUI

The SDK provides convenient Observable objects to observe changes to the referral user and claim code.

ReferralUserObservableObject

Provides observable ReferralUser and isRefreshing properties.

@State var referralUserObservableObject = Referrals.shared.referralUserObservableObject

...

VStack {
    Text("Your referral code")
    Text(self.referralUserObservableObject.referralUser?.code ?? "-")
      .font(.title3)
}

ReferralClaimCodeObservableObject

Provides observable isClaimingCode, didClaimCodeSuccessfully, grantedRewards properties and claim(code:) method.

@State var referralClaimCodeObservableObject = Referrals.shared.referralClaimCodeObservableObject

...

Button(action: {
    self.referralClaimCodeObservableObject.claim(code: self.referralCode)
}) {
    Text("Claim referral code")
}

In SwiftUI only apps it is possible to build complete integration with only these observable objects.

User Interface

The SDKs provide interface for interacting with WinWinKit APIs, but not the UI.

Thus you need to build the user interface for the referral feature in your app.

We plan to provide UI with the SDK in the future. Please let us know if that is your requirement to get started.


Check out the SDK on GitHub