How do I make an HTTPS request on iOS? - ios

I need to connect to an API via a GET request. I have a string that will be the URL. What is the current best practice way on iOS 7 to send that GET request over HTTPS, and store the string in a string variable?
I just need to make a simple request. There is no session data involved. No cookies. Just a simple GET from a URL and it returns me a string in the response body. Thanks!

First you need to implement connection delegate methods.
Following is the code for making http request.
NSString *strURL = [NSString stringWithFormat:#"https://your_url"];
NSURL *url = [NSURL URLWithString:strURL];
NSLog(#"URL Request : \n %#",url);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"your API Key" forHTTPHeaderField:#"Api-Key"];
[req setHTTPMethod:#"GET"];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}

Related

Submit to Google Form using objective c

I know this question was asked before. And my code is basically utilizing that and some other post here. However I am receiving an error where the page is not found in the response data.
-(void)sendToForm{
whereString = whereField.text;
nameString=name.text;
phoneString=phone.text;
emailString=email.text;
dateChoice = dateLabel.text;
serviceChoice = whatLabel.text;
//initialize new mutable data ***I DONT SEE WHAT THE POINT OF THIS IS
NSMutableData *data = [[NSMutableData alloc] init];
//THIS GIVES me an error, received data is not defined anywhere
// self.receivedData = data;
//initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString:#"https://docs.google.com/forms/d/1jeyxQYlgIUylhXnZZEcy6BC2dx_2AUB3qSNuCqFHUDU/viewform"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:#"POST"];
//initialize a post data
NSString *postData = [NSString stringWithFormat:#"entry.1550955212=%#&entry.935955213=%#&entry.1547576233=%#&entry.202631922=%#&entry.1387567225=%#&entry.1031369018",serviceChoice,dateChoice,whereString,emailString,phoneString,nameString]; //set request content type we MUST set this value.
[request setValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
//set post data of request
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//start the connection
[connection start];
}
I have some functions checking the connection and response and they all work. but in the response data it returns as Page Not Found
Needed to be formResponse instead of viewForm

I am implementing Oracle UCM in iPad application is there any web services for login authentication?

I am implementing Oracle UCM in iPad application is there any web services for login authentication. now i am using the j_security_check?.. i am unable to handle j_security_check response.
NSURL *url = [NSURL URLWithString:#"http://serveripaddress/cs/idcplg/j_security_check?"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:#"POST"];
//initialize a post data
NSString *postData =[NSString stringWithFormat:#"j_username=%#&j_password=%#",usernameTF.text,passwordTF.text];
NSLog(#"userDetails : %#" ,postData);
//set request content type we MUST set this value.
[request setValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
//set post data of request
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *urlconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = urlconnection;
You can use the iPad app for Webcenter Content rather than building your own app for the same . Please check the following links for app specific details :
http://docs.oracle.com/cd/E29542_01/doc.1111/e26695/mobile_integration.htm
https://itunes.apple.com/us/app/oracle-webcenter-content/id701199944?mt=8
Hope this helps .
Thanks,
Srinath

POST request for login page

I creating the app for my site and have a problem with signin. I have login page with login and pass fields and the "Login" button.
I tried to write post request (You can see it below) for sign in, but it doesn't work.
Please, give me some solutions.
I've been coding in Obj-C just a few weeks and I don't now many things.
NSString *login = loginField.text;
NSString *password = passField.text;
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://mysite",login, password]]; //Here you place your URL Link
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
if (connection) {
NSLog(#"connection successful");
}
else {
NSLog(#"Failed");
}
NSURLRequest HTTPMethod is by default "GET" and you need to set it to "POST" in your code.
My example shows a NSMutableURLRequest because I have had this code tested in an application. You should be able to do the same with NSURLRequest if you prefer that.
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
[req setURL:[NSURL URLWithString:theUrl]];
[req setHTTPMethod:#"POST"];
Hope this helps.

ios invalid_request while calling for autherization token using oauth

I'm using google api to do google lattitude operations in my application. I have done till autherizing the user using credentials. Now i'm trying to generate accesstoken using the autherization_code generated from the initial request. But it is throughing invalid_request as error in the didReceiveData function. can some one of you please suggest me to solve my problem. Oauth request format is suppose to be like follows.
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
my ios code to perform the above service calll is follows
NSString * urlString = [NSString stringWithFormat:#"https://accounts.google.com/o/oauth2/token?code=%#&client_id=363756608022.apps.googleusercontent.com&client_secret=dury2bxloSVQ1sdJjg9MKO9s&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code",[userDefaults valueForKey:#"accessTokenResponse"]];
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
[request addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"POST"];
NSLog(#"sendng req : %#",request);
NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
use asihttpheader to form header request. [request setpostvalue" forkey :]. this solved my problem

Send JSON data to an URL

I have a certain URL "http://dev.mycompany.com" and I need to send some data (in JSON format) to it and get the response.
I can't find my way through the hugeload of documentation and related questions here on SO. I've managed to get the data (without sending any data) with NSURLConnection and it works good, the code I have so far is nearly the same as on https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html and so I don't see any point of posting the code I have.
I can't append some string to my URL as the data I need to send is JSON data. I am sorry if I sound like a noob, but I have very little experience in Obj-C and server communication.
You should send them as parameter to a post NSUrlRequest
As following:
NSURL *aUrl = [NSURL URLWithString:#"http://dev.mycompany.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSString *postString = #"yourVarialbes=yourvalues";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
For sending JSON
Pelase read
How to send json data in the Http request using NSURLRequest

Resources