How to create a webview with only code - ios

We are trying to create a simple app in xcode 5 targeting ios 7. We want to create a simple webview but we do not care to use the storyboard to set the whole thing up.
Is it possible to create a webview (and set the proportions/position) by only using a code snippet?
Thanks.

You just need to create a UIWebView instance and add it to a container view. You can defined the container view in code, a nib, or a storyboard.
CGRect frame = CGRectMake(0, 0, 100, 100); // Give the frame a position and size
UIWebView *webView = [UIWebView alloc] initWithFrame:frame];
[containerView addSubView:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL urlWithString:#"http://www.google.com"]]];

Related

WKWebView show content wrong frame in old project

I have an old iOS project using Objective C. When I add a WKWebView to a ViewController
NSURL *url = [NSURL URLWithString:#"https://www.google.com"];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:theConfiguration];
[self.view addSubview:_webView];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[
[self.webView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[self.webView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
[self.webView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
[self.webView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
]];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
If I install app via Xcode with iOS 14, webview works fine, but after I disconnect Xcode and open app directly, webview show content with wrong frame. If I build app in iOS 12.5 via Xcode, web view also show content with wrong frame.
If I create new project from Xcode 12.5, and using same code, it works fine.
This is screenshot when open app via Xcode build
https://i.stack.imgur.com/rYb3M.png
And this is screenshot when open app directly:
https://i.stack.imgur.com/4q0uD.png
A few observations -
This code uses a mix of _webView and self.webView. For clarity, you should unify the usage for at least this code block.
Where exactly are you calling this part? viewDidLoad() or After some event? Can you try adding a layoutIfNeeded() call after you add this?
/// After activating constraints
[self.view layoutIfNeeded];
You should try to use leadingAnchor & trailingAnchor instead of leftAnchor & rightAnchor respectively.
If nothing helps, I think your view controller's view storyboard setup isn't updated to use Autolayout. In that case, you can try this -
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:theConfiguration];
_webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:_webView];
Please note that in this case - you MUST REMOVE all the constraints related code.
/*
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[ ...... ]];
*/
After spending a few days searching, I finally found the cause of the error.
In a UIView Extension file overridden center property
#import <UIKit/UIKit.h>
#interface UIView (FrameExtension)
#property CGPoint center;
#end
.
#import "UIView+FrameExtension.h"
#implementation UIView (FrameExtension)
- (CGPoint)center {
return CGPointMake(self.frame.origin.x + self.frame.size.width / 2,
self.frame.origin.y + self.frame.size.height / 2);
}
- (void)setCenter:(CGPoint)center {
CGRect frame = self.frame;
frame.origin = CGPointMake(center.x - self.frame.size.width / 2,
center.y - self.frame.size.height / 2);
self.frame = frame;
}
#end
Somehow, when loading the webview, it called setCenter which caused the error.

Recieved memory warning issue in webview add inside UIScrollview?

I have tried to load 40 UIWebview in uiscrollview. user scroll more than 5. Recieved memory warning alert comes continuously, finally application quit.
How to manage this memory issue, please let me know.
Thanks in Advance
I tried this:
Description: Load one by one webview are added in UIScrollview because i have tried to load all webview using for loop single time but app crash in this for loop so doing add one by one process.
CGRect vFrame = self.view.frame;
UIWebView *wViewLocal = [[UIWebView alloc] initWithFrame:CGRectMake(vFrame.origin.x, vFrame.origin.y, vFrame.size.width, 900)];
wViewLocal.delegate = self;
wViewLocal.backgroundColor = [UIColor grayColor];
NSString *fileUrl = [[subSubChap objectAtIndex:selectedIndex] stringByReplacingOccurrencesOfString:#".htm" withString:#""];
[wViewLocal fileUrl baseURL:nil];
[wViewLocal setScalesPageToFit:YES];
[wViewLocal setContentMode:UIViewContentModeScaleToFill];
[wViewLocal setFrame:CGRectMake(selectedIndex*viewRect.size.width,0,768, 900)]; // here set X-Value each webview.
[wViewLocal setTag:selectedIndex];
[scroll_View addSubview:wViewLocal]; // Here i have add UIWebview
[self.array_MainObject addObject:wViewLocal]; // Here i have add all UIWebview in NSMutableArray for get particular webview info.
[wViewLocal release];

UIWebview full screen size

I am trying to display a pdf in a UIWebview using the entire screen of the device, except the status bar and top bar. So far, I created IBOutlet UIWebview *webview; in my .h file. I connected the webview in my storyboard and wrote the following code in my .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[webview setBackgroundColor:[UIColor clearColor]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Chart" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];
[self.view addSubview:webview];
}
The pdf displays correctly in my 3.5 inch, but in my 4 inch display, the bottom is cut off. This is even more noticeable when I rotate to landscape view. About 33% of the screen is not used at all. How can I fix this? I know it must have something to do with the UIWebview dimensions in my code above (webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];), but is there a better way to simply display the webview on the entire screen, without specifying a specific screen height and width?
One cheap thing to do would be to set the frame of the webview in overriding - (void)viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
the advantage of doing it here, is that the size and orientation of the view has already been determined here, and you can get an accurate measurement.
Using Auto Layout
Be sure the "Constrain to margins" option is not checked, all constrain values are 0:
And your webview is a first child of your main view:
Without Auto Layout
Change webview autosizing connections:
After a lot of headache dealing with compatibility of iOS6, 7 and different screen sizes, I use this one:
- (void)viewDidLoad {
[super viewDidLoad];
self.webview = [[UIWebView alloc] init];
// More setting your webview here
// Set webview to full screen
self.view = self.webview;
[self.webview loadRequest:aRequest];
}
I was able to get this working correctly for me by selecting the Reset to Suggested Constraints under the Resolve Auto Layout Issues menu.

UIWebView shows a black screen with no content

I've created a UIWebView from the IB and subclassed it to my needs. Somewhere inside my app I'm using the following code:
_mainWebView = nil;
_mainWebView = [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
_mainWebView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_mainWebView loadRequest:request];
NSLog(#"WebView: %#", _mainWebView); //->> WebView: <MyWebView: 0x15403000; baseClass = UIWebView; frame = (0 0; 320 548); layer = <CALayer: 0x1544dac0>>
Which gives me a black screen with no content.
It's most likely because you're setting _mainWebView to nil. This pretty much means you "unplug" the outlet you hooked up from Interface Builder. So the web view you're configuring in this code isn't connected to the screen at all. There are two options you can choose from:
1) Use Interface Builder, connect the outlets, and remove the first two lines of code in the snippet above.
2) Remove the web view from Interface Builder, remove the first line of code in the snippet above, and add [self.view addSubview:_mainWebView]; before you load the request.
By the way, I believe UIWebView is not to be subclassed, at least in an App Store release. It could be potentially breakable in future iOS's...
I hope this helps you!
_mainWebView = [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
Above line of code tell that you create a new MyWebView, all hook up from IB will disconnect here.
And you can show it by:
[self.view addSubview:_mainWebView];
If you draw and hook up from IB, simply comment out init a new one
I faced the same issue,
What my issue was, the web view that I placed on the storyboard is crossing the borders of the view controller.
When I pulled it back into the view controller it is working fine.

scrollView with webView not scrolling horizontally

I'm trying to create an app to display various document formats and have the following code in a ViewController.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768 , 1024)];
[self.view addSubview:scrollView];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768 , 1024)];
[scrollView addSubview:webView];
[webView loadRequest:service.urlRequest];
}
The trouble is, the app is only to be used in portrait mode and one of the Word documents is 1024 wide. I'd like to be able to scroll horizontally to see the whole document but this doesn't work.
I think I need to implement some setting on the scrollview but so far haven't been able to figure out what it is.
I've tried setting the width of both the scrollView and webView widths to 1024 and scrollView.contentSize.width but appear to be barking up the wrong tree.
I've also tired self.scrollView.contentSize = CGSizeMake(1024, 1024); but feel as though I'm just trying various things in an almost random manner to see if one works.
Can anyone tell me how to implement this?
WebView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Resources