Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create UIWebView programmatically

Tags:

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?

like image 626
user1153798 Avatar asked Sep 25 '12 15:09

user1153798


People also ask

What is use of UIWebview or WKWebView?

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.

What is WKWebView?

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.

How do I use WKWebView?

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.

How do I load a web view in Swift?

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);


2 Answers

I hope this solution will help you.

  1. As per your problem, simply add these lines in - (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.

  1. But UIWebView is deprecated: first deprecated in iOS 12.0 - No longer supported; please adopt WKWebView, so updated code for Objective C and Swift are below

Swift:

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];
like image 88
Ashvin A Avatar answered Oct 30 '22 16:10

Ashvin A


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]; 
like image 26
Sergey Kalinichenko Avatar answered Oct 30 '22 15:10

Sergey Kalinichenko