js调用oc方法 UIWebView 跳 到另一个UIWebView
1 // 2 // BusinessListViewController.h 3 // LouLiLouWai 4 // 5 // Created by jouhu on 15/7/31. 6 // Copyright (c) 2015年 jouhu. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface BusinessListViewController : UIViewController 12 @property (strong, nonatomic) NSString *urlString; 13 //@property (strong, nonatomic) NSString *titleString; 14 15 @end
1 // 2 // BusinessListViewController.m 3 // LouLiLouWai 4 // 5 // Created by jouhu on 15/7/31. 6 // Copyright (c) 2015年 jouhu. All rights reserved. 7 // 8 9 #import "BusinessListViewController.h" 10 #import "BusinessList2ViewController.h" 11 #import "MBProgressHUD.h" 12 13 @interface BusinessListViewController ()<UIWebViewDelegate> 14 @property (weak, nonatomic) IBOutlet UIWebView *webView; 15 @end 16 17 @implementation BusinessListViewController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view from its nib. 22 self.automaticallyAdjustsScrollViewInsets = NO; 23 self.edgesForExtendedLayout = UIRectEdgeNone; 24 25 UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init] ; 26 backBarButtonItem.title = @"" ; 27 self.navigationItem.backBarButtonItem = backBarButtonItem ; 28 29 self.webView.delegate = self; 30 31 NSURL *url = [NSURL URLWithString:self.urlString]; 32 NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 33 [self.webView loadRequest:urlRequest]; 34 } 35 36 - (void)viewWillAppear:(BOOL)animated 37 { 38 [super viewWillAppear:animated]; 39 40 // self.navigationItem.title = self.titleString; 41 42 // NSURL *url = [NSURL URLWithString:self.urlString]; 43 // NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 44 // [self.webView loadRequest:urlRequest]; 45 } 46 47 //- (void)viewDidDisappear:(BOOL)animated 48 //{ 49 // //清除缓存 50 // [self.webView loadHTMLString:@" " baseURL:nil]; 51 //} 52 53 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 54 { 55 NSString *urlString = [[request URL] absoluteString]; 56 urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 57 NSLog(@"url1String=%@",urlString); 58 59 // if ([urlString isEqualToString:@"about:blank"]) { 60 // return NO; 61 // } 62 63 NSArray *urlComps = [urlString componentsSeparatedByString:@"://"]; 64 65 if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@"protocol"]) 66 { 67 NSArray *arrFucnameAndParameter = [(NSString*)[urlComps objectAtIndex:1] componentsSeparatedByString:@";"]; 68 NSString *funcStr = [arrFucnameAndParameter objectAtIndex:0]; 69 70 if (1 == [arrFucnameAndParameter count]) 71 { 72 // 没有参数 73 if([funcStr isEqualToString:@"doFunc1"]) 74 { 75 /*调用本地函数1*/ 76 NSLog(@"doFunc1"); 77 } 78 } 79 else 80 { 81 //有参数的 82 if([funcStr isEqualToString:@"toActivity"]) 83 { 84 NSLog(@"doFunc2"); 85 [self toActivityWithUrl:[arrFucnameAndParameter objectAtIndex:1] title:[arrFucnameAndParameter objectAtIndex:2] share:[arrFucnameAndParameter objectAtIndex:3]]; 86 } 87 } 88 return NO; 89 } 90 return TRUE; 91 } 92 93 - (void)webViewDidStartLoad:(UIWebView *)webView 94 { 95 [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 96 } 97 98 - (void)webViewDidFinishLoad:(UIWebView *)webView 99 { 100 [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; 101 } 102 103 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 104 { 105 if ([error code] != NSURLErrorCancelled){ 106 [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; 107 UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil]; 108 [alterview show]; 109 } 110 } 111 112 - (void)toActivityWithUrl:(NSString *)urlString title:(NSString *)titleString share:(NSString *)shareString 113 { 114 BusinessList2ViewController *businessList2ViewController = [[BusinessList2ViewController alloc] initWithNibName:@"BusinessList2ViewController" bundle:[NSBundle mainBundle]]; 115 [businessList2ViewController setHidesBottomBarWhenPushed:YES]; 116 businessList2ViewController.urlString = [NSString stringWithFormat:@"http://%@",urlString]; 117 businessList2ViewController.navigationItem.title = titleString; 118 businessList2ViewController.shareString = shareString; 119 [self.navigationController pushViewController:businessList2ViewController animated:YES]; 120 } 121 122 - (void)didReceiveMemoryWarning { 123 [super didReceiveMemoryWarning]; 124 // Dispose of any resources that can be recreated. 125 } 126 127 /* 128 #pragma mark - Navigation 129 130 // In a storyboard-based application, you will often want to do a little preparation before navigation 131 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 132 // Get the new view controller using [segue destinationViewController]. 133 // Pass the selected object to the new view controller. 134 } 135 */ 136 137 @end
posted on 2015-08-03 16:59 codemaker313 阅读(276) 评论(0) 编辑 收藏 举报