Skip to main content
Version: 1.3.x

AIPlayer Set up

Full setup process (4 steps)
  • Step 1: Authenticate SDK user. (Failure is returned in the callback if not a valid userKey.)
  • Step 2: Get default AI List after authentication. (Failure occurs if authentication is not performed.)
  • Step 3: Create AIPlayer.
  • Step 4: Remove AIPlayer Object.

Step 1: Authenticate SDK user

In order to use AIPlayer, it is necessary to check if the user is authenticated. The first step for user authentication is to obtain userkey. Userkey is a unique string generated by DeepBrain AI and should never be disclosed.

After setting the App ID and userKey, call generateToken() method to finish authentication.

If your token expires, you need to call this API again and re-authenticate.

AIPlayer.setAppId("app identifier")
AIPlayer.setUserKey("unique userKey")
AIPlayer.generateToken { (error) in
if let error = error { // error : succeeded = nil, failed = error
// TODO: error handling here
return
}
}

Step 2 : Get default AI List after authentication

Once authentication completes, AIPlayer object contains authentication result data. By calling getAiList(), all the available AI List will be returned. If you don't have any AI model permissions, or if you call this function before authentication, 0 is returned.

AIPlayer authenticates and returns the authentication result. To check which AIs can be used after authentication, call the following method after authentication succeeds. If there is no AI available or if this value is checked before authentication, 0 is returned.

AIPlayer.getAiList { [weak self] (res, error) in
/* res : {"ai": [
{aiName: "", aiDisplayName: "", language: ""}
]}
error : succeeded = nil, failed = error
*/
guard let res == res else {
if let error = error {
print(error)
}
return
}

if let list = res["ai"] as? Array<[String: Any]> {
...
}
}

You can get this list anytime by calling AIPlayer.aiList() method.

let aiList = AIPlayer.aiList()

Step 3: Create AIPlayer

After checking available AIs in step 2, initialize an AIPlayer class through the create method. You would need to prepare aiName as a parameter to the create method in order to initialize AIPlayer instance. Once AIPlayer class is created, you can place AIPlayer on UIView. You can keep track of AI state through the class's delegate property.

Using the AI name as a parameter, call the create function of the AIPlayer class to create an AI object.

By registered delegate to the created AIPlayer object, the AI states can be called back.

class CustomViewController: UIViewController, AIPlayerCallback {

...

AIPlayer.create(name: "ai_name") { [weak self] (aiPlayer, error) in
guard let aiPlayer = aiPlayer else {
...
return
}

if let aiPlayer = aiPlayer {
aiPlayer.delegate = self
self?.view?.addSubview(aiPlayer)
}
}

...

}

Step 4: Remove AIPlayer Object

AIPlayer's releasePlayer method must be called to remove all the used resources.

Then, remove AIPlayer from UIView.

aiPlayer.releasePlayer()
aiPlayer.removeFromSuperview()