Objective-C
Implementing Content Coach using Objective-C uses the standard class instantiation (alloc/init) and block-based callbacks (^) to handle successful API responses and errors asynchronously.
Retrieve AI-generated topic suggestions
To retrieve AI-generated topic suggestions for a product within the review submission form, initiate the BVReviewTokensRequest class with the target Product ID, then execute the request by calling the loadWithSuccess:failure: method as shown below:
// 1. Initialize the request with the Product ID
BVReviewTokensRequest *request = [[BVReviewTokensRequest alloc] initWithProductId:@"product_id"];
// 2. Dispatch the request asynchronously
[request loadWithSuccess:^(BVReviewTokensResponse *response) {
// Success Block: Check for tokens and update the UI
NSLog(@"%@", response.tokens ?: @"");
if (!response.tokens) {
NSLog(@"No tokens returned");
return;
}
NSLog(@"%@", response.tokens);
} failure:^(NSError *error) {
// Error Block: Handle network or API failures
NSLog(@"review tokens request error: %@", error);
[expectation fulfill]; // Example: Fulfilling an async expectation in tests
}];Retrieve matched topics
Once the user types at least 5 words into the Review text box, trigger BVMatchedTokensSubmission to pass both Product ID and the live texts. Use this to evaluate which words match the topic suggestions from BVReviewTokensRequest. Now, execute the request by calling the submitWithSuccess:failure: method as shown below:
// 1. Initialize the submission query with Product ID and the text to analyze
BVMatchedTokensSubmission *testMatchedTokensQuery = [[BVMatchedTokensSubmission alloc] initWithProductId:@"product_id" withReviewText:@"review_text"];
// 2. Dispatch the submission asynchronously
[testMatchedTokensQuery submitWithSuccess:^(BVSubmissionResponse * _Nonnull response) {
// Success Block: Safely unwrap the result and tokens
BVMatchedTokensResult *result = response.result;
if (!result) {
return;
}
NSArray *tokens = result.tokens;
if (!tokens) {
return;
}
// Highlight the matched tokens in the UI
NSLog(@"%@", tokens);
} failure:^(NSArray<NSError *> * _Nonnull errors) {
// Error Block: Handle an array of potential submission errors
NSLog(@"matched tokens request error: %@", errors);
}];
