# Initialize Referd User Profile

Showing the **Referd** widget on your mobile application is slightly different than showing it on the website. You have two options; first, if you want to design your customer interface, you will use our set of REST APIs.\
\
The other option as this section elaborates, is through using our iOS SDK.

Using the SDK, you can open the **Referd** user profile from a button in your app, programmatically when someone does something, or from a persistent button that sits over your app’s UI.

When you trigger the **Referd** user profile, your user is presented with a home screen. This is configurable inside **Referd** to change how it looks and what’s presented.

From there, your user can check his progress across different **Referd** programs as per your configurations.

<figure><img src="https://983627972-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1bAQ4IvAKNqrQlsRN5b4%2Fuploads%2FXcTIA8yqOOlts6FbUE1R%2Fimage%20(4).png?alt=media&#x26;token=7c6e6b59-8448-440d-8256-aed4dcd40e15" alt=""><figcaption></figcaption></figure>

**Referd’s** views are accessible through the code below. You just need to use it on any button action.

## Register User

The **Register User** method is used to create or update user accounts at **Referd**. This API call should be made when the user successfully logs in or whenever there are updates to the user's account information.

```swift
// After completion block is called from init the SDK, you can register player

self.gameball?.registerPlayer(
    playerUniqueId: "UNIQUE_PLAYER_ID", // The only required field
    deviceToken: "firebase_token",
    playerAttributes: [
        "custom_attribute": "custom_value",
        "country": "Egypt"
    ],
    completion: { (playerId, error) in // Completion block after registeration is complete
    // You either get Referd playerId or error
})
```

The below is description of register user parameters

<table data-header-hidden><thead><tr><th width="212">Parameter</th><th>Type</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Type</strong></td><td><strong>Required</strong></td><td><strong>Description</strong></td></tr><tr><td><code>playerUniqueId</code></td><td><strong>string</strong></td><td><strong>Yes</strong></td><td><p>Unique identifier for the user in your database.</p><p>Could be database ID, random string, email or anything that uniquely identifies the user.</p></td></tr><tr><td><code>deviceToken</code></td><td><strong>string</strong></td><td><strong>No</strong></td><td>Firebase token obtained from Firebase SDK to avail <strong>Referd</strong> to send notifications to the user.</td></tr><tr><td><code>playerAttributes</code></td><td><strong>dictionary</strong></td><td><strong>No</strong></td><td>Any custom parameters you want to attach to the user.</td></tr><tr><td><code>completion</code></td><td><strong>Completion block</strong></td><td><strong>Yes</strong></td><td>Returns either <strong>Referd</strong> user ID or error in registration.</td></tr></tbody></table>

{% hint style="success" %}
Every time the **Referd View** is initialized with a new **PlayerUniqueId** , the user profile is created or updated at **Referd** side. You may consider enriching your Referd's user profile with attributes that are not available to the UI by using server side [Create\Update User API](https://developer.tryreferd.com/api-reference/api-reference/player#post-create-player)
{% endhint %}

{% hint style="warning" %}
**Choose an Unchangeable Player Unique ID**

**Referd** user profile gets created using the **`playerUniqueId`**. It is highly recommended to have the unique ID as an identifier that would NEVER be changed. If this unique ID changes for a given user , you risk losing all original data for that user on **Referd**. Accordingly, it is NOT recommended to use email address or mobile number as the unique ID as both can be changed by the user at anytime.
{% endhint %}

To generate **Device Token (FCM Token)** you can use Firebase latest SDK you can put this code in AppDelegate or your ViewController to get the token and once you get the token you can call the above method to register and Launch Referd&#x20;

```swift
// Based on latest Firebase documentation you can generate token by

       Messaging.messaging().token { token, error in
          if let error = error {
            print("Error fetching FCM registration token: \(error)")
          } else if let token = token {
            print("FCM registration token: \(token)")
              // you can save the token anyware then use it while launching Referd
          }
        }
```

Once the **APIKey** and **playerUniqueId** have been registered, Referd views can be made visible to the user. So ideally, **`registerPlayer`** is called after the **`completion block`** of the SDK initalization.

## Show the widget

To show the Referd user profile that contains the user details, user challenges, and the leaderboard use the ***showProfile*** SDK function.

```swift
@IBAction func didTapLaunch(_ sender: UIButton) {
    gameball?.showProfile(
        playerUniqueId: "UNIQUE_PLAYER_ID",
        openDetail: "custom_detail",
        hideNavigation: false,
        completion: { viewController, errorMessage in
            guard let gameBallVC = viewController else {return}
            gameBallVC.modalPresentationStyle = .fullScreen
            self.present(gameBallVC, animated: true, completion: nil)
        }
    )
}
```

Upon user action, you can use **`showProfile`** function to show the widget. The **`completion`** block tells you that the **`viewController`** is ready and you can present it the way you want, for example by default if you don't explicitly specify **`modalPresentationStyle`**, it'll appear as modal. If you want it full screen, you can specify it like the above example.

| Parameter        | Type        | Required | Description                                                                                                                               |
| ---------------- | ----------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `playerUniqueId` | **String**  | Required | Unique identifier for the user in your database.Could be database ID, random string, email or anything that uniquely identifies the user. |
| `openDetail`     | **String**  | Optional | Specify if you want the widget to open on a specific view. Possible values are `details_referral`                                         |
| `hideNavigation` | **Boolean** | Optional | Set to true to stop widget navigation otherwise leave as null                                                                             |

{% hint style="info" %}
You may also check the sample project [here](https://github.com/gameballers/gameball-ios/tree/master/Example) to view full implementation.
{% endhint %}
