Create UIWebView programmatically

40,177

Solution 1

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

Solution 2

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]; 
Share:
40,177
user1153798
Author by

user1153798

Updated on July 09, 2022

Comments

  • user1153798
    user1153798 almost 2 years

    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?

  • Sergey Kalinichenko
    Sergey Kalinichenko over 11 years
    @user1153798 if the code is in UIViewController, self.view should be fine. The .view may be wrong - try [self.view webview] instead (see the edit).
  • Ashvin A
    Ashvin A over 10 years
    I used static frame of web view, you can use according your requirement.