I want to load a gif image with webView,And it works well.But when i find leaks with instruments,there is a memory leak. Who can tell me why,Here is the code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"icon_coinMoney_image" ofType:#"gif"];
UIWebView *gifWebView = ({
UIWebView *webView = [UIWebView new]; //Leaks Here
[self addSubview:webView];
[webView setBackgroundColor:[UIColor clearColor]];
webView.userInteractionEnabled = NO;
[webView setScalesPageToFit:YES];
webView.opaque = NO;
NSURL *url = [NSURL URLWithString:path];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(backImageView.mas_left).offset(-MARGIN);
make.centerY.mas_equalTo(backImageView.mas_centerY).offset(1.f);
make.size.mas_equalTo(CGSizeMake(ZLL_AUTOSIZING_WIDTH(30.f), ZLL_AUTOSIZING_HEIGTH(30.f)));
}];
[webView.layer setMasksToBounds:YES];
[webView.layer setCornerRadius:ZLL_AUTOSIZING_WIDTH(15.f)];
webView;
});
Related
I have a UIWebView and i want when users go to a link in it they can go back hereis my webview code
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)]; // x is width,y is hght
NSString *urlAddress = #"http://livewiretech.co.nf/ NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
And i have a UIButton and the code for it is
-(void) backButtonPressed {
//Back code here
}
something like this should get you going
#property (nonatomic, strong) UIWebView * webView;
....
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)]; // x is width,y is hght
NSString *urlAddress = #"http://livewiretech.co.nf/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
-(void) backButtonPressed {
if ([webView canGoBack]) {
[webView goBack];
}
}
For Reading PDF file in UIWebView with horizontally in iOS.
So i write following to add with UIScrollView to scroll horizontally.
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.contentSize = CGSizeMake(2000, 420);
scrollView.bounces = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:self.spiderWeb];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.spiderWeb loadRequest:request];
[self.view addSubview:scrollView];
It doesn't showing anything. Without ScrollView , i can read PDF in UIWebView with vertically. Where am i wrong?
Check the below links may be it is useful for you
https://github.com/iclems/iOS-htmltopdf
https://developer.apple.com/library/ios/samplecode/PrintWebView/Introduction/Intro.html
http://www.labs.saachitech.com/2012/10/23/pdf-generation-using-uiprintpagerenderer/
i triad to click on URL which is in PDF file. and PDF file opened in UIWebView, but i cant able to click on URL.
any one tell me how to open URL which is in PDF file.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
NSURL *targetURL = [NSURL URLWithString:#"http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
In you webView deleget method, first of detect the url from PDF and then open in webview or etc. I have used following method to do this, may be its helping you :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [[ request URL] retain];
if (([[requestURL scheme] isEqualToString: #"http"] || [[requestURL scheme] isEqualToString: #"https"]) && (navigationType == UIWebViewNavigationTypeLinkClicked))
{
// Your method when tap on url found in PDF
}
}
If you have a PDF file in Local storage
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
If you have Access to file From the Internet
- (void) loadRemotePdf
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSURL *myUrl = [NSURL URLWithString:#"http://www.mysite.com/test.pdf"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[webView loadRequest:myRequest];
[window addSubview: myWebView];
[myWebView release];
}
Basically, I want to click a button in my web view that will download a PDF from a remote website and display it with a close/back button. I'm using QLViewController so people can pinch-zoom the PDF, and I'm using PhoneGap 3.0 with XCode 5 to code it.
Here's the code for my plugin that will show the new view with the back button but I can't seem to get the PDF to show. Any ideas?
#import "PDFViewer.h"
#import <Cordova/CDV.h>
#implementation PDFViewer
- (void)loadRemotePdf:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString *website = [command.arguments objectAtIndex:0];
NSString *filename = [command.arguments objectAtIndex:1];
if (website != nil && [website length] > 0) {
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
[webView canGoBack];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// NSURL *myUrl = [NSURL URLWithString:website];
// NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
// [webView loadRequest:myRequest];
// [window addSubview: webView];
NSURL *url = [NSURL URLWithString:website];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, filename];
[urlData writeToFile:filePath atomically:YES];
// Create test view controller
QLPreviewController *previewer = [[QLPreviewController alloc] init];
previewer.dataSource = self;
previewer.delegate = self;
// Create navigation controller
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:previewer];
[window addSubview: [nav view]];
[window makeKeyAndVisible];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
// return result
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
{
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
return fileURL;
}
#end
Yes, this can be done with the UIWebView.
If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
//Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
I am trying to view a PDF file from a server but I only get a view in black and doesn't contain anything. Here's my code:
pdfViewer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 768, 949)];
pdfViewer.backgroundColor = [UIColor whiteColor];
pdfViewer.delegate = self;
[sgContainer addSubview:pdfViewer];
chatSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
chatSpinner.frame = CGRectMake(334, 462, 100, 100);
[pdfViewer addSubview:chatSpinner];
NSString *httpSource = #"http://www.mywebsite.com/folder/subfolder/file.pdf";
NSURL *fullUrl = [NSURL URLWithString:httpSource];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
[pdfViewer loadRequest:httpRequest];
If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Let me know if you face any problem.