To test working of xml in iphone? - iphone

HI can i place both xml files and images that the xml file transports in xcode to test the working since i don't have the internet ? If it can be done , what should be the path name for the xml to be coded in program and images to be typed in xml.?

NSString *stringToXML = [[NSBundle mainBundle] pathForResource:#"theXMLDoc" ofType:#"xml"];
NSURL *XMLURL = [NSURL fileURLWithPath:stringToXML];
Using that you can get XML documents from the resource folder and use them.
Just substitute your url in your code for the variable XMLURL and you should be fine. Its the same for images, you just need to make an NSURL out of the path resource.

Related

Using Local Resource and Relative Link in the same UIWebView

I have a problem with a WebView. I'm loading an Html String into a web view. As base URL I need to use my site URL (because there are some relative link inside the html and I cannot modify it).
The problem is that I also need to use some images and css that are stored inside the Application bundle.
So I'm creating a reference to the local resource using this code (the result string is used as src in html file):
NSString *cssUrl = [NSString stringWithString:#"file:"];
cssUrl = [cssUrl stringByAppendingString:[[NSBundle mainBundle] resourcePath]];
cssUrl = [cssUrl stringByAppendingString:#"/story.css"];
Then I've also added:
cssUrl = [cssUrl stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
cssUrl = [cssUrl stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Any idea how to solve the problem and how to create a reference to a file that can be used inside a web view?
Thanks
Francesco
First, import a directory, e.g. one called web, into your project where all the files reside. Make sure you select "Create Group References For Any Folders" when Xcode asks you.
Now you can load your initial string like this after putting it into a file inside web, say index.html:
NSString* path = [[NSBundle mainBundle] pathForResource:
#"index" ofType:#"html" inDirectory:#"web"];
if (path)
[self.webView loadRequest:
[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Now all relative source URLs in your string will work as expected, including <img>, <a> etc.
How about this?
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"story" ofType:#"css"];
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSString *theMagicalURLString = [url absoluteString];
It seems that what I was trying to do it's not possible. To use the bundled resource you have to set the base URL of your web view to the resource path.
Thanks to everyone for your help
Francesco

Open the files in my application

I am having an application which consists of many different file formats like ppt,pdf,txt,png etc, I want to open it whenever the user taps the particular file. How this can be done?Please help me
Can I use WebView for all the files ?
Have a look at the quick look framework with UIDocumentInteractionController class it will have you to open different format of file in its view controller..
QuickLook is a framework that provides quick previewing of a range of document types – supported documents include iWork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (csv) files.
Sample Application
UIWebview allow you to display office documents :
You can open them by loading the file data, example with RTF file :
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[self.webView loadData:myData MIMEType:#"application/rtf" textEncodingName:#"latin1" baseURL:nil];
Or you can load them by creating a request with local path :
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"pdf"];
NSURL *url = [NSURL URLWithStringdfPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
Note that for Office documents with complex layout you will have a very (very) poor rendering.
The only solution is to call a separate application that handles this kind of documents (Pages, Quick office, etc)
Have a look at UIDocumentInteractionController to open your documents in another app
Hope this helps,
Vincent

Loading a local HTML file in Navigation based application

I am new to iOS development.
I have been trying to find an answer to my question but I could not, I have found some related answers but I could not fix the problem.
My problem is, I have made an application which has a table, and for every row there should be a URL which loads a UIWebView, and should be loaded when you click on the row, but it loads an online URL such as #"http://www.google.com". But I want it to load a local HTML file.
I have used this for the URL:
azkarDetailViewController.detailURL=
[[NSURL alloc] initWithString:
[[[azkarData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"url"]];
And for every row in the table I use:
[AZKAR addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"THE NAME OF THE ROW",#"name",#"DayandNight.png",#"picture",#"http://www.google.com",#"url",nil]];
So I want the application to load a local file from the Resources (say for example the name of the HTML file is "index.html").
Can you please help me solving this problem ?
Thank you very much..
When you compile your app, the output is a “bundle” of files and directories. You can actually access the bundle through the NSBundle class; the static method +mainBundle will return a pointer to an instance of NSBundle that represents your app's main bundle.
You can then use the -URLForResource:withExtension: and -URLForResource:withExtension:subdirectory methods of NSBundle (these require iOS 4.0 or higher—there are older equivalents as well). If the HTML file is stored in your main bundle directory (that will be the case unless you created a an actual directory—different from a group—in your bundle), you can find its URL this way:
NSString *myDocumentName = #"index.html";
NSURL *documentURL = [[NSBundle mainBundle] URLForResource:myDocumentName extension:Nil];
Note that you do not have to specify the extension separately if it's already in the filename.
Note: my explanation is a little oversimplified (I'm assuming you don't need to deal with localizations, otherwise there are other issues you should be aware of explained in the docs).
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 40000
// code for iOS below 4.0
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WindowsTest" ofType:#"html"]];
#else
// code for iOS 4.0 ++
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"WindowsTest" withExtension:#"html"];
#endif

Displaying localized text unavailable at compile time on iPhone

For use in a questionnaire application, a web service will provide a list of questions in one of several languages, chosen by the user at runtime. The questions will be downloaded from the web service in the chosen language and displayed to the user.
The problem: I have no idea how to do this.
As a sample, I tried loading in UTF-8 text files (e.g. arabic.txt) in the resources containing samples of text in said languages. The files open and render properly in TextMate and TextEdit, but are illegible in Xcode. They are successfully read in, but their contents will not display.
Example:
I create a UITextView and, at initialization:
...
NSString *path = [[NSBundle mainBundle] pathForResource:#"arabic" ofType:#"txt"];
NSString *arabicString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringENcoding error:&error];
myTextView.text = arabicString;
...
The NSError returns NULL, so there's no error reading in the text file, but the contents of the UITextView is not set. Nothing happens. No error (compilation or runtime), nothing.
Any ideas for a different approach? Or a way to make this work?
Thanks so much.
What you are currently doing sounds reasonable, but for a different approach that I have used (that worked for me), try using a UIWebView. Something along the lines of:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/arabic.html",path]];
NSData *data = [NSData dataWithContentsOfURL:url];
[self.helpWebView loadData:data MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:baseURL];
(Be interesting to see if using arabic.txt and mime-type of text/plain loads any differently in the web view.)

UIWebView display locally stored website (HTML, images, Javascript)

I've looked EVERYWHERE for this, can't find anything. I basically need to store an entire website inside of my iPhone for some interesting reasons. Regardless I can't figure out how to display it correctly in my UIWebView.
EDIT: I should clarify, I can load the original HTML file, and I have chagned all of the pathing to be local, except nothing gets linked in.
Here is the code
self.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self loadRequest:request];
index.html has a bunch of <script type="text/javascript" src="somescript.js">
None of the JS code gets executed
Looks like you're loading the HTML from inside your bundle. This means that all the additional files (.js, .css, and any media files) also need to be present in your bundle. So the first thing to check is to look inside the contents of your executable and make sure the js, etc. files are included.
If that looks fine the next thing to check is if the html, js, or css files reference content via relative or absolute URLs. If there's an absolute path reference in the web content then UIWebView is going to try to download that content each time so it'll only work when you have a net connection. If the path is relative then it's going to look in the bundle to see if such a file exists.
When you included the html and content into the XCode project file you probably dragged the file(s) over to the project side-bar and were asked whether to "Recursively create groups for any added folders" or to "Create Folder References for any added folders."
The default is the first one which means XCode creates a yellow folder in your project, but it'll ignore the directory hierarchy on disk when time comes to generate the output bundle. If you choose the second option then the folder is blue and if you look in your output bundle you'll see that the whole folder hierarchy has been replicated.
The first works for simple web pages where everything is at the same folder level and you can use the method you list above to load it. The second case works better if your web page is complex and references content in sub-folders in which case you need to load the web pages from a relative path (say, the 'webpages' folder):
NSString *path = [[NSBundle mainBundle]
pathForResource:#"index" ofType:#"html"
inDirectory:#"webpages"];
The last thing to check for is if there are any BASE tags in the html file. This is a way to specify a default address or target for all links on a page, but it can muck up webview links.
The problem is that this call:
NSURL *url = [NSURL fileURLWithPath:path];
doesn't setup a baseURL and so relative paths in the .html file for things like javascript, css, images etc don't work.
Instead use this:
url = [NSURL URLWithString: [path lastPathComponent]
relativeToURL: [NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory: YES]];
and then things like "styles.css" in the index.html file will be found IFF they are copied into the bundle next to the .html file.
You need to set this:
myWebView.dataDetectorTypes = UIDataDetectorTypeLink
Make sure that the .js files are in your copy to resource bundle section and not in the compile section. Xcode places them in the compile group by default.
When adding pathFor resource in Dictionary , it displays a nil string error.
My attempt was to run an entire web page out of the Xcode project file. To do that you must:
When importing the file select "Create folder references for any added folders".
Set up the web view, but make sure you set the relative path as previously mentioned.
NSString *path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"html"
inDirectory:#"Directory"];
NSURL *url = [NSURL URLWithString:[path lastPathComponent] relativeToURL:
[NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory:YES]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.currentWebView loadRequest:request];