iOS 与 JS 交互

#import <UIKit/UIKit.h>

@interface EFOrderGift : UIView

@property (nonatomic, copy) NSString *strUrl;
@property (nonatomic, strong) UIWebView *webView;

+ (instancetype)efOrderGift;

@end


#import "EFOrderGift.h"

@interface EFOrderGift()<UIWebViewDelegate>

@property (nonatomic, assign) int count;

@end

@implementation EFOrderGift



- (instancetype)init {
    
    if (self = [super init]) {
        [self webView];
        
        self.count = 0;
    }
    return self;
}


- (UIWebView *)webView {
    
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.delegate = self;
        
        [self addSubview:_webView];
    }
    return _webView;
}

+ (instancetype)efOrderGift {
    
    return [[self alloc] init];
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
//    _webView.frame = CGRectMake(20, 80, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 160);
    
    [self preparUI];
}


- (void)preparUI {
    
    
    NSURL *url = [NSURL URLWithString:_strUrl];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    
}


#pragma mark - 代理方法 

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
 
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
}


//UIWebView如何判断 HTTP 404 等错误
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if ((([httpResponse statusCode]/100) == 2)) {
        // self.earthquakeData = [NSMutableData data];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        
        [ _webView loadRequest:[ NSURLRequest requestWithURL: url]];
        _webView.delegate = self;
    } else {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
                                  NSLocalizedString(@"HTTP Error",
                                                    @"Error message displayed when receving a connection error.")
                                                             forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
        
        if ([error code] == 404) {
            NSLog(@"xx");
            _webView.hidden = YES;
        }
        
    }
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   
    
    NSString *requestString = [[request URL] absoluteString];
    NSLog(@"requestString : %@",requestString);
    
    
    //判断是否是单击
    if ([requestString isEqualToString:@""] || [requestString isEqualToString:@""])
    {
//        NSURL *url = [request URL];
//        if([[UIApplication sharedApplication]canOpenURL:url])
//        {
//            [[UIApplication sharedApplication]openURL:url];
//        }
        
        [self removeFromSuperview];
        return NO;
    }
    
    return YES;
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    
    self.frame = newSuperview.frame;
    
    self.webView.frame = newSuperview.frame;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值