RootViewController Class
A root view controller displays the first custom view that your app presents. In Objective-C apps, this view is the one at the bottom, or root, of your view stack. For the Mobile SDK Objective-C template project, Mobile SDK appropriately names its root view controller class RootViewController. This class sets up a mechanism for your app’s interactions with the Salesforce REST API. Regardless of how you define your root view controller, you can reuse the template app’s code for retrieving Salesforce data through REST APIs.
RootViewController Design
- Use Salesforce REST APIs to query Salesforce data
- Display the Salesforce data in a table
Swift Implementation
Beginning in Mobile SDK 9.0, the Swift template app no longer defines a RootViewController class. Instead, it uses SwiftUI views and models with Combine extensions to accomplish the same goals, as described in Native Swift Template.
Objective-C Implementation
1- (void)viewDidLoad
2{
3 [super viewDidLoad];
4 self.title = @"Mobile SDK Sample App";
5
6 SFRestRequest *request =
7 [[SFRestAPI sharedInstance]
8 requestForQuery:@"SELECT Name FROM Contact LIMIT 10"
9 apiVersion:kSFRestDefaultAPIVersion];
10 [[SFRestAPI sharedInstance] send:request delegate:self];
11}After calling the superclass method and setting the view’s title, the app creates and sends its REST request. Notice that the requestForQuery and send:delegate: messages are sent to a singleton shared instance of the SFRestAPI class. You use this singleton object for all REST requests.
1[[SFRestAPI sharedInstance] send:request delegate:self];1- (void)request:(SFRestRequest *)request
2 didSucceed:(id)jsonResponse
3 rawResponse:(NSURLResponse *)rawResponse {
4
5 NSArray *records = jsonResponse[@"records"];
6 [SFLogger d:[self class]
7 format:@"request:didLoadResponse: #records: %lu",
8 (unsigned long)records.count];
9 self.dataRows = records;
10 dispatch_async(dispatch_get_main_queue(), ^{
11 [self.tableView reloadData];
12 });
13}As the use of the id data type suggests, this code handles JSON responses in generic Objective-C terms. It addresses the jsonResponse object as an instance of NSDictionary and treats its records as an NSArray object. Because RootViewController implements UITableViewController, it’s simple to populate the table in the view with extracted records.