Objective-C iOS - SpamBlock API and Forms Integration
Learn how to integrate SpamBlock API and Forms solution with Objective-C iOS apps. Complete example with NSURLSession for spam checking before form submission.
Overview
This example shows how to integrate both the SpamBlock API and SpamBlock Forms solution with Objective-C iOS applications. Note: The SpamBlock Pixel is web-only and cannot run in iOS apps. However, you can use the SpamBlock API directly to check submissions for spam, and submit to SpamBlock Forms endpoints.
Note: While you can use any backend endpoint, this example specifically demonstrates using both the SpamBlock API and SpamBlock Forms together. The SpamBlock Forms endpoint (
https://api.spamblock.io/f/{form_id}) provides a complete hosted form solution. Learn more about Forms or use your own backend endpoint.
Step 1: Create API Helper Class
#import <Foundation/Foundation.h>
@interface SpamBlockAPI : NSObject
+ (void)checkSpamWithName:(NSString *)name
email:(NSString *)email
message:(NSString *)message
completion:(void (^)(BOOL allowed, NSError *error))completion;
+ (void)submitFormWithName:(NSString *)name
email:(NSString *)email
message:(NSString *)message
completion:(void (^)(BOOL success, NSError *error))completion;
@end
Step 2: Implement API Helper
#import "SpamBlockAPI.h"
@implementation SpamBlockAPI
+ (void)checkSpamWithName:(NSString *)name
email:(NSString *)email
message:(NSString *)message
completion:(void (^)(BOOL allowed, NSError *error))completion {
NSURL *url = [NSURL URLWithString:@"https://api.spamblock.io/v1/check"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *bodyDict = @{
@"name": name,
@"email": email,
@"message": message
};
NSError *jsonError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bodyDict
options:0
error:&jsonError];
if (jsonError) {
completion(NO, jsonError);
return;
}
[request setHTTPBody:jsonData];
NSURLSessionDataTask *task = [[NSURLSession sharedSession]
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, error);
});
return;
}
NSError *parseError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&parseError];
if (parseError) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, parseError);
});
return;
}
BOOL allowed = [json[@"allowed"] boolValue];
dispatch_async(dispatch_get_main_queue(), ^{
completion(allowed, nil);
});
}];
[task resume];
}
+ (void)submitFormWithName:(NSString *)name
email:(NSString *)email
message:(NSString *)message
completion:(void (^)(BOOL success, NSError *error))completion {
NSURL *url = [NSURL URLWithString:@"https://api.spamblock.io/f/{form_id}"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *bodyDict = @{
@"name": name,
@"email": email,
@"message": message
};
NSError *jsonError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bodyDict
options:0
error:&jsonError];
if (jsonError) {
completion(NO, jsonError);
return;
}
[request setHTTPBody:jsonData];
NSURLSessionDataTask *task = [[NSURLSession sharedSession]
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, error);
});
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
BOOL success = httpResponse.statusCode == 200;
dispatch_async(dispatch_get_main_queue(), ^{
completion(success, nil);
});
}];
[task resume];
}
@end
Step 3: Use in View Controller
#import "ViewController.h"
#import "SpamBlockAPI.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet UITextView *messageField;
@end
@implementation ViewController
- (IBAction)submitButtonTapped:(id)sender {
NSString *name = self.nameField.text;
NSString *email = self.emailField.text;
NSString *message = self.messageField.text;
if (!name.length || !email.length || !message.length) {
[self showAlert:@"Error" message:@"Please fill in all fields"];
return;
}
// First, check for spam
[SpamBlockAPI checkSpamWithName:name
email:email
message:message
completion:^(BOOL allowed, NSError *error) {
if (error) {
[self showAlert:@"Error" message:@"Error checking spam"];
return;
}
if (!allowed) {
[self showAlert:@"Error" message:@"Submission blocked as spam"];
return;
}
// If allowed, submit the form
[SpamBlockAPI submitFormWithName:name
email:email
message:message
completion:^(BOOL success, NSError *error) {
if (error) {
[self showAlert:@"Error" message:@"Error submitting form"];
return;
}
if (success) {
[self showAlert:@"Success" message:@"Submission successful!"];
self.nameField.text = @"";
self.emailField.text = @"";
self.messageField.text = @"";
}
}];
}];
}
- (void)showAlert:(NSString *)title message:(NSString *)message {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
}
@end
What This Example Shows
- How to integrate SpamBlock API with Objective-C iOS apps
- Using NSURLSession for HTTP requests
- Spam checking before form submission
- Completion handlers for async operations
When to Use It
Use this approach when:
- You're building an Objective-C iOS application
- You're using NSURLSession for API calls
- You need spam protection in native apps
- You want to check submissions before sending to backend
How it Works
- User fills out form in iOS app
- User submits form
- App calls SpamBlock API to check submission
- If spam: submission blocked, user notified
- If allowed: submission sent to your backend
- Success/error feedback shown to user
Important Notes
- Pixel is web-only: The SpamBlock Pixel cannot run in iOS apps
- API integration: Use the SpamBlock API directly (
/v1/checkendpoint) - Main thread: Always dispatch UI updates to main queue
- API key: You may need an API key for production use
Common Mistakes
- Trying to use Pixel - Pixel is JavaScript and won't work in iOS
- Not checking spam first - Always check with SpamBlock API before submitting
- Main thread operations - Always use
dispatch_async(dispatch_get_main_queue())for UI updates - Missing error handling - Always handle both success and error cases
- JSON serialization errors - Check for JSON errors before making requests
- Memory management - Be careful with retain cycles in completion blocks
Learn More
- Pixel Getting Started - Learn about the web Pixel (web-only)
- Forms Getting Started - Explore our hosted form solution