Documentation

Powered by Algolia

Alan iOS SDK

Download

Download iOS SDK framework here

Setup

  1. Copy framework to your project copy
  2. Add framework to "Embedded Binaries" (General tab for your target) embedded
  3. Add framework to "Linked Frameworks and Libraries" (General tab for your target) linked
  4. Add NSMicrophoneUsageDescription key to Info.plist of your application (must be added to microphone access) mic

Integrate into Swift

Add this Swift snippet to your view controller

Simply import AlanSDK

import AlanSDK

Define AlanButton variable

fileprivate var button: AlanButton!

Setup AlanButton in viewDidLoad()

let config = AlanConfig(key: "YOUR_KEY_FROM_ALAN_STUDIO_HERE")
self.button = AlanButton(config: config)
self.button.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.button)
let b = NSLayoutConstraint(item: self.button, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -40)
let r = NSLayoutConstraint(item: self.button, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: -20)
let w = NSLayoutConstraint(item: self.button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 64)
let h = NSLayoutConstraint(item: self.button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 64)
self.view.addConstraints([b, r, w, h])

To add configuration for Alan Studio project add dataObject as a parameter to AlanConfig.

let object: [String: Any] = ["username": user.username, "password": user.password]
let config = AlanConfig(key: "YOUR_KEY_FROM_ALAN_STUDIO_HERE", dataObject: object)
self.button = AlanButton(config: config)

Integrate into Objective C

Add this Objective C snippet to your view controller

Simply import AlanSDK

@import AlanSDK;

Define AlanButton variable

@property (nonatomic) AlanButton* button;

Setup AlanButton in viewDidLoad

AlanConfig* config = [[AlanConfig alloc] initWithKey:@"YOUR_KEY_FROM_ALAN_STUDIO_HERE"];
self.button = [[AlanButton alloc] initWithConfig:config];
[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.button];
NSLayoutConstraint* b = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-40.0];
NSLayoutConstraint* r = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-20];
NSLayoutConstraint* w = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:64.0];
NSLayoutConstraint* h = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:64.0];
[self.view addConstraints:@[b, r, w, h]];

Run application

AlanConfig

Object that describes parameters which will be provided for AlanButton.

  1. Create new AlanConfig instance with given project key:
- (instancetype)initWithKey:(NSString *)key;
Name Type Description
key NSString Project key from Alan Studio
  1. Create new AlanConfig instance with given project key and custom data object:
- (instancetype)initWithKey:(NSString *)key dataObject:(NSDictionary *)dataObject;
Name Type Description
key NSString Project key from Alan Studio
dataObject NSDictionary Given data object which will be passed to Alan Studio project

Example

AlanConfig *config = [[AlanConfig alloc] initWithKey:@"fcdb8ab082683c6b6470551acf098ef52e956eca572e1d8b807a3e2338fdd0dc/stage"];

AlanButton

This class provides a view with voice button and instance methods to communicate with Alan Studio

Create new AlanButton instance with given config object:

- (instancetype)initWithConfig:(AlanConfig *)config;
Name Type Description
config AlanConfig AlanConfig object for configuration which is described above
Example
@interface ViewController ()
@property (nonatomic) AlanButton *button;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    AlanConfig *config = [[AlanConfig alloc] initWithKey:@"fcdb8ab082683c6b6470551acf098ef52e956eca572e1d8b807a3e2338fdd0dc/stage"];
    self.button = [[AlanButton alloc] initWithConfig:config];
    [self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:self.button];

    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-20.0];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-20.0];
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:64.0];
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:64.0];
    [self.view addConstraints:@[right, bottom, width, height]];
}
@end

Play text via Alan:

- (void)playText:(NSString *)text;
Name Type Description
textString NSString Text to be played
Example
- (IBAction)didTapPlayButton:(id)sender
{
    NSString *play = @"someString";
    [self.button playText:play];
}

Send voice synchronized data event:

- (void)playCommand:(NSDictionary *)command;
Name Type Description
command NSDictionary Data event to be send
Example
- (IBAction)didTapDataButton:(id)sender
{
    NSDictionary *data = @{@"someKey": @"someValue"};
    [self.button playCommand: data];
}

Set visual state of an application:

- (void)setVisualState:(NSDictionary *)visualStateData;
Name Type Description
data NSDictionary Data with visual state description
Example
- (IBAction)didTapVisualButton:(id)sender
{
    NSDictionary *visual = @{@"someScreen": @"someValue"};
    [self.button setVisualState:visual];
}

Call a function from Alan Studio:

- (void)callProjectApi:(NSString *)method withData:(NSDictionary*)data callback:(void(^)(NSError *error, NSString *object))callback;
Name Type Description
method NSString Function name
data NSDictionary Function params
callback (void(^)(NSError *error, NSString *object)) Callback to handle result
Example
- (IBAction)didTapCallButton:(id)sender
{
    NSString *function = @"script::updateGPS";
    NSDictionary *data = @{@"lat": @"55.0000", @"lon": @"55.0000"};
    [self.button callProjectApi:function withData:data callback:^(NSError *error, NSString *object) {
        NSLog(@"result: %@", object);
    }];
}

Handle events from AlanSDK. Add observer for notification with name "kAlanSDKEventNotification":

Example
- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEvent:) name:@"kAlanSDKEventNotification" object:nil];
}

- (void)handleEvent:(NSNotification*)notification
{
    NSDictionary *userInfo = notification.userInfo;
    if( userInfo == nil )
    {
        return;
    }
    NSString *jsonString = [userInfo objectForKey:@"jsonString"];
    if( jsonString == nil )
    {
        return;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    id unwrapped = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if( error != nil )
    {
        return;
    }
    NSLog(@"unwrapped: %@", unwrapped);
}