21

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

我在支持文件中编写了以下初始化函数:

- (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

4 回答 4

35

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

  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 的更新代码

迅速:

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)

目标 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];
于 2012-09-26T05:13:56.150 回答
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]; 
于 2012-09-25T15:43:05.897 回答
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];

在viewcontroller的接口中声明UIWebviewDelegate

于 2015-01-05T05:26:44.120 回答
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];
}
于 2019-05-14T09:00:21.250 回答