I have file on local desktop.
I'm converting its url by using [NSURL fileURLWithPath:filePath] but I'm getting error. Here is my code:
NSString* filePath = #"/Users/Desktop/bb.ppt";
[powerWeb loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"application/vnd.ms-powerpoint"
textEncodingName:#"utf-8"
baseURL:[NSURL fileURLWithPath:filePath]];
It is giving me this error:
error:::Operation could not be completed. (NSURLErrorDomain error 100.)
error:::Frame load interrupted.
The following may work for you:
[[NSBundle mainBundle] URLForResource:#"mypresentation" withExtension:#"ppt];
I suspect that this is not right:
baseURL:[NSURL fileURLWithPath:filePath]
You are using the same full path in loadData and baseURL parts.
Do you have a file at the location: /Users/Desktop/bb.ppt, even if you had it there I suspect it will try to access /Users/Desktop/bb.ppt/Users/Desktop/bb.ppt looking at the baseURL setting.
I use this code to display ppt in iPhone application:
[myWebView loadRequest:[NSURLRequest
requestWithURL:[NSURL
fileURLWithPath:tempFilePath]]];
But it have a leak when viewing. I don't know why.
Related
Specifically, I have a folder structure that looks like the below:
about (main folder)
css (this sub-folder contains the css files)
img (this sub-folder contains the img files)
js (this sub-folder contains the js files)
page (this subfolder contains the index.html file)
If I click on the index.html file from a normal computer browser, everything works as expected.
However, I am trying to load this index.html into a UIWebView.
So far, what I've done is I dragged the "about" folder to XCode and copied it there as I would any other file. Then I tried the following code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseURL];
The webview loads the index.html, however it doesnt load the images/css/js, as I presume it can't find them within the folder structure.
Any help would be appreciated! Thank you!
oops, I actually found the answer here: Load resources from relative path using local html in uiwebview
I was able to use the actual folder structure as is with the following code:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"/about/page"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Instead of using images/css/js use only sample.js.
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
I have an IOS project with html file resources shown in a webview. These html files have different sections which correspond to fragments (eg, index.html#section1, index.html#section2), which I would like to load in the webview. Unfortunately using [NSURL fileURLWithPath:url] does not work with fragments. The # is converted to %23 in the url, and the file is not found. If I use the [NSURL URLWithString:url] method, it works, but this method cannot load local resources.
Is there a way to have the webview load the local resource url with the fragment?
As you have noticed, this seems to be impossible for file URLs, but you could use a workaround if you don't mind:
[webView stringByEvaluatingJavaScriptFromString:#"window.location.hash = '#section2';"];
It's not obvious how Apple intended this sort of navigation to be triggered. There might be several ways to do it, but this one does the trick for me:
NSURL *url = [NSURL fileURLWithPath:#"/full/path/to/index.html"];
NSString *fragment = #"#section1";
url = [NSURL URLWithString:fragment relativeToURL:url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
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.)
I am using NSData to get content from a URL (RSS feed) and then parse it. While most of the URLs are loaded fine, some of them don't return any data.
These URLs open on the web so I know they are valid, but they just don't return any NSData. One such URL - feed://jpl.nasa.gov/multimedia/rss/rovers.xml
Here's how I am using it (the NSURL object is formed properly)
NSURL *feedURL = [NSURL URLWithString:[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL:feedURL];
Any ideas why?
Thanks a lot.
Switch to NSURLConnection so you have some delegate methods to watch what's happening in debugger. check out the URL Loading System docs if you haven't.