【问题标题】:Xcode & WebView: Load local html if no internet connection (fallback)Xcode 和 WebView:如果没有 Internet 连接,则加载本地 html(后备)
【发布时间】:2012-10-15 13:46:35
【问题描述】:

目前我正在使用 UIWebView 为 iOS 编写应用程序。我的目标是使用 WebView 显示一个 php 站点(来自我的网络服务器)。我很擅长 HTMl、CSS、JS 和 PHP,但 Object C 不是我的强项。但是我设法实现了一切,我的目标是现在(当 iOS 没有互联网连接时)在错误警报之后显示本地文件而不是服务器上的文件。使用 Google 后,我设法独立完成,但不是作为后备。

现在它显示警报,但点击确定后,用户会得到一个空白页面。不是很友好:(在本地 html 文件中,我可以实现一种“刷新按钮”。如果您有(更好的?)解决方案,我会很高兴。谢谢!

我的系统:OS X 10.8.2 上的 Xcode 4.5.1

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loadingSign;
- (void)loadSite;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize webView;
@synthesize loadingSign;

-(void) webViewDidStartLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.loadingSign startAnimating];
    self.loadingSign.hidden = NO;
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.loadingSign stopAnimating];
    self.loadingSign.hidden = YES;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.loadingSign stopAnimating];
    self.loadingSign.hidden = YES;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung" message:@"Bitte verbinde dich mit dem Internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)loadSite
{
    NSString *fullURL = @"http://website.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    webView.scrollView.bounces = NO;
    webView.delegate = self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self loadSite];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

  • 发生错误时加载本地html(需要检查错误原因)从-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

标签: objective-c ios c xcode


【解决方案1】:

您可以实现以下 UIAlertViewDelegate 方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

这个方法在 alertView 被解除时被调用,所以你可以在他的 body 中加载你的本地资源作为后备。

在你的 ViewController 的界面中你应该添加:

@interface ViewController : UIViewController <UIWebViewDelegate,UIAlertViewDelegate>

当你分配你的 alertView 时,将委托设置为 self。

我的意思是:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung" message:@"Bitte verbinde dich mit dem Internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

【讨论】:

  • 感谢您的快速解决方案。它完美地工作。但我不明白你的最后一句话......
猜你喜欢
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2019-11-01
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
相关资源
最近更新 更多