UIWebView load local content with external baseURL - ios

is it possible to load local content in a WebView when settings the BaseURL to something external?
I need to set the baseURL so something external because I'm integrating the Facebook Widget, what uses the same origin as the source file (origin=file).
So I've to set the origin to http://
But then I'm unable to load local ressources (like css, images), also when setting the absolute css file path in the bundle.
How can I fix that?

As far as I know you need set http or local and for the facebook use the native api.
https://developers.facebook.com/docs/ios
So you can do all in local with facebook api.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
hope this helps

Related

How to open a web (.html) ressource file with native code (ios app)

I have an ios app built with Cordova.
I would like to open a web ressource file (.html) after a deep linking, in a UIWebView with the handleURL ios method.
I know I'm supposed to do this within my cordova JS files, but I would like to know the way to do it natively?
Let's say I define a my-app URL scheme and want to open file1.html.
What are the ways for doing that?
I have found this:
// Load the html as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// Tell the web view to load it
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
But nothing happens, like it can't find that file.
Why is that?
put all files in a folder and while adding drag drop in project. and select create folder references and copy items if needed. and try to load in UIWebView
You make a new class that inherits from NSURLProtocol.
You need to call [NSURLProtocol registerClass:YOURCLASS] in your app
In your class, implement + (BOOL) canInitWithRequest:(NSURLRequest *) to return true if the scheme matches your scheme (e.g. my-app)
See the docs for NSURLProtocol to see how to implement the rest of the functionality. For simple file system reads, you can implement startLoading and call self.client callbacks (e.g. URLProtocol:didLoadData:). For more complex things, you might need to override more.
See this tutorial: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial

Make app act as a Server

