IBAction Open Local HTML File - ios

I've got a view based application that one is at last just a single WebView ... with a Tabbar with some Icons.
So i need to add a small icon for go back to the localHTML File home.html.
Maybe could someone give me some informations how to handle it without change the view to a new one?
Here is how my other IBActions looks like
- (IBAction)news_button:(id)sender; {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
}
Other option could be open a Website but not in safari ... like it does at the moment.
Thanks for sharing! :)

With the line above you are opening it in Safari right now.
You can get path to the local file if it is in main bundle catalog with
NSString *filePath = [[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], #"home.html"] retain];
So why not use
[yourWebView loadRequest:[NSURLRequest initWithURL:[NSUrl initWithString:filePath]]];

Related

Load local file UIWebview before transition

I searched alot for this but couldnt find a perfect answer.
My Ios app has its every page in UIWebview.
The only problem I am facing is when transiting from one page to another, the loading of UIWebview takes 1-2 seconds and the user sees a white background. Since all the html, css, image files are stored locally, how can I preload the UIWebview before transition? So that the user gets native feel.
I am loading the URL this way :
[self.navigationController setNavigationBarHidden:YES];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"start" ofType:#"html" inDirectory:#"main/html"]];
NSString *path = [url absoluteString];
NSURL *filePath = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#",path,#"?logged_in=1"]];
[self.webView loadRequest:[NSURLRequest requestWithURL:filePath]];
I am using a different viewcontroller for each page as I want the native push and modal transitions.

how to send file from from one app to another app using url scheme?

Summary (iOS 8, Xcode 6.4)
First question:- Can i share my app's Documents Directory's data with my other app?
If Yes, I've seen many questions related to this;
Move data/images between two iOS apps using custom URL handler,
http://code.tutsplus.com/tutorials/ios-sdk-working-with-url-schemes--mobile-6629
But I found that these example only send text or URLs. Then I tried myself as below:
NSString* path = [NSString stringWithFormat:#"MY_URL_SCHEME://"];
NSURL* url = [NSURL URLWithString:path];
if([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
The above code works well to open my other app. But when I try like below, I can't open my other app.
NSArray* mainPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sourcePath = [mainPath objectAtIndex:0];
NSString* path = [NSString stringWithFormat:#"MY_URL_SCHEME://%#",sourcePath];
NSURL* url = [NSURL fileURLWithPath:path isDirectory:YES];
if([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
So, please help me, what am I missing?
EDIT:-
i forgot to mention that,iOS7 support is important in my App.so, i think extension might not work.
Use NSUserDefaults with app group to share the data between apps
NSUserDefaults *defaults=[[NSUserDefaults
alloc]initWithSuiteName:#"app group name"];
[defaults setObject:filedata forKey:#"keyfordata"];
[defaults synchronize];
in the app delegate of consuming app fetch the data from NSUserDefaults
another way is to use share extension-
http://easynativeextensions.com/how-to-launch-your-app-from-the-ios-8-share-menu/
You can refer MGInstagram files as Instagram mobile app works same. You can pass image from your application to Instagram application.
You can download it from here: https://github.com/mglagola/MGInstagram
Hope this helps.

Pass an Url to image view through AFNetworkingLibrary

The project was created with xcode 4.3.
I have used AFNetworking Library (non-ARC) in this app.
NSURL *url = [NSURL URLWithString:urlstring];
[exhibitPortraitImageView setImageWithURL:url];
Here, when I print a NSLog value, am getting the URL but the imageview is not displayed.
How can I solve this?
Your code is fine, problem is with URL.
Your URL, don't have prefix, http://, I added it and the code is same as in quesiton.
NSString *urlstring = #"http://mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg";
NSURL *url = [NSURL URLWithString:urlstring];
[exhibitPortraitImageView setImageWithURL:url];
Here, you've three options to fix this,
1) If URLs are not coming from server (or some where else) you can fix it within the app, see how,
NSString *badUrlString = #"mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg";
NSString *goodUrlString = [NSString stringWithFormat:#"http://%#",badUrlString];
2) Or if its coming from server (or some where else) you can ask them to fix this from their side.
3) If its under 2nd option then, ask them if this will always happen (static) then you can also modify this from your side if server side developers not able to fix.
see this for more help, Check string containing URL for "http://"
Yes,
I also tried your url, it is working fine.
I loaded it as:
NSURL *correctURL = [NSURL URLWithString:#"http://mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg"];
_imgFromUrl.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:correctURL]];
And the result,

Open local PDF file in iBooks

I am trying to open a PDF file that I have stored locally within my app in iBooks. The app currently has a list of "literature" in a table view and when each cell is tapped the app segues to a webView that displays the PDF file (works great). I have made a BarButton at the top right in navBar to allow the user to open the PDF in iBooks (so they can store it on his/her device).
So far the button will open a UIDocumentInteractionController that displays all apps on the device that can open the file (after checking if iBooks is installed). Then when I click the iBooks icon the app crashes. I was able to revise the code so that iBooks opens without crashing, but the PDF file is not carried through so it's kind of pointless (code below is reverted back to when it crashes).
Code below is inside the IBAction of the barButton...
NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"itms-bookss:"]])
{
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
NSLog(#"ibooks is installed");
}
else
{
NSLog(#"no ibooks installed");
}
Fixed it! Took some time away from the project and after coming back two weeks later I figured it out in <15 minutes.
So it was a memory issue for the docController and by declaring in the .h file and using (retain) it works perfectly. I was also able to use the [NSBundle mainBundle] method as well.
.h
#property (retain)UIDocumentInteractionController *docController;
.m
#synthesize docController;
//in bar button IBAction
NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"itms-books:"]]) {
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
NSLog(#"iBooks installed");
} else {
NSLog(#"iBooks not installed");
}
On iOS 8 the layout of the file system changed and sharing files directly from the main bundle no longer works. Copy the file to the documentsdirectory and share it from there.
Here is how to create the file path:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *fileName = [NSString stringWithFormat:#"%#.pdf",litFileName];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *url = [NSURL fileURLWithPath:filePath];

Run HTML5/CSS3 in UIWebView

I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com

Resources