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
相关文章推荐
- 17个提升iOS开发效率的必用工具
- iOS 中英文字符串混排的字符串,测试长度
- IOS开发 strong,weak,retain,assign,copy nomatic 等的区别与作用
- iOS开发--沙盒路径与操作文件
- [iOS]从相册获取以及用相机拍摄视频并缓存到沙盒
- iOS开发打电话的功能
- Masonry框架
- iOS内存管理之自动释放池延迟销毁对象
- iOS-数组,字典常用的字面量写法
- iOS 之可穿戴设备开发之蓝牙4.0
- iOS下的Masonry适配
- iOS应用崩溃日志分析
- iOS-更新CocoaPods出现错误 提示重复文件
- iOS 证书失效,更换WWDR试一下,吼吼
- iOS文件目录
- iOS开发中如何合理地制造BUG
- iOS 图片点击放大封装
- iOS-常用宏定义
- ios签名机制的理解
- iOS-.pch如何使用