Call specific web service on -didSelectRowAtIndexPath - iphone

I am implementing the application which consumes webservice (first authentication and then calling different webservices with provided token as a header field in URL)
After the authentication phase, I am showing the Home page to the user.
On Home page i am showing UITableView (with 9 cells on it.)
If those were the buttons I would write separate 9 - "onButtonClick" functions to call 9 different webservices, accordingly.
What can be best implementation (or flow), for implementing this structure.
Let me know if you want more details.
Thank you.

You could keep the URLs for the services in an array attached to the table view's view controller (or delegate really) and then use the selected row as an index into the array and call the service.

No need to write nine methods separately
you can use the JASON COCO way as he said store the URL in an array.
when you will click on a particular row. you should pass that URL to Web services method
suppose you have
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* urlString=[self.URLArray objectAtIndex:indexPath.row];
[self callWebSerciceWithUrl:urlString];
}
-(void)callWebSerciceWithUrl:(NSString* )Url{
//Use that URL String for calling web services...
//You Should write your Remaining web service Code here...
}

Related

Lazy Loading for multiple UITableView in one view

I am working on iPad app. And I am using 2 tables in one view and images urls are coming from
web services. I am using Apple provided lazy loading for both tables, and there are 2 web-service called for showing the two tables data, one is calling in ViewDidLoad() and second is calling in callback method of first web service. And I am facing problem in this method.
- (void)appImageDidLoad:(NSIndexPath *)indexPath withDataReceived:(NSData*)receivedData
The values for indexpath.row values are changing every time , some time it gives correct value and some time not.
Can anybody please help me for that.
Thanks in advance.
you might have to pass in which tableview also.. you kno how it is passing indexPath to that method.. modify it to pass tableview.. and inside the method.. check which tableView and then get the right data source array for appropriate tableview..

How to map UITableView cell to a remote database backend?

I'm making a simple To-Do list app for the iPhone with a rails backend.
So far, I can pull all the tasks in a To-Do list from the web app, parse the JSON, and insert it into the UITableView.
The json response from rails:
[
{"id":1,"name":"Buy bread"},
{"id":8,"name":"Get gas"},
{"id":14,"name":"Call John"}
]
In the UITableView, a row for each task, the textLabel of the cell is the name of the task:
The problem I'm having now is how to associate the cell with the record id of the task.
Two tasks can have the same name, so I can't just get the id from the hash based only on the name.
I need the record id, so I can post back to the rails app, after the user edits the UItableView (deletes a task for example).
Is there a property I can set on the cell? I feel like I'm missing something obvious. This is my first iPhone app, so please be gentle, hehe.
How are you getting the JSON array to an Objective-C array for use in the table? You'll have to do that again for the ID of the task, and in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method use something like theID = [idArray objectAtIndex:indexPath.row];
The better solution would be to maintain a proper data model on the client. You could use the JSON as your model, but Core Data is a bit more robust. What you'd want is to have your controller associate what it knows about the view with what it knows about the model. For instance, if you use didSelectRowAtIndexPath: You'd want to use this index path to map to your model.
Your solution of setting a property on a cell which allows a client of your view to query your data model is in violation of the rules of the MVC paradigm. It couples your view to your model which isn't great.

Submit a form in UIWebView

