I have successfully implemented profile sharing option with PayPal iOS Sdk.
I am getting proper code once user logged in to paypal account in the app.
I have tried to get the user information with curl command I got success.
Now I want to implement 2nd and 3rd step through api call.
Below is what I have implemented for getting refresh token from PayPal server.
func getTheRefreshToken(authToken:NSString) {
print("Token \(authToken)")
let urlPath: String = "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice"
let url: NSURL = NSURL(string: urlPath)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
let basicAuthCredentials: String = "AXvaZH_Bs9**CLIENTID**0RbhP0G8Miw-y:ED_xgio**SECRET**YFwMOWLfcVGs"
let plainData = (basicAuthCredentials as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let base64String = "Basic \(plainData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)))"
request.HTTPMethod = "POST"
let params = ["grant_type":"authorization_code","redirect_uri":"urn:ietf:wg:oauth:2.0:oob", "authorization_code":authToken as String] as Dictionary<String, String>
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue(base64String, forHTTPHeaderField: "Authorization")
request.timeoutInterval = 60
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
request.HTTPShouldHandleCookies=false
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
let refreshResponse = NSString(data: data!, encoding: NSISOLatin1StringEncoding)
print("Response \(refreshResponse!)")
}
}
Every time I am getting the error with grant_type as null.
Error
Response {"error_description":"Grant type is null","error":"invalid_grant","correlation_id":"e5d4cc9c47d21","information_link":"https://developer.paypal.com/docs/api/#errors"}
A couple things here...
1. You should never have your client Secret stored on the client side for security reasons.
2. Can you attempt the call from your server using the curl commands outline here and let me know the result?
The only thing I can see from our internal logs is the same as the error or grant_type missing. Running the test from your server, using the authorization code in the response, should let us know if it's just something in your code that's getting discombobulated.
Using this code you can refresh or got new Access token on PayPal.
NSString *clientID = #"YOUR_CLIENT_ID";
NSString *secret = #"YOUR_SECRET";
NSString *authString = [NSString stringWithFormat:#"%#:%#", clientID, secret];
NSData * authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
NSString *credentials = [NSString stringWithFormat:#"Basic %#", [authData base64EncodedStringWithOptions:0]];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setHTTPAdditionalHeaders:#{ #"Accept": #"application/json", #"Accept-Language": #"en_US", #"Content-Type": #"application/x-www-form-urlencoded", #"Authorization": credentials }];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"https://api.sandbox.paypal.com/v1/oauth2/token"]];
request.HTTPMethod = #"POST";
NSString *dataString = #"grant_type=client_credentials";
NSData *theData = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:theData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSLog(#"data = %#", [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]);
}
}];
[task resume];
This will give this Response.
data = {
"access_token" = "A101.S6WF1CZIz9TcamYexl6k1mBsXhxEL1OWtotHq37UVHDrK7roty_4DweKXMhObfCP.7hNTzK62FqlDn3K9bqCjUIFmsVy";
"app_id" = "APP-80W284485P519543T";
"expires_in" = 32042;
nonce = "2016-12-26T10:24:12Z8qEQBxdSGdAbNMg2ivVmUNTUJfyFuSL30OI_W9UCgGA";
scope = "https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*";
"token_type" = Bearer;
}
Related
When I use AFNetworking to post parameters is NSMutableDictionary, this request is succeeded.
But when I used NSURLSession, the self.request!.HTTPBody must be NSData, so request failed.
How can I use NSURLSession to make request succeeded?
postDict[#"jgId"] = "1000000000";
[manager GET:SELECTDEPART parameters:postDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"-----%#", error);
}];
//use NSData
NSJSONSerialization.dataWithJSONObject(postDict, options: NSJSONWritingOptions.PrettyPrinted)
Please help me.
let params = ["jgId": "1000000000"]
let data = try? JSONSerialization.data(withJSONObject: params, options: [JSONSerialization.WritingOptions(rawValue: 0)])
var request = URLRequest(url: URL(string: "https://my-url.com")!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 30)
request.httpMethod = "POST"
request.httpBody = data
request.allHTTPHeaderFields = [:]
URLSession.shared.dataTask(with: request) { (data, response, error) in
}
You are correct, for NSURLSession you need NSData.
But you can easily convert an NSDictionary to NSData by using NSKeyedArchiver.
You are using GET method to request data from server, so your parameters is not sent by request HTTPBody data. Your params is simple past via request URL.
In question You have SELECTDEPART as base URL of request and postDict as parameter. The following code use NSURLSession to make GET request with parameters
// get request URL from base URL and params
NSURLComponents *components = [NSURLComponents componentsWithString:#"http://stackoverflow.com"]; // some thing like SELECTDEPART in your question
NSDictionary *params = #{ #"q": #"ios", #"count": #"10" }; // params of request like your postDict
NSMutableArray *queryItems = [NSMutableArray array];
for (NSString *key in params.allKeys) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:params[key]]];
}
components.queryItems = queryItems;
NSURL *url = components.URL;
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"GET";
// data task with NSURLSession
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
// parse your data here
} else {
// handle error here
}
}];
[task resume];
I have app iPhone app which will be using API's call.
I made successful call with Username/Password with NSURLSession and receiving token from server....
NSString *username = #"admin";
NSString *password = #"test";
NSString *authString = [NSString stringWithFormat:#"%#:%#",
username,
password];
// 2 - convert authString to an NSData instance
NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
// 3 - build the header string with base64 encoded data
NSString *authHeader = [NSString stringWithFormat: #"Basic %#",
[authData base64EncodedStringWithOptions:0]];
// 4 - create an NSURLSessionConfiguration instance
NSURLSessionConfiguration *sessionConfig =
[NSURLSessionConfiguration defaultSessionConfiguration];
// 5 - add custom headers, including the Authorization header
[sessionConfig setHTTPAdditionalHeaders:#{
#"Accept": #"application/json",
#"Authorization": authHeader
}
];
// 6 - create an NSURLSession instance
NSURLSession *session =
[NSURLSession sessionWithConfiguration:sessionConfig delegate:self
delegateQueue:nil];
// 7 - create an NSURLSessionDataTask instance
NSString *urlString = #"http://test.myserver.am/api/authentication/Login?username=admin&password=test";
NSURL *url = [NSURL URLWithString:urlString];
NSURLSessionDataTask *task = [session dataTaskWithURL:url
completionHandler:
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
if (error)
{
// do something with the error
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200)
{
arrTokenData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self getDomain];
} else {
// failure: do something else on failure
NSLog(#"httpResponse code: %#", [NSString stringWithFormat:#"%ld", (unsigned long)httpResponse.statusCode]);
NSLog(#"httpResponse head: %#", httpResponse.allHeaderFields);
return;
}
}];
// 8 - resume the task
[task resume];
Now I am using token received from server and making another call to get user Data......
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *authValue = [arrTokenData valueForKey:#"Token"];
//Configure session with common header fields
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{#"bearer": authValue};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSString *url = #"http://test.myserver.am/api/mobile/LookUps/getuserdata";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200)
{
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
//Process the data
}
}
}];
[task resume];
but I am receiving below status code and ....request is not getting successful....
httpResponse code: 500
httpResponse head: { "Cache-Control" = "no-cache"; "Content-Length" = 36; "Content-Type" = "application/json; charset=utf-8"; Date = "Thu, 06 Oct 2016 12:16:58 GMT"; Expires = "-1"; Pragma = "no-cache"; Server = "Microsoft-IIS/8.5"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; }
****Please note the same APIs is working fine from another(xamarin APP) platform....**
I am using Objective-C.... IOS10
is there my sending token request is not proper....?
please help me out ..... I am stuck here from yesterday...
As has already been mentioned, I'm pretty sure it should be:
NSString *authValue = [NSString stringWithFormat:#"Bearer %#",
[arrTokenData valueForKey:#"Token"]];
sessionConfiguration.HTTPAdditionalHeaders = #{#"Authorization": authValue};
With that said, a 500 error is an internal server error, not an authentication error. It seems likely that the real problem has nothing to do with authentication, and that your request itself is malformed in some way or that there is a bug in the server-side code that you're somehow tickling.
Also, your code doesn't seem to be checking to see if the token is actually present in the response, unless you're doing that elsewhere.
I would start by checking to make sure the token is actually there, and if it is, enable whatever debugging you can enable on the server and look through the logs to try to figure out what is causing the 500 error on the server side. Chances are, the fix will be obvious once you see what's actually happening on the server side.
Firstly, you should not pass username / password in the ULR in the first authentication call, since you already pass it as a header field. Parameters in the URL are not secure. Sensitive data should always be passed by using POST instead of GET method. But this is not the problem.
Try set the header field in the second call like this:
NSString *authorizationHeader = [NSString stringWithFormat: #"Bearer %#", authValue];
[sessionConfig setHTTPAdditionalHeaders:#{
#"Accept": #"application/json",
#"Authorization": authorizationHeader
}
];
Basically, the auth header field should look like this (from postman):
image
Attention: I did not test / build my code!
I want to use vimeo to host videos for a AppleTV app. I realise I will need a pro account to do exactly what I want, but at the moment just trying to do a POC.
What I need to achieve is to retrieve a url of my private video that expires after 1 hour, and I want the app to be authenticated with my credentials, not having the user have to sign in (as if the have the app, then they can view the videos).
Code that I am using is below, the constants set are:
kVimeoToken is an access token I created on vimeo for the app, and I have imported the vimeo api into my project.
-(NSString*) getVimeoUrls2 {
VIMClient *client = [[VIMClient alloc] initWithDefaultBaseURL];
AFJSONRequestSerializer *serializer= [AFJSONRequestSerializer serializer];
NSString *token = kVimeoToken;
//[serializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[serializer setValue:#"application/vnd.vimeo.*+json; version=3.2" forHTTPHeaderField:#"Accept"];
[serializer setValue:[NSString stringWithFormat:#"Bearer %#", token] forHTTPHeaderField:#"Authorization"];
client.requestSerializer = serializer;
__block NSString *str= nil;
[client requestURI:#"/me" completionBlock:^(VIMServerResponse *response, NSError *error)
{
id JSONObject = response.result;
NSLog(#"JSONObject: %#", JSONObject);
NSLog(#"Error: %#", [error localizedDescription]);
str = [[NSString alloc] initWithData:JSONObject encoding:NSUTF8StringEncoding];
}];
return str;
}
All I get back is an empty string, any idea what I am doing wrong?
If I change the access token so it is incorrect then I get back an error message {"error":"You must provide a valid authenticated access token."}, so it appears that I get authenticated ok. I have also tried some other endpoints but all of them end up with an empty string.
I have tried two separate approaches, both with the same result...i.e none, or an empty string.
I posted the question on the vimeo forums and got provided these two links:
developer.vimeo.com/api/authentication#single-user
github.com/vimeo/VIMNetworking#lightweight-use
The is the output from the log for the above code is below:
2016-01-09 08:13:26.091 tvOSShortGame[68357:91090409] server start (/me/watched/videos)
2016-01-09 08:13:26.461 tvOSShortGame[68357:91090448] server success 0.370109 sec (/me/watched/videos)
..and if I change the endpoint to /xxx (to force an error)
2016-01-09 08:07:28.826 tvOSShortGame[67829:91039056] server start (/xxx)
2016-01-09 08:07:29.003 tvOSShortGame[67829:91039045] server failure 0.177531 sec (/xxx)
2016-01-09 08:07:29.003 tvOSShortGame[67829:91039460] JSONObject: (null)
2016-01-09 08:07:29.003 tvOSShortGame[67829:91039460] Error: Request failed: not found (404)
Other endpoints get the same result, reports success but there is no JSON object returned.
Any help appreciated.
I finally got this working by using Postman which I saw on one of the Vimeo forums. It produces the correct code in multiple languages, and shows the resulting JSON so you can validate your endpoints.
Hope someone finds it useful.
//Obj C version
NSDictionary *headers = #{ #"authorization": #"Bearer MY_VIMEO_TOKEN",
#"cache-control": #"no-cache",
#"postman-token": #"MY_POSTMAN_TOKEN" };
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://api.vimeo.com/videos/116999999"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:#"GET"];
[request setAllHTTPHeaderFields:headers];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(#"%#", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(#"%#", httpResponse);
}
}];
[dataTask resume];
//Swift version
let headers = [
"authorization": "Bearer MY_VIMEO_TOKEN",
"cache-control": "no-cache",
]
var request = NSMutableURLRequest(URL: NSURL(string: "https://api.vimeo.com/videos/116999999")!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 10.0)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = headers
var str = ""
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? NSHTTPURLResponse
str = self.parseJSON(data!)
}
})
dataTask.resume()
How to post this type of format
{
"Authentication": {
"Username": "testUser#123",
"Password": "testPassword#123"
},
"FileID": "2",
"RequestType": 5
}
I know how to post this type of format to json in objective-c, here is my code
NSURL *url=[NSURL URLWithString:#"http://adservicedev.azurewebsites.net/order/json/process"];
dict = #{#"Authentication":#{#"Username":#"testUser#123",#"Password":#"testPassword#123"},#"RequestType":[NSNumber numberWithInt:4]};
if([NSJSONSerialization isValidJSONObject:dict])
{
__jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
__jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
NSLog(#"Error %#", __jsonString);
}
// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: __jsonData];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%lu", (unsigned long)[__jsonData length]] forHTTPHeaderField:#"Content-Length"];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
but am new to swift language,how to write the same in swift.Please help me out.
Thanks in advance
Well that's the way you need to implement.
Swift code
let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8888/php/index.php")!)
request.HTTPMethod = "GET"
let postString = "" // if you want to pass some string to the url you can also do it here i.e type=user now on php side you can get the value by using $_GET['type'] or $_REQUEST['type']
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{
data, response, error in
if error != nil
{
println("error=\(error)")
return
}
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var FileID : NSString = jsonResult["FileID"]! as NSString
var RequestType : NSString = jsonResult["RequestType"]! as NSString
let Auth : AnyObject = jsonResult["Authentication"]!
var Username : NSString = Auth["Username"]! as NSString
var Password : NSString = Auth["Password"]! as NSString
println("Username : \(Username)")
println("Password : \(Password)")
println("RequestType : \(RequestType)")
println("FileID : \(FileID)")
println("Auth : \(Auth)")
}
task.resume()
PHP code (index.php)
<?php
$v = '{"Authentication": {"Username": "testUser#123","Password": "testPassword#123" },"FileID": "2","RequestType": 5}';
echo $v;
?>
Final Output
How can I send an SMS message programatically from an iPhone app? I'm using Twilio right now, and can correctly set up a HTTP Request, authenticate with the server, and get a response.
There must be some misconfiguration of the HTTP Headers as I can get a response from the Twilio servers but never passes the right data through.
My current code is in a method that's called by a simple button press.
- (IBAction)sendButtonPressed:(id)sender {
NSLog(#"Button pressed.");
NSString *kYourTwillioSID = #"AC8c3...f6da3";
NSString *urlString = [NSString stringWithFormat:#"https://AC8c3...6da3:bf...0b7#api.twilio.com/2010-04-01/Accounts/%#/SMS/Messages", kYourTwillioSID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:#"+18584334333" forHTTPHeaderField:#"From"];
[request setValue:#"+13063707780" forHTTPHeaderField:#"To"];
[request setValue:#"Hello\n" forHTTPHeaderField:#"Body"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSString *response_details = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",response_details);
}
NSLog(#"Request finished %#", error);
If you are just looking to send an SMS message in iOS you can use the MFMessageComposeViewController inside of the MessageUI.framework. As you know though, this requires user-interaction.
As you had requested, you can use Twilio to send SMS directly using almost any platform. For iOS you can use the following Swift code to hit the Twilio API and send any text messages you'd like:
func tappedSendButton() {
print("Tapped button")
// Use your own details here
let twilioSID = "AC8c3...6da3"
let twilioSecret = "bf2...b0b7"
let fromNumber = "4152226666"
let toNumber = "4153338888"
let message = "Hey"
// Build the request
let request = NSMutableURLRequest(URL: NSURL(string:"https://\(twilioSID):\(twilioSecret)#api.twilio.com/2010-04-01/Accounts/\(twilioSID)/SMS/Messages")!)
request.HTTPMethod = "POST"
request.HTTPBody = "From=\(fromNumber)&To=\(toNumber)&Body=\(message)".dataUsingEncoding(NSUTF8StringEncoding)
// Build the completion block and send the request
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in
print("Finished")
if let data = data, responseDetails = NSString(data: data, encoding: NSUTF8StringEncoding) {
// Success
print("Response: \(responseDetails)")
} else {
// Failure
print("Error: \(error)")
}
}).resume()
For any further API interaction you can check out the official docs: https://www.twilio.com/docs/api/rest
Use AFNetworking to send request.
NSString *kTwilioSID = #"AC73bb270.......4d418cb8";
NSString *kTwilioSecret = #"335199.....9";
NSString *kFromNumber = #"+1......1";
NSString *kToNumber = #"+91.......8";
NSString *kMessage = #"Hi";
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};
__block NSArray* jsonArray;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:#"application/xml"];
[manager POST:urlString parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSError* err;
NSLog(#"success %#",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
jsonArray=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments
error:&err];
[_del getJsonResponsePOST:jsonArray];
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
[_del getError:[NSString stringWithFormat:#"%#",error]];
}];
It could be this:
The number +YOURNUMBER is unverified. Trial accounts cannot send messages to unverified numbers; verify +YOURNUMBER at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers.
Twilio with Swift 2.2+, Alamofire, SwiftyJSON -> answer:
import Alamofire
import SwiftyJSON
........
........
//twillio config
private static let TWILIO_ACCOUNT_SID = "A...7"
private static let TWILIO_AUTH_TOKEN = "6...5"
//end url string is .json,to get response as JSON
static let URL_TWILIO_SMS = "https://\(TWILIO_ACCOUNT_SID):\(TWILIO_AUTH_TOKEN)#api.twilio.com/2010-04-01/Accounts/\(TWILIO_ACCOUNT_SID)/SMS/Messages.json"
Alamofire.request(.POST, URL_TWILIO_SMS, parameters: ["To":"+880....6","From":"+1...9","Body":"Hellow Rafsun"])
.responseJSON { response in
if let jso = response.result.value {
let json = JSON(jso)
//Twilio response
if let twStatus = json["status"].string,twSentMessage = json["body"].string where twStatus == "queued"{
//Twilio message sent
}else{
//Twilio message not sent
}
}else if let error = response.result.error?.localizedDescription{
//parse error
}
}
An example (updated) for Xcode 8 and Swift 3.
https://www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html
We don't recommend storing your credentials client side and so the post shows you how to avoid a potential vulnerability using a server-side language of your choosing and Alamofire for HTTP requests:
#IBAction func sendData(sender: AnyObject) {
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
let parameters: Parameters = [
"To": phoneNumberField.text ?? "",
"Body": messageField.text ?? ""
]
Alamofire.request("YOUR_NGROK_URL/sms", method: .post, parameters: parameters, headers: headers).response { response in
print(response)
}
}