Sample code for 30+ languages & platforms
Objective-C

MedTunnel: Send Message Text Only

See more MedTunnel Examples

Demonstrates the MedTunnel SendMessage method to send a message to one or more recipients.

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoHttpRequest.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>

BOOL success = NO;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoHttp *http = [[CkoHttp alloc] init];

// Implements the following CURL command:

// curl https://server.medtunnel.com/MedTunnelMsg/api/Message/SendMessage -X POST -k 
//         -F "ApplicationId=yourApplicationId" -F "LocationId=yourLocationId" 
//         -F "MedTunnelId=yourMedTunnelId" -F "MedTunnelPassword=yourMedTunnelPassword" 
//         -F "To=recipientsMedTunnelId" 
//         -F "Body=Test of SendMessage"

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.HttpVerb = @"POST";
req.Path = @"/MedTunnelMsg/api/Message/SendMessage";
req.ContentType = @"multipart/form-data";
[req AddParam: @"ApplicationId" value: @"yourApplicationId"];
[req AddParam: @"LocationId" value: @"yourLocationId"];
[req AddParam: @"MedTunnelId" value: @"yourMedTunnelId"];
[req AddParam: @"MedTunnelPassword" value: @"yourMedTunnelPassword"];
[req AddParam: @"To" value: @"recipientsMedTunnelId"];
[req AddParam: @"Body" value: @"Test"];

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpSReq: @"server.medtunnel.com" port: [NSNumber numberWithInt: 443] ssl: YES request: req response: resp];
if (success == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;

NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);

int respStatusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",resp.Header);
    NSLog(@"%@",@"Failed.");
    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "ReturnCode": 1,
//   "ReturnCodeText": "Success",
//   "Data": "{\"Id\":989432,\"FromUserId\":36990,\"FromMailBoxId\":36965, ... \"SendGlobalNotifications\":false}"
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

int ReturnCode = [[jResp IntOf: @"ReturnCode"] intValue];
NSString *ReturnCodeText = [jResp StringOf: @"ReturnCodeText"];
NSString *Data = [jResp StringOf: @"Data"];

// Load the Data into another JSON object and parse..
CkoJsonObject *jsonData = [[CkoJsonObject alloc] init];
[jsonData Load: Data];
jsonData.EmitCompact = NO;
NSLog(@"%@",[jsonData Emit]);

// {
//   "Id": 989436,
//   "FromUserId": 36990,
//   "FromMailBoxId": 36965,
//   "FromUserType": 0,
//   "FromUserName": "...",
//   "FromUserFullName": "...",
//   "FromUserAccountName": "...",
//   "FromUserAccountTitle": "...",
//   "ToUserId": 36990,
//   "ToUserType": 1,
//   "ToUserMailboxId": "36965",
//   "ToUserName": "...",
//   "ToUserFullName": "...",
//   "EmailAddress": "",
//   "Password": "",
//   "Subject": "",
//   "PatientMedTunnelId": "",
//   "Body": "Test",
//   "DateReceived": "4/29/2021 2:29:18 PM",
//   "DisplayDateReceived": "04/29/2021  2:29 PM",
//   "ViewCount": 0,
//   "ViewedOn": "",
//   "AttachmentCount": 0,
//   "AttachmentNames": [
//   ],
//   "AllRecipients": [
//     {
//       "Id": 989436,
//       "UserName": "...",
//       "AccountId": 0,
//       "AccountName": "...",
//       "AccountTitle": "",
//       "FirstName": "...",
//       "LastName": "...",
//       "EmailAddress": "",
//       "LastSentOn": "",
//       "SendCount": 0,
//       "IsFavorite": false
//     }
//   ],
//   "Status": 1,
//   "ParentMessageId": 989436,
//   "DistributionListId": 0,
//   "DistributionListName": "",
//   "BodyHistory": "",
//   "ReadReceiptCallbackUrl": null,
//   "SendGlobalNotifications": false
// }
// 

NSString *UserName = 0;
int AccountId;
NSString *AccountName = 0;
NSString *AccountTitle = 0;
NSString *FirstName = 0;
NSString *LastName = 0;
NSString *LastSentOn = 0;
int SendCount;
BOOL IsFavorite;

