简体   繁体   English

使用 UIwebView 加载 URL 时出现问题

[英]Problem loading URL with UIwebView

I have what is quite a simple app at the moment that i am trying to get to load a url in a UIwebView.目前我有一个非常简单的应用程序,我试图在 UIwebView 中加载 url。 However all I get is a blank page and from what I can tell its just not trying to load the url at all.然而,我得到的只是一个空白页,据我所知,它根本没有尝试加载 url。

I have posted the code i added from my.h and.m files below to let you guys see what is going on.我已经发布了我从下面的 my.h 和 .m 文件中添加的代码,让你们看看发生了什么。 It does actually load the web view just not the url.它实际上加载了 web 视图而不是 url。

Hope I have explained this ok.希望我已经解释过了。

Thanks谢谢

The.h file .h 文件

@interface StaffsAirsoftAppViewController : UIViewController {

IBOutlet UIWebView *webView;
IBOutlet UIActivityIndicatorView *active;


}

The.m file .m 文件

-(void)viewDidLoad {

[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkLoad) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkNotLoad) userInfo:nil repeats:YES];


}

-(void)checkLoad {

if(webView.loading) {
    [active startAnimating];
}

}

-(void)checkNotLoad {
if(!(webView.loading)) {
    [active stopAnimating];



}

You should use the webview delegate methods for the same.您应该使用相同的webview 委托方法。

– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:

In addition to Preveen's suggestions for using the delegate, you could also check to verify that the UIWebView is visible on the screen.除了 Preveen 对使用委托的建议之外,您还可以检查以验证 UIWebView 在屏幕上是否可见。 If you just create a UIWebView, but don't add it to the view hierarchy (either in code or in a nib file), then the user will never see it.如果您只是创建一个 UIWebView,但不将其添加到视图层次结构中(无论是在代码中还是在 nib 文件中),那么用户将永远看不到它。 This could also be a problem if you forgot to add your view controller's view as a subview of the app's window, for example.例如,如果您忘记将视图控制器的视图添加为应用程序 window 的子视图,这也可能是一个问题。

A quick way to check this is to do:一种快速检查方法是:

webView.backgroundColor = [UIColor redColor];

in the viewDidLoad method.viewDidLoad方法中。 Now, even if the page doesn't load, you should be seeing red.现在,即使页面没有加载,您也应该看到红色。 If not, your view hierarchy is not set up correctly.如果没有,您的视图层次结构设置不正确。 One way to see what the view hierarchy actually is is to use code like this in your application delegate:查看视图层次结构实际上什么的一种方法是在应用程序委托中使用如下代码:

- (void)printViewHierarchy {
  NSLog(@"%@", [self.window recursiveDescription]);  // Ignore compiler warning.
}

// (inside your didFinishLaunching... method:
  [self performSelector:@selector(printViewHierarchy)
             withObject:nil
             afterDelay:1.0];

The reason for calling that method with delay 1.0 is to give the nib file a chance to load - they perform asynchronous loading, so if you print out the view hierarchy immediately, it will probably not be complete yet.使用延迟 1.0 调用该方法的原因是让 nib 文件有机会加载 - 它们执行异步加载,因此如果您立即打印出视图层次结构,它可能还没有完成。 After 1sec (less than that really) it will be set up, so you can rely on that printed-out value to see what your view hierarchy looks like. 1 秒后(实际上比这还短),它将被设置,因此您可以依靠该打印输出的值来查看您的视图层次结构是什么样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM