IOS如何在一个界面嵌入一个网页

来源:互联网 发布:数据库管理员怎么样 编辑:程序博客网 时间:2024/06/05 08:19

1.判断当前是否可以使用网络

+(BOOL) isConnectionAvailable

{
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            //NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //NSLog(@"3G");
            break;
    }
    
    return isExistenceNetwork;

}

2.如果网络可用就可以把网页加载进来

-(void)creatWebView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
webView.delegate = (id)self;
webView.scalesPageToFit = YES ;
webView.scrollView.scrollEnabled = YES;
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",live800URL]]];
[self.view addSubview:webView];
[webView loadRequest:request];
}

3.如果网络不可用用加载一张网络不用用的图片并添加事件处理函数


-(void) createErrorView
{
self.wifiImageView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
self.wifiImageView.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"wifi"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
imageView.center = CGPointMake(self.view.frame.size.width/2, self.wifiImageView.frame.size.height/2);
[self.wifiImageView addSubview:imageView];
[self.view addSubview:self.wifiImageView];
self.wifiImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reFreshNetwork)];
[self.wifiImageView addGestureRecognizer:singleTap1];
}

事件处理函数

-(void) reFreshNetwork{

if ([ConnectNetwork isConnectionAvailable])
{
[self.wifiImageView removeFromSuperview];
[self creatWebView];
}else
{
[self createErrorView];
}

}

0 0
原创粉丝点击