I'm using CocoaHTTPServer, and I'm able to start the server. In my resources folder, I added a file called "index.html", and here is something that made me confused.
I can get the path of the index file by
[mainbundle
pathForResource:#"index" ofType:#"html"]
but it gave me the path as
/Users/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/.
Is there any way to make the path become a web-url like
http://127.0.0.1:56000/user/library/.../index.html" to load it on an
UIWebView.
I want to get the file path like the format above because I want to
catch when the UIWebView request to it and when will the app
response.
Is there any way for me to achieve it??
Thanks in advance.
You can load a resource into a UIWebView like this
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"www"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
For catching the requests of a UIWebView please check UIWebViewDelegate protocol here. You can play around with following method for example webView:shouldStartLoadWithRequest:navigationType:

UIWebView loadHtmlString not working on device

I have a webview which i want to load using the loadHtmlString method. The problem is that I want to be able to change the img src's with images that i have previously downloaded. I also use google analitics in the html so I need to set the baseUrl to the actual url so it will work. Here comes the problem. If I put the baseUrl in, the images will not load. If I don't set the baseUrl, it works. How can I get around this, so I will be able to both use google analitycs and have the images store locally in my application? I would prefer not having to implement the google analitics sdk in my project.
A strange thing is that if I run it in simulator, and not put the "http://" prefix in front of my baseUrl, it works fine. However, when I run it on a device, I receive the following error and it doesn't work:
Domain=WebKitErrorDomain Code=101 "The URL can’t be shown"
Thanks
EDIT
If I do this, it works:
[appsWebView loadHTMLString:htmlString baseURL:nil];
However, I must provide a baseURL in order to have Google Analitics working, I have two further cases:
This one gives the above mentioned error: (it works ok in simulator but gives error when running on device)
[appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:#"test.com"]];
This one simply doesn't show anything: (neither loads the html string or the url)
[appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:#"http://test.com"]];
I incorrectly assumed that the problem was that the local image was not fully specifying the full path, but that does not appear to be the problem here. But, you are quite right that it appears (somewhat surprisingly) that you cannot specify some web-based baseURL and also reference a local image in your HTML string. No simple solutions are leaping out at me, but at the very least, it appears that you might have a couple of (not very good) options:
First, you could base64 encode the local image using some base64 library like Mike Gallagher's NSData+Base64 category, e.g.:
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *imageDataBase64 = [imageData base64EncodedString];
NSString *imageHtml = [NSString stringWithFormat:#"<img src='data:image/png;base64,%#'>", imageDataBase64];
This slows the initial rendering, but maybe it's better than nothing.
Second, you could always try leaving the baseURL as nil, removing the JavaScript that does the Google Analytics from the HTML string, and then try injecting that JavaScript via stringByEvaluatingJavaScriptFromString. This approach may or may not work depending upon the complexity of the Google Analytics JavaScript (e.g. what further web-based references it might have), but there's a outside chance you might be able to do something that way.
My apologies for assuming the problem was a trivial img URL. Clearly you had identified a more fundamental issue.
Original answer:
Create your image URLs in your HTML string to be fully qualified file URLs within your local file system:
The image is either in Documents:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:imageName];
Or in the bundle:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName
ofType:nil];
But, once you have fully qualified path, you should be able to use that:
NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
NSString *imageHtml = [NSString stringWithFormat:#"<img src='%#'>", imageUrl];
I would bet it's a casing issue. Take into account that the Device is case sensitive whereas the Simulator is not. Check the URL and make sure it contains the right characters.

UIWebView with absolute path reference to CSS

I have a number of web pages that are used inside a UIWebView that currently reference resources with both relative and absolute paths like so:
<link rel="stylesheet" type="text/css" href="/css/path/mycss.css"/>
<img src="myimage.png" />
The HTML file itself is at path "/page/path/page.html" with myimage.png in the same directory. These same pages are also used for rendering directly in a browser and ideally I'd rather not change the paths. On the iPad, the web site content is dynamically loaded into the Documents directory for offline viewing. The problem is that there doesn't seem to be any way to get UIWebView to handle the resolution of the absolute paths. I'm using the following to load the content:
NSData *data = [NSData dataWithContentsOfFile:fullPath];
[self loadData:data MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:baseURL];
If I point baseURL at the "/page/path" directory, UIWebView will correctly find myimage.png, but will not find mycss.css. I also tried pointing baseURL at the Documents directory, but this fails to resolve either resource. In fact, I haven't been able to get UIWebView to resolve any absolute paths at all.
Has anyone been able to get absolute paths to work with UIWebView?
Did you try to use something like that (see below) for the path of your CSS ?
<link rel="stylesheet" type="text/css" href="../../css/path/mycss.css"/>
<img src="myimage.png" />
Why do you want some absolute path references in your html files ?
I don't think it is a good idea, because you cannot know your future users application installation pathes.
For example, my embedded web page is at
file://localhost/var/mobile/Applications/0E8A7BA5-8DB7-4F88-AC69-0E48F9182DD2/MyApp.app/homepage/index.html
The 0E8A7BA5-8DB7-4F88-AC69-0E48F9182DD2 part changes every time you delete your app !
I think the good way is
adding your website in xCode by selecting "Create folder references for any added folders"
only have some relative pathes
use :
NSString * urlString = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"homepage"];
NSURL * url = [NSURL fileURLWithPath:urlString isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];

show .key(keynote) file in iphone

I am given some keynote files .
I have to put along with an app .
when I open any app it should show that keynote presentation in the app .
my questions are ,
Does ios supports keynote presentation?
if yes, how to show them ?
However I checked this out ...
but.. not working
http://developer.apple.com/library/ios/#qa/qa1630/_index.html
iWork '09 documents do not use a package format and must not be ZIP compressed.
To display supported documents in a UIWebView, create an NSURL as a file URL with the path to the document. Listing 1 demonstrates a method that uses a UIWebView to load a document from your application bundle.
Listing 1 Loading a document into a UIWebView.
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:#"mydocument.rtfd.zip" inView:self.myWebview];
You can open the keynote file in a UIWEBVIEW as a local bundled file or sitting on a server as well.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"key"];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
you can view Excel (.xls), Keynote(.key.zip), numbers (.numbers.zip), pages (.pages.zip), PDF(.pdf), Powerpoint (.ppt), Word (.doc), Rich text format (.rtf), Rich text format Directory (.rtf.zip), Keynote '09(.key), numbers '09 (.numbers) and pages '09(.pages)
Alternatively, or indeed alongside the preview functionality, you can give the user the option to open the keynote using any able and installed applications externally.There's a good answer to a question requesting this feature here: How to use "open in..." feature to iOS app?

Resources