Make app act as a Server - ios

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:

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

WCErrorDomain 7013 when trying to send image using Watch OS 2

I´m testing Apple Watch OS 2 and I´m trying to send a image from the application to the watch. According to Apple, I shall use WCSession transferFile to do this.
Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.
for example:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
It all looks ok in the debugger, the path is correct and the file is accessible (checked with NSFileManager) and readable.
However, everytime I try I get a callback to the didFinishFileTransfer function, including an error:
Error Domain=WCErrorDomain Code=7013 "The operation couldn’t be completed. (WCErrorDomain error 7013.)"
Looking up the error:
WCErrorCodeFileAccessDenied
An error indicating that a file could not be transferred because it was inaccessible.
Available in watchOS 2.0 and later.
It seems the file is not accessible by the send function? I have tried things like resaving the file to another directory etc, but nothing seems to work.
Anyone got an idea?
The URL you are creating is not a fileURL. Try:
NSURL *path = [NSURL fileURLWithPath:string];
I managed to solve the issue!
It was because my path did not start with file://
The following code worked just fine:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
string = [NSString stringWithFormat:#"file://%#", string];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
So it´s quite picky regarding the path.

UIWebView load local content with external baseURL

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

hyperlink not working in a pdf in a UIWebView

I have text file (instructions.docx) with a hyperlink in it - I convert it to a pdf (instructions.pdf) and use it in a UIWebView as follows:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Instructions" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
[_webView setScalesPageToFit:YES];
All works fine except the hyperlink does not work - shows blue - but tapping it does nothing.
Am I going about this the wrong way - or am I missing something?
I'm not sure, but you can try this line of code:
[_webView setDataDetectorTypes:UIDataDetectorTypeLink];
Otherwise, you can try Reader library by vfr. PDF embedded links work in it.
Also you may want to take a look at this question.
I had a similar problem. Turns out the links in the PDF weren't hyperlinks. They were just typed in (even though they were blue and looked like real hyperlinks).
Resolved the issue by:
Manually adding hyperlinks
Checked Word to PDF conversion allowed for "Best for electronic distribution and accessibility"

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