以编程方式创建UIWebView

21

我已经尝试了一段时间,但是我还没有弄对。

我在一个支持文件中编写了以下的init函数:

- (id)initWithFrame:(CGRect)frame
 {
    self = [super initWithFrame:frame];
    if (self) {
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
 }
    return self;
}

并在 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]; 
}

我也尝试在appDelegate的didFinishLaunchingWithOptions:方法中创建webview,但它也没有起作用。

正确的方法是什么?

4个回答

36

我希望这个解决方案能够帮到你。

  1. 根据你的问题,只需在 - (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];

我已经使用了webView的静态框架,您可以根据您的需求使用它。

  1. 但是UIWebView已被弃用:自iOS 12.0起已被弃用-不再受支持,请采用WKWebView,因此下面是Objective C和Swift的更新代码:

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

我使用了Web视图的静态框架,您可以根据自己的需求进行使用。 - Ashvin

4

看起来您忘记将webview作为其父视图的子视图添加:

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
        [self addSubview:webview];
    }
    return self;
}

另外,viewDidLoad不是创建子视图的正确位置。您应该将webview公开为视图的属性,然后从viewDidLoad访问它,像这样:

NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[[self.view webview] loadRequest:nsrequest]; 

如果代码在 UIViewController 中,那么 self.view 应该没问题。.view 可能是错误的 - 可以尝试使用 [self.view webview](参见编辑)。 - Sergey Kalinichenko

0
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
webView.delegate = self;   
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest]; 
[self.view addSubview:webview];

在视图控制器的接口中声明UIWebviewDelegate。

0

这个答案是对@Ashvin Ajadiya的答案的轻微改进。为了根据您的设备动态设置Web视图的宽度和高度,请在viewWillAppear中使用此代码,否则它将无法获得正确的设备宽度和高度。

-(void)viewWillAppear:(BOOL)animated
{
    UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url=@"http://www.google.com";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];
    [self.view addSubview:webview];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接