Parse XML and generate UI in UIWebView iPad App - objective-c

I will receive an XML from Server, and have to parse the xml file, and based on that have to populate textbox, listbox etc(a form) in UIWebView.
I know XML parsing for iOS using NSXML, I do not know how to fire events to generate Form/UI at runtime.
Any tutorials or idea pls.

If you have created your HTML as a NSString already, you can load it into the UIWebView by using the method loadHTMLString:baseURL:. For example:
NSString *htmlString = [[NSString alloc] init];
//Download XML, parse it and turn it into HTML.
[myWebView loadHTMLString:htmlString baseURL:nil];
//if you have to do any thing else to the HTML string do it here
[htmlString release];
Changing the htmlString after calling loadHTMLString:baseURL: won't update the UIWebView. You will need to call the method again, sending the modified string.
If you need further help, comment below and I'll edit my answer or comment back.

Related

Extracting Text from url Xcode?

I am new to Objective C.
I was trying to extract text from a webpage and display it in a textView;
Except for when I run the app it appears to show html instead of the article.
NSURL *URL = [NSURL URLWithString:[self.url
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSData *theData = [NSData dataWithContentsOfURL:URL];
NSString *content = [[NSString alloc] initWithData:theData encoding:NSStringEncodingConversionAllowLossy];
_viewPage.text = content;
The viewPage is the textview itself. How do I extract the text only?
You have not included any code to extract the text from the web page. When you run "dataWithContentsOfURL" and it's a web page, then, as you have seen, you get the whole page.
To extract the data you need to process the results. In a simple case you can get the text with some string manipulation. In more complex cases you should look for a library which will parse the whole page for you. You can then access the parsed structure to get the content that you want.
Look here for an example of the simple case;
http://natashatherobot.com/html-css-parser-objective-c/
There are libraries for the more complex case.

Get Information About a Website Using there URL in Xcode

Im trying to Get Information About a Website like title (Like what you see in safari in the Tabs and Windows)and Description of that Website from its URL Without Using the WebKit Framework in Xcode and Displaying it on screen as text. I Just want to know have to obtain the title of the Website/Page that the URL Belongs to using Cocoa. Is There a Tutorial or Something. It will be a Big Help.
Try this:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* title = [webView stringByEvaluatingJavaScriptFromString: #"document.title"];
navbar.title = title;
}
Taken from: http://blog.mcohen.me/2008/12/12/getting-the-title-of-a-web-view-in-cocoa-touch/
If you don't want to display the webview, make it hidden and use above delegate method to get what you want...
A way you could do it is by downloading the HTML content at the target url and parse it, for example using hpple.
Example (not tested):
NSURL *url = [NSURL URLWithString:#"http://..."];
//Only using this for simplicity of the example.
//You should really use an asynchronous method instead (see `NSURLSession`).
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Parse HTML and search for title element
TFHpple *hppleParser = [[TFHpple alloc] initWithHTMLData:data];
TFHppleElement *titleElement = [hppleParser search:#"//title"][0];
NSString titleString = titleElement.text;

How to display NSString content in WebView (Cocoa Mac app)

Is it possible to load a WebView with the text from a NSString? If I save the string content into a file and then use something like:
[[main_browser mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"file:///Volumes/AA/myfile.txt"]]];
It works, but how can I do it without writing the content into a file?
Yeah you can load a NSString into a webview, just look at the following method:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
You can also open files from the disk this way by creating a NSString object and then loading it into the webview. However using loadRequest might be better ( as it is doing the same thing, but then internally ).
If you want to write a NSString to a file you can:
[someStringObject writeToFile:_path_];
Did you actually take a look at the NSString class reference? It is pretty clear there.

Code=102 "Frame load interrupted" when I load a NSstring as a html Iphone

I am developing an app for iphone/ipad that I have some problems with.
I am building dynamically a nsmutablestring with html code and loading as html to a uiwebview. I also load a local js file to this html page where I have some logical functions.
In this html I got a form with a submit button at the end.
What I am trying to do is that at the moment the submit button is click, it will call a method that return a string back to the objective-c code. I have no problem with the javascript controlling the form. But when the submit button is click, the native code gives me
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x6c60b20 {NSErrorFailingURLKey=file://
I have tried to redirect in js-function like window.location="myapp:custom:"+string; But I got the same problem. Even trying libraries like JSBridge api.
I suspect that is because I am loading a NSString to the webview that has the baseUrl the mainbundle path for loading the local js-file.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:string baseURL:baseURL];
I only had to change the input submit with a input button in the html code and works. So now I got the string from javascript back to objective-c.

Get EXC_BAD_ACCESS when tableview get data from URL?

My tableview is get XML data from URL,First I declare a NSMutableArray *dataArray;
and this is how I get the data in my TableviewSample.m
- (void)getDataFromURL{
NSString *theURLAsString = [NSString stringWithFormat:GetAllData];//<-EXE_BAD_ACCESS HERE
//#define stringWithFormat:GetAllData #"http://192.168.10.28/req_alldata.php"
NSURL *theURL = [NSURL URLWithString:theURLAsString];
self.dataArray = [NSMutableArray arrayWithContentsOfURL:theURL];
}
Then I just get the elements form this array to my tableview ...
But here I have to say one more thing , actually it won't crash before I add another view...
I add a bar button to go to a webView , this webView is use to load a IP Cam stream video
When I back to the tableview , It will appear EXC_BAD_ACCESS
This is odd things I cannot to solve it...because both side code are all looks normal
If I remove this webview ,no matter how I run the program ,it won't crash...
And sometimes I leave the webView I will receive memory warning :level 2
But just only one time.
or do I use a wrong way to open an ip cam stream ???
Thanks for all reply : )
OK,here is the code different I use in my webview class
This is first version I use
- (void)viewDidAppear:(BOOL)animated{
NSString *directGoToWebCam = [NSString stringWithFormat:GetAllData];
self.IPCamWebView=[[[UIWebView alloc] initWithFrame:CGRectMake(0,0,640,960)] autorelease];
[self.IPCamWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:directGoToWebCam]]];
[self.view addSubview:self.IPCamWebView];
}
Where does GetAllData come from? It looks like that isn't pointing to anything. More code, the exact error, and a more careful description will go a long way here.
It probably has something to do with the use of #define which does not say anything about the type of the object you are using.
If you want to define a constant string in your code the best would be to use something like this:
static NSString *GetAllData = #"192.168.10.28/req_alldata.php";
Where you need to use the string you can simply write:
NSString *GoToWebCam = [NSString stringWithString:GetAllData];