6

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

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

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

视图控制器.h

#import <UIKit/UIKit.h>

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

@end

视图控制器.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
4

1 回答 1

2

您可以实现以下 UIAlertViewDelegate 方法:

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

当 alertView 被解除时调用此方法,因此您可以在他的正文中加载本地资源作为后备。

在 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];
于 2012-10-15T14:42:25.210 回答