Xcode中的网络错误警报急需

时间:2012-07-21 18:55:34

标签: ios xcode alert connect

我最近开始了一个Xcode项目,它主要是基于Web的,从某种意义上说它主要与UIWebView一起使用。我是一个完全的菜鸟。我想做一个“没有互联网连接警报”。如果您没有互联网连接,基本上会出现警报。我尝试过使用Reachability但是因为我是一个菜鸟,所以我没有设法解决任何问题。继承人我的视图控制器: 这是我的.h文件:P.S:我的WebView被称为'webone'。

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

-(IBAction)refreshClicks:(id)sender;

@property (weak, nonatomic) IBOutlet UIWebView *webone;
@end

这是我的.m文件:

#import "FirstViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize webone;


-(void)viewDidLoad {
    NSURL *url = [NSURL URLWithString:@"http://www.lostcraft.net/mobile"]; 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
    [webone loadRequest:req];    
    [super viewDidLoad];
}

-(void)awakeFromNib{  //IGNORE
    [self refreshClicks:self]; //IGNORE
}

-(IBAction)refreshClicks:(id)sender{//IGNORE
    [webone loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.lostcraft.net/mobile"]]];//IGNORE
}

- (void)viewDidUnload
{
    [self setWebone:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

1 个答案:

答案 0 :(得分:1)

将其放在.m文件中

在您的IBAction下:

NSString *web = @"http://YOUR WEB ADRESS HERE";
                 NSURL *url = [NSURL URLWithString:web];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webdone loadRequest:requestUrl];

然后在.m

中的某个地方
-(void)webView:(UIWebView *)webdone didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check     your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}  

进入xcode

中界面构建器的连接选项卡

enter image description here

右键单击代理,如图所示(顶部选项),然后将其拖到视图控制器中,您可以在其中显示Web视图(webdone)

enter image description here