Friendly Score iOS SDK delivers toolset for user scoring on iOS devices.

Features

  • handles authentication and communication to FriendlyScore servers
  • provides beautiful and customizable user interface
  • helps with connections to social networks
  • controls needed permissions using built-in Permissions Manager

Requirements

  • iOS 11.0+
  • Swift 4.0+

Instalation

To integrate FriendlyScore iOS SDK in your project, you must use CocoaPods (specific instructions how to add CocoaPods can be found here). Include line below to your Podfile:

pod 'FriendlyScoreSDK'

Usage

Appearance

Colors

Almost every single detail can be customized. Contact us if you want to change default color palette.

Text Labels

Every text label can be changed. Contact us if you want to change default text labels.

Fonts

It is possible to use custom bold&normal font in FriendlyScore SDK. Two conditions must be fulfilled:

  • fonts must be added to your XCode project (detailed instructions can be found here)
  • font names should be included in configuration file (contact us if you want to change system fonts)

This simple snippet helps you obtain font names included in your project:

func printFonts() {
    let fontFamilyNames = UIFont.familyNames
    for familyName in fontFamilyNames {
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNames(forFamilyName: familyName)
        print("Font Names = [\(names)]")
    }
}

If SDK won't be able to find given fonts, system font will be used.

Loader

SDK will use default loader (activity) view for inital loading of configuration and setup. You can change this behaviour and provide your custom class that conforms Loading protocol. Example below shows how to create own activity:

class MyLoader:UIView,Loading {
    var circle:UIView!
    public func stopAnimating() {
        self.circle.layer.removeAllAnimations()
        self.removeFromSuperview()
    }

    public func startAnimating() {
        animate()
    }

    func animate() {
        let a:CGFloat = circle.alpha == 1 ? 0 : 1
        UIView.animate(withDuration: 0.2, animations: {
            self.circle.alpha = a
        }) { _ in
            self.animate()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() {
        circle = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
        circle.layer.cornerRadius = 5
        circle.backgroundColor = UIColor.white
        addSubview(circle)
        circle.translatesAutoresizingMaskIntoConstraints = false

        let height = circle.heightAnchor.constraint(equalToConstant: 10)
        let width = circle.widthAnchor.constraint(equalToConstant: 10)
        let centerX = circle.centerXAnchor.constraint(equalTo: centerXAnchor)
        let centerY = circle.centerYAnchor.constraint(equalTo: centerYAnchor)

        NSLayoutConstraint.activate([width, height, centerX, centerY])

        backgroundColor = UIColor.black
    }
}

It creates white circle on black background and adds some opacity animation. To actually invoke loader in SDK you have to pass it in FriendlyScoreViewController constructor like this:

let myLoader = MyLoader()
FriendlyScore.show(credentials:yourCredentials, loader:myLoader)

Usage of FriendlyScoreViewController is explained in section below

Note that your custom loader will be constrained to edges of the screen.

Setup

Before using, import library to your project:

import FriendlyScoreSDK

In order to proper connection to FriendlyScore servers you need to provide credentials:

let credentials = FriendlyScore.Credentials(appId: "1004", sessionKey: nil)

If you wish add existing user, you should provide session key in credential object:

let credentials = FriendlyScore.Credentials(appId: "1004", sessionKey: "12345678905")

FriendlyScore iOS SDK is presented as modal UIViewController:

FriendlyScore.show(credentials:yourCredentials)

Completion handler

From now on, SDK takes control over user actions. Add completion handler to your code to be notified when score is ready:

FriendlyScore.completionHandler = { result in
    switch result {
    case .failure(let error):
        print(error.localizedDescription)
    case .success(let user):
        print("User id: \(user.id)")
    }
}

When succeed you will receive FriendlyScore.User object which contains score details:

public struct User {
    public var id:Int
    public var score:Int
    public var maxScore:Int
    public var scorePercent:Double
    public var data:Array<User.Data>
    public var avatarURL: URL?
}

Social data handler

SDK will calculate score for you but it's your resposibility to obtain all necessary access tokens for each social network. When user hits one of social network card, SDK will request for proper token. Example below shows how to manage this action for Google service:

FriendlyScore.socialDataHandler = { request in
    switch request.socialNetwork {
    case .google:
        //... 
        //... getting google access token goes here
        //...
        let token = FriendlyScore.Token.google(accessToken: "83qltg1r4aok0n383qltg1r4aok-0n3llilli",
                                              expiresIn: 13423353,
                                              idToken: "29tIiwiZXhwIjoxNDI2NDIwODAwLCJodHRwOi8-vdG")
        request.completion(token)
    default: break
    }
}

If your client's configuration includes other networks remember to provide necessary tokens, like:

FriendlyScore.accessTokenHandler = { request in
    switch request.socialNetwork {
    case .google:
        let googleToken = ...
        request.completion(googleToken)
    case .twitter:
        let twitterToken = ...
        request.completion(twitterToken)
    case .linkedin:
        let linkedinToken = ...
        request.completion(linkedinToken)
    case .paypal:
        let paypalToken = ...
        request.completion(paypalToken)
    }
}

Few helpers and advices for getting tokens can be found in demo application.