Beam Web Apps

There are several platforms available which support the launching of web apps. A web app is typically run on a temporary basis in a full-screen browser instance. 

Web App IDs

Both webOS and Chromecast platforms require a web app ID for API calls to launch & communicate with web apps. This web app ID is translated it into your web app's URL on web app launch.

For information on creating a web app ID for webOS, please visit the registration site.

To learn how to register for a Chromecast web app ID, visit Google's app ID registration site.

Launch web app with identifier

Connect SDK currently supports web app launching on webOS and Chromecast devices, which both translate a web app identifier into your web app's URL.

NSString *webAppId;

if ([_device serviceWithName:@"webOS TV"])
    webAppId = @"5G7328DE";
else if ([_device serviceWithName:@"Chromecast"])
    webAppId = @"3E5106AB";

if (!webAppId)
    return;

[_device.webAppLauncher launchWebApp:webAppId success:^(WebAppSession *webAppSession) {
    NSLog(@"web app launch success");
} failure:^(NSError *error) {
    NSLog(@"web app launch error: %@", error.localizedDescription);
}];

Communicate with web app

Bi-directional communication with your web app is made extremely simple. Data can be sent and received strongly-typed as a string or a keyed set of values (JSON object).

WebAppSession *_webAppSession;

[_device.webAppLauncher launchWebApp:webAppId success:^(WebAppSession *webAppSession) {
    NSLog(@"web app launch success");

    _webAppSession = webAppSession;
    _webAppSession.delegate = self;

    [_webAppSession connectWithSuccess:^(id responseObject) {
        NSLog(@"web app connect success");
    } failure:^(NSError *error) {
        NSLog(@"web app connect error: %@", error.localizedDescription);
    }];
} failure:^(NSError *error) {
    NSLog(@"web app launch error: %@", error.localizedDescription);
}];

After successfully establishing a connection, you can send messages to your web app.

[_webAppSession sendText:@"This is a test message" success:nil failure:nil];

You can also send an NSDictionary which will be received by the web app as a JSON object.

NSDictionary *message = @{
    @"someParameter" : @"someValue",
    @"anArray": @[
        @"array value 1",
        @"array value 2",
        @"array value 3"
    ],
    @"anotherObject" : @{
        @"anotherParameter" : @"anotherValue"
    }
};

[_webAppSession sendJSON:message success:nil failure:nil];

WebAppSessionDelegate allows you to receive messages from your web app.

- (void) webAppSession:(WebAppSession *)webAppSession didReceiveMessage:(id)message {
    // message may be either an NSString or an NSDictionary, depending on what was sent from the web app
    NSLog(@"Received message from web app %@", message);
}

Beam media to web app

A common use case for web apps is the playback and control of media files. Connect SDK provides capabilities for directly playing/controlling media on a WebAppSession, provided that web app has integrated the Connect SDK JavaScript Bridge.

Rather than calling playMedia on your device's mediaPlayer, webAppSession provides its own mediaPlayer. After media has been beamed into the web app, the control is just like any other media session.

[webAppSession.mediaPlayer playMedia:mediaURL
                             iconURL:iconURL
                               title:title
                         description:description
                            mimeType:mimeType
                             success:^(LaunchSession *launchSession, id<MediaControl> mediaControl) {
                                 NSLog(@"play video success");

                                 // save these object references to control media playback
                                 _launchSession = launchSession;
                                 _mediaControl = mediaControl;

                                 // you will want to enable your media control UI elements here
                             }
                             failure:^(NSError *error) {
                                 NSLog(@"play video failure: %@", error.localizedDescription);
                             }];