open iBooks from my app with my PDF file from my bundle - ios

It is continuation of open iBooks from my app I did try https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html but it is hard to understand with out code sample.
I have iPhone app, that have Web View.
After starting the app Web View opens custom PDF, and this is working fine.
User wil not read that PDF from my app, so I wont to add button that will open that PDF in iBooks (because iBooks have search and lost of other features…).
I have Toolbar on bottom with button for opening my PDF in iBooks.
And this is my problem, I do not have code for opening my PDF in iBooks.
ViewControler.h
#import <UIKit/UIKit.h>
#interface BG_ViewController : UIViewController
{
IBOutlet UIWebView *myWebView;
}
- (IBAction)openInIBooks:(id)sender;
#end
ViewControler.m
#import "BG_ViewController.h"
#interface BG_ViewController ()
#end
#implementation BG_ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyBOOK" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myWebView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)openInIBooks:(id)sender
{
NSLog(#"Clicked\n");
// CODE ???
}
#end
What code need to be added to - (IBAction)openInIBooks:(id)sender so that it will open MyBOOK.pdf ???
Best that I have is:
NSString *stringURL = #"itms-books:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
But this will just open iBooks app, but with out MyBOOK.pdf.
I tried just to add "itms-books:MyBOOK.pdf", but it is not working :-(

You can open iBooks app from your code by calling [NSURL URLWithString:#"ibooks://"]. But If you want to pass document you should have a look on UIDocumentInteractionController this is a way Apple wants you do do that.

Related

iOS show webview offline with images

I am trying to download a webpage (html) then display the local html that has been downloaded in a UIWebView. It is working, but the offline UIWebView doesn‘t show me the images.
This is my code:
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
NSURL *url = [NSURL URLWithString:#"https://www.mypage.com"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
[Website loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
Can anyone share some sample code to add the website content (the images)?
Any help would be appreciated. Thanks!
Most likely the HTML references images that you aren't also downloading. The first step would be to parse the HTML and download the images too. Then you may need to fix the local copy of HTML if the image references aren't relative to the base document. For example, if they refer to images on a different server, you'll have to fix those links internally. And if the original document uses Javascript to load images dynamically, it may never work the way you want.

Xcode 7 UIWebView doesn't load url

I use UIWebView in my app and it all worked fine in Xcode4,5,6 simulator. but not for Xcode 7 simulator, I don't know why, there is no warning or error in simulator, and the screen is just showing blank page. Please help me. Thanks.
#import "IndexViewController.h"
#interface IndexViewController ()
#end
#implementation IndexViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = nil;
NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([languageCode isEqualToString:#"zh-Hans"]) {
urlString = #"http://www.originoftime.net/index-cn";
}else if ([languageCode isEqualToString:#"zh-Hant"]) {
urlString = #"http://www.originoftime.net/index-cn";
}else{
urlString = #"http://www.originoftime.net/index-en";
}
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[_Index loadRequest:urlrequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Xcode 7 with iOS9 now force you to not use HTTP call, but HTTPS ones.
this is a security point enhanced in AppTransportSecurity.
Try this:
Go to your info.plist
Add a dictionary called NSAppTransportSecurity
Add a boolean attribute to this one, called NSAllowsArbitraryLoads
Pass it to TRUE
Reload your app.
I advice you that if Apple want to block HTTP (unsecured) calls is for a good reason. http://www.originoftime.net/index-cn has a HTTPS one but the server certificate seems to be self-signed.
Let me know if this workaround work for you
Cheers from france
Do you implement the web view delegate methods? In particular:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
If a load fails, that will tell you what the issue is.
It could well be the error related to the new security model which is being enforced for network access. You can override this new behavior by adding the following into your Info.plist file. Just edit the XML and paste this in:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
The changes are summarized here: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

Is it possible to resume where you last were in UIwebView Xcode 5

Hi im am currently building a app where i have multiple webViews with different sites. My question is if you can when you click a view and surf on it and the switch to a different view and the back to the first view resume where you were and not having to start over just because the view reloaded? I now the question is kind of messy but if you could help it would be awsome! im using a regular UIvewView
NSURL *url = [NSURL URLWithString:#"https://www.mywebsite.com"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
Ok, you can just save the last url you went to and reload that page? I think you can do that with NSUserDefaults.
No need to use NSUserDefaults or state restoration if you're just looking to save the page address between views. (If you're looking to save the data between app uses, then yes, you could store the url string using NSUserDefaults.)
To store the value in your app delegate for global access, declare the variable in your app delegate's .h:
#property (nonatomic) NSUrl *currentUrl;
To access the variable in a class, create an instance of the app delegate in the .m:
#import "ViewController.h"
#import "AppDelegate.h"
#interface ViewController ()
#end
#implementation ViewController
AppDelegate *mainDelegate;
- (void)viewDidLoad
{
mainDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
Then instead of using:
NSURL *url = [NSURL URLWithString:#"https://www.mywebsite.com"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
initially set the currentUrl value before webView is ever opened:
mainDelegate.currentUrl = [NSURL URLWithString:#"https://www.mywebsite.com"];
and prior to each webView open, initialize the NSUrlRequest to:
NSURLRequest *requestURL = [NSURLRequest requestWithURL:mainDelegate.currentUrl];
Finally, to store the current address of each webView upon closing, use:
mainDelegate.currentUrl = webView.request.URL;

IOS App Development WebView

I am trying to make an iOS app and I need to add a web view but the issue is I get an error:
Duplicate declaration of method 'viewDidLoad'
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
I am testing with google for now and when I develop the web site for the web view I will put the website in.
objective-C does not support method overloading, so you have to use different method names.I am sure you have two viewDidload methods implemented in your .m class so please try to remove one method
i have attached screen shot for your reference try to remove any one viewDidLoad method

UIWebView Delegates Not Triggering ? iOS

I am loading a local HTML into a UIWebView as follows :
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup our UIWebView
webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.delegate = self;
webView.scalesPageToFit = YES;
webView.userInteractionEnabled = YES;
[self.view addSubview:webView];
...
[self loadWebPage];
}
- (void)loadWebPage {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
And in the header :
#interface ViewController : UIViewController <UIWebViewDelegate>
However for some reason my webview delegates are not being called :
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"Starting HTML Load Process");
}
- (void)webViewDidFinishLoad:(UIWebView *)webViewRef {
NSLog(#"Web View Finished Loading Method");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Error loading HTML ! %#", error);
}
Can anyone suggest why ?
Thanks !
Called the method [self loadWeb];
But on definition the method is - (void)loadWebPage;
Is it typo?
The only way I can reproduce is this by declaring the webView #property as weak. Can you confirm you are declaring it as a strong reference?
how did you declare your UIWebView instance variable, as a strong or weak? If weak make it strong.
The problem is that this call.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]];
Looks like you're loading the HTML from inside your bundle. This means you need to confirm that all the additional files (.js, .css, and any media files) also need to be present in your bundle.
So the first thing need to check fileURLWithPath: point either it's returning nil or not. If it's not then it means that .html page resolved, however the additional files not resolved as part of the request because it's not placed on the same "index.html" path.
First you need to confirm the directory structure of your "index.html" and all the additional files used inside the page. In order to do that, you have to right click on binary .app in your "build" directory in the finder and then select "Show Package contents".
binary (.app) > right click > Show Package contents
This shows what the app's main bundle contains. Look inside for your HTML file. If it's in a folder, that folder name needs to be in the inDirectory parameter of the pathForResource call. If it's on the top-level then you don't have a folder reference. Either delete & re-drag it into project or use the pathForResource version without inDirectory.
The best way to don't setup the base path or relative path for the resources inside .html page (.js, .css and images) because it don't work. Copy all the additional resources next to "index.html" file so it would be easy to resolve on runtime from bundle.
NSString *path = #"<Index.html Path>";
NSURL *url = [NSURL URLWithString: [path lastPathComponent]
relativeToURL: [NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory: YES]];
Try this
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO];

Resources