I have a form in the UIWebView to interact with a web service, when I submit the form, the data gets sent to a web service and it will return the result to another UIWebView. The two UIWebViews are under a navigation controller, so when the use submits the form, it slides to another view to display the result. Can anyone point me out how I can achieve this? What do I fill in the "action" attribute in the form element? How can I get the user input back to the application so I can use the web service? I am new to this field, any help will be appreciated. Thanks!
Update:
Is there a way to save form data from web view back to my program? I think it's better to save form data back to the program (i.e. store params into nsstring) when I click submit button, and then start querying web service.
You can use the
stringByEvaluatingJavaScriptFromString
function for firing a javascript method from objective c and get a return value from that method.
For example
//Calling the getTheUsername in the javascript.
NSString *userName = [yourWebView stringByEvaluatingJavaScriptFromString:#"getTheUsername()"];
and in javascript
//Method in javascript
function getTheUsername()
{
return userNameVariable;
}
And I doubt that here you may wanna do vice-versa (Call obj-c method from javascript). This cannot be done directly here is the work around.
Set a UIWebViewDelegate, and implement the method webView:shouldStartLoadWithRequest:navigationType:. In your JavaScript code, navigate to some fake URL that encodes the information you want to pass to your app, like, say:
window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";
In your delegate method, look for these fake URLs, extract the information you need, take whatever action is appropriate, and return NO to cancel the navigation.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"%#",[[request URL] query]);
return YES;
}
If I understood your problem correctly, I think following steps will help you to provide the solution.
In the action of form, you need to write the URL to the web service.
In your form, the name of the input field should be matched with the name of the parameters expected for the web service.
In your first UIWebView Navigation control, Create the object of screen having second UIWebView and set the response of the web service to the instance member of the object.
In your first UIWebView Navigation control, you need to write
[self presentModalViewController:webView2Object animated:YES];
In the screen of having webView2, parse the response before loading the second UIWebView. and inject it with the displayed page using javascript.

Problems getting an xml feed parsed and loaded into a tableview using delegates

Very new to programming for iOS and Cocoa so please take it easy on me as I try to wrap my brain around the following. I'm trying to display a tableview populated from an XML feed as the opening screen of my app. I've tried to consume the XML from inside my AppDelegate using the ApplicationDidFinishLaunching method (and then making my AppDelegate a delegate for the XML parser which I access using a NSUrlConnection and its delegate methods) but I can't figure out how to take the parsed XML file and pass it to a tableviewcontroller which can then use it as the datasource for a tableview. When I do try, I always get a blank tableview.
I've written the code a few times and nothing seems to work.. I'll post what I have here to show what I've got so far but I'm afraid its mostly vanilla AppDelegate with a few parser methods thrown in.. any pointers in the right direction would be super appreciated.
Thank you in advance!
Hmm, probably a bad idea to do the network call in the AppDelegate. Try to put all that code at the view controller level. Here's a brief structure of what I do (Since it's very similar)
View Controller listens to button events
Use ASIHTTPRequest to talk to your web service. Handles network really well, you can skip the NSURLConnection stuff.
Try to load your data source (an array?) with static values and see if they come up on the table view.
Parse the response from ASIHTTPRequest using NSXMLParser, and load the data you want into the static array you were using. More here.
Call [tableView reloadData] once you're done and the changes will reflect.
Did you specify a UITableViewDataSource for your table view and implement the two required methods?
tableView:cellForRowAtIndexPath:
and
tableView:numberOfRowsInSection:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
When I get blank tables, this is what it is; I've forgotten to specify the data source

how do I take the URL and page title from a UITableView?

I have an iPhone app that shows the user a UIWebView in the first view, what I am trying to do is allow the user to save what is essentially favorites to be saved and then displayed in a table view in the second view. To do this all i need is the page URL and also the Page Title to be saved to then populate the table view. Now here is the problem I am just starting to learn how to make iPhone apps and I have no clue what would be the best way to save it and how to get this information from the web view?
Implement the UIWebViewDelegate protocol. You can obtain the URL from the request object in the delegate method -webView:shouldStartLoadWithRequest:navigationType:
To extract the title from the HTML, run some Javascript on the content using – stringByEvaluatingJavaScriptFromString:. Make sure you do this after the content is fully loaded, i.e. in the delegate method - (void)webViewDidFinishLoad:(UIWebView *)webView
To populate and display in a TableView, you have to have some sort of a DataSource, preferably an NSArray, since you shall be appending or altering this array, use a NSMutableArray. Now to save two bits of information, the URL and the Title, have two arrays, one for Title and one for the URL. (Since the title can be customised).
In the TableView delegate, where you return the number of rows, return [URLArray count];
and in the function to populate the cell, set the Text to [titleArray objectAtIndex:[indexPath row]] and the SubTitle to [URLArray objectAtIndex:[indexPath row]];
Hope this resolves your question and is what you were looking for.