int Id = [[jsonData IntOf: @"Id"] intValue];
int FromUserId = [[jsonData IntOf: @"FromUserId"] intValue];
int FromMailBoxId = [[jsonData IntOf: @"FromMailBoxId"] intValue];
int FromUserType = [[jsonData IntOf: @"FromUserType"] intValue];
NSString *FromUserName = [jsonData StringOf: @"FromUserName"];
NSString *FromUserFullName = [jsonData StringOf: @"FromUserFullName"];
NSString *FromUserAccountName = [jsonData StringOf: @"FromUserAccountName"];
NSString *FromUserAccountTitle = [jsonData StringOf: @"FromUserAccountTitle"];
int ToUserId = [[jsonData IntOf: @"ToUserId"] intValue];
int ToUserType = [[jsonData IntOf: @"ToUserType"] intValue];
NSString *ToUserMailboxId = [jsonData StringOf: @"ToUserMailboxId"];
NSString *ToUserName = [jsonData StringOf: @"ToUserName"];
NSString *ToUserFullName = [jsonData StringOf: @"ToUserFullName"];
NSString *EmailAddress = [jsonData StringOf: @"EmailAddress"];
NSString *Password = [jsonData StringOf: @"Password"];
NSString *Subject = [jsonData StringOf: @"Subject"];
NSString *PatientMedTunnelId = [jsonData StringOf: @"PatientMedTunnelId"];
NSString *Body = [jsonData StringOf: @"Body"];
NSString *DateReceived = [jsonData StringOf: @"DateReceived"];
NSString *DisplayDateReceived = [jsonData StringOf: @"DisplayDateReceived"];
int ViewCount = [[jsonData IntOf: @"ViewCount"] intValue];
NSString *ViewedOn = [jsonData StringOf: @"ViewedOn"];
int AttachmentCount = [[jsonData IntOf: @"AttachmentCount"] intValue];
int Status = [[jsonData IntOf: @"Status"] intValue];
int ParentMessageId = [[jsonData IntOf: @"ParentMessageId"] intValue];
int DistributionListId = [[jsonData IntOf: @"DistributionListId"] intValue];
NSString *DistributionListName = [jsonData StringOf: @"DistributionListName"];
NSString *BodyHistory = [jsonData StringOf: @"BodyHistory"];
NSString *ReadReceiptCallbackUrl = [jsonData StringOf: @"ReadReceiptCallbackUrl"];
BOOL SendGlobalNotifications = [jsonData BoolOf: @"SendGlobalNotifications"];
int i = 0;
int count_i = [[jsonData SizeOfArray: @"AttachmentNames"] intValue];
while (i < count_i) {
    jsonData.I = [NSNumber numberWithInt: i];
    i = i + 1;
}

i = 0;
count_i = [[jsonData SizeOfArray: @"AllRecipients"] intValue];
while (i < count_i) {
    jsonData.I = [NSNumber numberWithInt: i];
    Id = [[jsonData IntOf: @"AllRecipients[i].Id"] intValue];
    UserName = [jsonData StringOf: @"AllRecipients[i].UserName"];
    AccountId = [[jsonData IntOf: @"AllRecipients[i].AccountId"] intValue];
    AccountName = [jsonData StringOf: @"AllRecipients[i].AccountName"];
    AccountTitle = [jsonData StringOf: @"AllRecipients[i].AccountTitle"];
    FirstName = [jsonData StringOf: @"AllRecipients[i].FirstName"];
    LastName = [jsonData StringOf: @"AllRecipients[i].LastName"];
    EmailAddress = [jsonData StringOf: @"AllRecipients[i].EmailAddress"];
    LastSentOn = [jsonData StringOf: @"AllRecipients[i].LastSentOn"];
    SendCount = [[jsonData IntOf: @"AllRecipients[i].SendCount"] intValue];
    IsFavorite = [jsonData BoolOf: @"AllRecipients[i].IsFavorite"];
    i = i + 1;
}