Skip to content

Instantly share code, notes, and snippets.

@benedictbartsch
Last active September 16, 2021 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benedictbartsch/d03c9d90cb24528138168232a77973c8 to your computer and use it in GitHub Desktop.
Save benedictbartsch/d03c9d90cb24528138168232a77973c8 to your computer and use it in GitHub Desktop.
Example implementation of FeatureCat's public board in an iOS UIKit app. It has all the code required in one single file for tutorial purposes - but it works.
import Foundation
import WebKit
import UIKit
// This is just an example ViewController that has been stripped of all code except the one relevant for FeatureCat.
class AnyViewController: UIViewController {
// Excluded here: All the other code you need for your UI.
// Return the WebView in whatever way suits you best (segues, etc.).
private func featureCatWebKitController() -> WKWebKitViewController {
// Create the FCUser with all of your properties. Only the user_id is required for FeatureCat and should be cached on device if it is only used for FeatureCat.
let fcUser = FCUser(user_id: ...)
// Grab the predefined request - or just define it here.
var request = URLRequest.featureCatBaseRequest
// Create a JSONEncoder for encoding the Codable FCUSer struct. Set the dateEncodingStrategy to ISO 8601 to make sure dates are formatted properly.
let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .iso8601
// Encode the fcUser struct you created earlier and attach ist to the request body on success.
if let json = try? jsonEncoder.encode(fcUser) {
request?.addValue("application/json", forHTTPHeaderField: "Content-Type")
request?.httpBody = json
}
// Create w WebKitController (or hande the request in whatever way you prefer), attach the request and your good to go.
let wkWebKitController = WKWebKitViewController()
wkWebKitController.request = request
return wkWebKitController
}
}
extension URLRequest {
static var featureCatBaseRequest: URLRequest? {
// Replace this URL with the one of your public board. Simply change the subdomain.
guard let url = URL(string: "https://appity.featurecat.app/identify") else {return nil}
var request = URLRequest(url: url)
request.httpMethod = "POST"
// Grab the token for your product from the product's settings in FeatureCat.
let token = "yoursecrettokenasgeneratedbyfeaturecat"
request.setValue( "Bearer \(token)", forHTTPHeaderField: "Authorization")
return request
}
}
// Creating these codable Structs is optional, but it does make the JSON creation a lot nicer. Instatiate these when creating the requests and fill them with your required data.
struct FCUser: Codable {
let user_id: String
let revenue: Double?
let name: String?
let email: String?
let user_since: Date?
let app_version: String?
let user_properties: FCUserProperties?
}
struct FCUserProperties: Codable {
let property1: String
// Add whatever you need here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment