I've been trying this for a while, but I'm not getting it right.
I have written the following init function in a supporting file:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
}
return self;
}
and following in ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *view = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[view loadRequest:nsrequest];
}
I also tried creating the webview in didFinishLaunchingWithOptions:
method of appDelegate, but it also didn't work.
What is the correct way?
Difference Between UIWebview and WKWebViewUIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.
A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.
Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.
Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url. let url = NSURL (string: "https://www.simplifiedios.net"); let request = NSURLRequest(URL: url!); webView. loadRequest(request);
I hope this solution will help you.
- (void)viewDidLoad
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; NSString *urlString = @"https://www.google.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; webView loadRequest:request]; [self.view addSubview:webView];
I have used the static frame of the webView, you can use according to your requirement.
UIWebView
is deprecated: first deprecated in iOS 12.0 - No longer supported; please adopt WKWebView
, so updated code for Objective C and Swift are belowSwift:
import WebKit
let theConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: view.frame, configuration: theConfiguration)
let nsurl = URL(string: "https://www.google.com")
var nsrequest: URLRequest? = nil
if let nsurl = nsurl {
nsrequest = URLRequest(url: nsurl)
}
if let nsrequest = nsrequest {
webView.load(nsrequest)
}
view.addSubview(webView)
Objective C:
#import <WebKit/WKWebView.h>
#import <WebKit/WKWebViewConfiguration.h>
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURL *nsurl=[NSURL URLWithString:@"https://www.google.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
It looks like you have forgotten to add webview
as a subview of its parent view:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
[self addSubview:webview];
}
return self;
}
Also viewDidLoad
is not the right place to create subviews. You should expose webview
as a property of your view, and then access it from viewDidLoad
, like this:
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[[self.view webview] loadRequest:nsrequest];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With