PhoneGap/ChildBrowser - POST App Form to ChildBrowser - ios

I am building an iPhone App using PhoneGap. I am using ChildBrowser plugin.
If I have a form in the App with username/pass, is there anyway I can post those information to an URL like www.mydomain.com/login.php and open it in ChildBrowser?
Please let me know.
Thanks.

You can certainly pass GET parameters to childBrowser.showWebPage().
You could intercept the submit of your form in your app and gather up the field names and values passed (assuming this is a login form, so say username and password) and pass them as parameters to the URL opened in ChildBrowser.
childBrowser.showWebPage('https://www.mydomain.com/login.php?username='+username+'&password='+password)
This is a rather unsophisticated version for demonstration purposes. It wouldn't take much to make it better.
The catch is of course that it is being sent via GET. If your login form is expecting POST and you can't change that, you could have a problem.
Edit:
If POST is your only option, perhaps you would be better off using AJAX to post to the form instead of using child browser.

I was looking for a solution, and ended developing it. This doesn't come out of the box in childbrowser for iPhone.
So, here the code that I added to the plugin.
FYI, data should be a string with the format: arg1=value1&arg2=value2&foo=bar
ChildbrowserViewController
I've added the following method there:
- (void)postURL:(NSString*)url data:(NSString*)data{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:url]];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [data dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
webView.hidden = NO;}
ChildbrowserCommand
I've added the following code in the showWebPage method:
NSString* method = [ options objectForKey:#"method"];
if ([method isEqualToString:#"POST"]){
[childBrowser postURL:url data:[options objectForKey:#"data"]];
}else{
[childBrowser loadURL:url];
}
HTH!
Milton.

Related

iOS dropbox sdk / core api - listing public folder contents without authorization

I've tried checking on stackoverflow and on the api docs, but couldn't find any info pertaining to this particular question.
What I'm trying to do is getting the public folder contents of a particular dropbox account (like the /metadata function of the api), without the need to authorize/link to dropbox.
I know "/metadata/link" allows you to get metadata of a particular link without user authorization, but I couldn't find a way to get regular metadata and file listing...
Actually it doesn't even need to be a public folder (as I've read recently that dropbox discourages developers regarding relying on public folders for apps), it can just as well be a regular folder through its shared link or anything of the kind...
I'm using api v1 (but can consider changing to v2 if this is impossible with v1), although I'm not even sure if this is at all possible.
Thank you!
For future reference, if anyone needs this as well, here's how I did it:
NSString *parameters = [NSString stringWithFormat:#"?link=%#&client_id=%#&client_secret=%#",fileUrl, appKey, appSecret];
NSString *sharedPath = [NSString stringWithFormat:#"%#/%#",#"https://api.dropbox.com/1/metadata/link", parameters];
NSURL *url = [NSURL URLWithString:sharedPath];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
theRequest.timeoutInterval = 5.0;
theRequest.HTTPMethod = #"POST";
NSData * resData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
Use NSJSONSerialization to parse resData and refer to the api documentation for json fields, and that's it.
For the sake of simplicity showing it here I used synchronous request, but it can just as easily be done with asynchronous connection delegates.

iOS url multiple statements

I have recently discovered that there is no way for me to access mysql with the pre defined iOS libraries.
So I have started making an app that has a web viewer (hidden) and will visit a url to pass the information to the database.
But I am having trouble making it so it visits a url that contains multiple variables from my objective c code my example is below.
NSURL *url = [[NSURL alloc] initWithString:#"http://webpageurl.com/updatelocation.php?friendsphonenumber=%#&yourphonenumber=%#&message=%#", _friendsPhoneNumber.text, _yourPhoneNumber.text, _message.text];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.WebView loadRequest:request];
This is what the url should come out looking like
http://webpageurl.com/updatelocation.php?friendsphonenumber=1112223333&yourphonenumber=1112223333&message=hioehfoi
But my code is returning a error and I am a little unsure why any and all help is greatly appreciated.

'ASIHTTPRequest' is not responding to setPostValue, need to pass email id as parameter

i am new to iOS development , recently i'm working with 'ASIHTTPRequest' library. i have downloaded it's example from Here. it's working fine. I need to send a parameter to my web service as 'email' and also authentication needed.
i tried following
[request setPostValue:#"abcd#gmail.com" forKey:#"email"]; based on this Reference
but it's give me warning that Instance method -setPostValue:forKey() not found. How can i pass email id as parameter to web service? For your reference i use web service to reset password when user forgot it.
EDIT :
Now , i need to convert following code into ASIFormDataRequest from ASIHTTPRequest with email id parameter and authentication. can you help me now ?
`
[self setRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:#"http://abc.foo.com/api/forgot_password/"]]];
[request setUseKeychainPersistence:[useKeychain isOn]];
[request setDelegate:self];
[request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[request setDidFinishSelector:#selector(topSecretFetchComplete:)];
[request setDidFailSelector:#selector(topSecretFetchFailed:)];
[request startAsynchronous];
`
That's because ASIHTTPRequest doesn't include a -setPostValue:forKey: method. ASIFormDataRequest, on the other hand, does.
It sounds like you're using a pointer of type ASIHTTPRequest* to send a message to an instance of ASIFormDataRequest. That's okay if the pointer really points to a form data request, ASIFormDataRequest being a subclass of ASIHTTPRequest, but if you're sure enough about the type of the object that you can send it a message specific to it's type, you also know enough to either use the more specific type in the first place or use a type cast to let the compiler know that it doesn't need to complain.

Need to authenticate a user using JSON on my server with iOS SDK

I've only been working on an iOS app now for a couple weeks so bear with me. I want my app's first screen to be a login with email address and password. I got the view set up however I'm just not sure what's the best way to store the authentication.
The authentication itself will be handled server side. When the user enters their email and password, it will run connect to my server and return JSON if it failed or not. If it is successful I will return whatever I need.
The question is, what information should I store and where do I store it? Should I store their user id and their password as a md5 string? Then every time I make a call to the server I can pass their user id and md5 string to verify if they have access. Does that make sense to do?
Yes, you should store the credentials on the device. That way, when the session expires on the server, the user can be immediately re-authenticated. (Keep in mind, this is less secure than forcing the user to log in each time, because if the user looses his phone, anyone who finds it could have access to his account.)
Just store the email address and password in an SQLite table, a plist, a text file, or whatever you want. It's probably best to encrypt the password, even though no other applications will be able to access the file you store it in.
Edit:
Btw, you don't necessarily have to pass the credentials to the server with every request. I don't know what you are using on the server side, but you can set it up to use sessions, so they user will stay longed in for a while before having to re-authenticate. Here is some code I have used to send credentials:
NSURLConnection *connection;
NSString *url = [NSString stringWithFormat:#"http://example.com/service/authenticate.ashx"];
NSString *username = [self.usernameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *password = [self.passwordField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableString* requestURL = [[NSMutableString alloc] initWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:#"username=%#&password=%#", username, password] dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
That NSURLConnection object will manage the cookie and keep the user logged in, so you can use it to keep sending requests to the server until the session expires.

Couldn't get Google custom search results using JSON

I'm developing an application that makes use of Google custom search api as a feature. I added a new custom search and included some sites to search, I was able to do a search and get the results I need, but all of a sudden it stopped returning results. It returns an empty array instead. I didn't alter anything, but it looks like I have a limited number of queries, am I right?
Here's the line of code responsible for query a search word:
NSString *search = [NSString stringWithFormat:#"https://www.googleapis.com/customsearch/v1?start=1&ie=utf8&key=XXXXXX&cx=XXXXX&q=SEARHWORD&alt=json"];
url = [NSURL URLWithString:[search stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *searchConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
Then the normal steps of parsing the results.
if it was working fine, and then stopped all of a sudden, you may have hit the limit.
You can double check by copy-pasting
https://www.googleapis.com/customsearch/v1?start=1&ie=utf8&key=XXXXXX&cx=XXXXX&q=SEARHWORD&alt=json
in a browser and seeing the response

Resources