Rakshit Korat Rakshit Korat - 4 months ago 20
Objective-C Question

Twilio SMS Parsing using objective C

I need to integrate SMS Verification using Twilio url.

I have

ACCOUNT_SID
,
AUTH_TOKEN
and url but not able to parse using Objective C.

CODE:

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];

NSString *post = [NSString stringWithFormat:@"From='number'To=%@&Body=message %d to verify your mobile number.",txt_otp.text,[[self sms_verification_code]intValue]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.twilio.com/2010-04-01/Accounts/(SID)/SMS/Messages"]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSString *authStr = [NSString stringWithFormat:@"SID:TOKEN"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];


[[session dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];


NSLog(@"SMS verification%@",responseDictionary);
}
}] resume];

Answer

I have succeeded parsing using AFNetworking SDK.

NSString *kTwilioSID = @"sid";
            NSString *kTwilioSecret = @"token";
            NSString *kFromNumber = @"";
            NSString *kToNumber =@"";
            NSString *kMessage = [NSString stringWithFormat:@"message %d",[[self sms_verification_code] intValue]];

            NSString *urlString = [NSString
                                   stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages/",
                                   kTwilioSID, kTwilioSecret,kTwilioSID];

            NSDictionary*
            dic=@{@"From":kFromNumber,@"To":kToNumber,@"Body":kMessage};


             AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
             manager.responseSerializer=[AFHTTPResponseSerializer serializer];
             manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/xml"];
             [manager POST:urlString parameters:dic progress:nil success:^(NSURLSessionTask *task, id responseObject) {


             NSLog(@"success %@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
             completion:nil];


             } failure:^(NSURLSessionTask *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             }];
Comments