您的位置:首页 > 移动开发 > IOS开发

iOS 与 JS 交互

2016-03-01 14:21 447 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: