Web content doesn't fit webview's frame? - ios

I'm using a UIWebView to display a mobile website on my iPhone. The UiWebView is smaller than the display of the iPhone.
My Problem is that the content size of the website (a mobile website based on JQuery mobile) doen't fit the size of the web view.
Just an example with a small web view trying to display the google mobile website:
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView sizeToFit];

Primarily : set the delegate to the WebView
In .h file
#interface WebViewViewController : UIViewController<UIWebViewDelegate>
In .m file
webView.delegate =self;
webView.scalesPageToFit = NO;
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

add these to your code for your webview :
[webView setScalesPageToFit:YES];
[webView setContentMode:UIViewContentModeScaleAspectFit];

Related

Attempt to Display Local PDF With WKWebView Results in White Screen

I have a PDF file stored in my local applications directory. I wrote the following code years ago to display the pdf in a webView. It has worked flawlessly, but no longer works. It instead displays a white screen.
NSString* pdfFileName = [self getPDFFileName];
WKWebView* webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *url = [NSURL URLWithString:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
I have since tried other methods of WKWebView such as:
NSData *data = [NSData dataWithContentsOfFile:pdfFileName];
[webView loadData:data MIMEType:#"application/pdf" characterEncodingName:#"" baseURL:[NSURL URLWithString:pdfFileName]];
or
[webView loadFileURL:url allowingReadAccessToURL:url];
but these don't work either.
What changed that is preventing a local pdf from displaying?
You need to use:
[NSURL fileURLWithPath:<valid_file_path>]
Always use your file browser to confirm both the file path and the path itself are valid in case of any issues.

WebView Url not full load in iOS10?

Same url in run in lower then iOS10 (ex. iOS8,iOS9) work properly.
-(void) loadUrl
{
NSString *urlAddress = #"http://myurl.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[detailWebView loadRequest:requestObj];
}
I tried your code and it works fine in ios 10 and xCode 8.
I used this,
-(void) loadUrl
{
NSString *urlAddress = #"http://google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
And call this method where you want using [self loadUrl];
Add URLSchemes in your plist file for your url.
I am getting webview like,
Your code is not wrong.
Hope this will help you.
In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.import webkit.h and use this code
#import <WebKit/WebKit.h>
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURL *nsurl=[NSURL URLWithString:#"http://www.apple.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];

images are not updating in the webview

I am using the below code,
NSURL * url=[NSURL fileURLWithPath:htmlfilepath];
NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
All the contents from html are updating to pdf except the signature.
UIWebView: Images are not updating
But I am still facing the same problem.
If you are using same url with updated content use this.
UIWebView* webV=[[UIWebView alloc]init];
NSString* htmlString= #"Loading...";
[webV loadHTMLString:htmlString baseURL:nil];
[webV loadRequest:[NSURLRequest requestWithURL:mainURL]];

Hiding Webview while streaming

For a online radio streaming app, I have the code below so far. The problem that I have is when I press play, a webview opens up and when I close it the stream stops. How do I make this webview not appear on the screen/do it in the background?
ViewController.h
#interface ViewController : UIViewController
{
IBOutlet UIWebView *webView;
}
-(IBAction)play:(id)sender;
ViewController.m
-(IBAction)play:(id)sender
{
NSString *stream = #"STREAMING LINK";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlrequest];
}
Also while using webview, would I be able to make a pause/stop button and a volume switch as well?
Forget Interface Builder, and do it programmatically.
UIWebView *webView = [[UIWebView alloc]init];
NSURL *url =[NSURL URLWithString:#"STREAMING LINK"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];

URL navigation bar in iOS webview

I created a simple webview that loads
a pre-defined website in iOS app.
NSURL *url =[NSURL URLWithString:#"http://jihadkawas.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Webview loadRequest:req];
how can i add a URL BAR, so that user can access any url? like normal browsers?
Thank you!
You need to have a Textfield (the URL-Bar where Users can type in the website's address.
UITextField *urlBarTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,320,40)];
[self.view addSubview:urlBarTextField];
And you need a go button or wait until the user pressed return on the Keyboard.
In the called method (either the Button target method or -(void)textField: shouldReturn:)
You need to load the new URL and display it in the UIWebView
NSURL *url = [NSURL URLWithString:urlBarTextField.text];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
That's it ;)
Just create a text field and a go button,
when the url presses the go button---
NSURL *url=[NSURL URLWithString:textField.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Webview loadRequest:req];
There you go

Resources