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

js调用oc方法 UIWebView 跳 到另一个UIWebView

2015-08-03 16:59 405 查看
//
//  BusinessListViewController.h
//  LouLiLouWai
//
//  Created by jouhu on 15/7/31.
//  Copyright (c) 2015年 jouhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BusinessListViewController : UIViewController
@property (strong, nonatomic) NSString *urlString;
//@property (strong, nonatomic) NSString *titleString;

@end


//
//  BusinessListViewController.m
//  LouLiLouWai
//
//  Created by jouhu on 15/7/31.
//  Copyright (c) 2015年 jouhu. All rights reserved.
//

#import "BusinessListViewController.h"
#import "BusinessList2ViewController.h"
#import "MBProgressHUD.h"

@interface BusinessListViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

@implementation BusinessListViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init] ;
backBarButtonItem.title = @"" ;
self.navigationItem.backBarButtonItem = backBarButtonItem ;

self.webView.delegate = self;

NSURL *url = [NSURL URLWithString:self.urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

//    self.navigationItem.title = self.titleString;

//    NSURL *url = [NSURL URLWithString:self.urlString];
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//    [self.webView loadRequest:urlRequest];
}

//- (void)viewDidDisappear:(BOOL)animated
//{
//    //清除缓存
//    [self.webView loadHTMLString:@" " baseURL:nil];
//}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlString = [[request URL] absoluteString];
urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"url1String=%@",urlString);

//    if ([urlString isEqualToString:@"about:blank"]) {
//        return NO;
//    }

NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];

if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@"protocol"])
{
NSArray *arrFucnameAndParameter = [(NSString*)[urlComps objectAtIndex:1] componentsSeparatedByString:@";"];
NSString *funcStr = [arrFucnameAndParameter objectAtIndex:0];

if (1 == [arrFucnameAndParameter count])
{
// 没有参数
if([funcStr isEqualToString:@"doFunc1"])
{
/*调用本地函数1*/
NSLog(@"doFunc1");
}
}
else
{
//有参数的
if([funcStr isEqualToString:@"toActivity"])
{
NSLog(@"doFunc2");
[self toActivityWithUrl:[arrFucnameAndParameter objectAtIndex:1] title:[arrFucnameAndParameter objectAtIndex:2] share:[arrFucnameAndParameter objectAtIndex:3]];
}
}
return NO;
}
return TRUE;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if ([error code] != NSURLErrorCancelled){
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription]  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
[alterview show];
}
}

- (void)toActivityWithUrl:(NSString *)urlString title:(NSString *)titleString share:(NSString *)shareString
{
BusinessList2ViewController *businessList2ViewController = [[BusinessList2ViewController alloc] initWithNibName:@"BusinessList2ViewController" bundle:[NSBundle mainBundle]];
[businessList2ViewController setHidesBottomBarWhenPushed:YES];
businessList2ViewController.urlString = [NSString stringWithFormat:@"http://%@",urlString];
businessList2ViewController.navigationItem.title = titleString;
businessList2ViewController.shareString = shareString;
[self.navigationController pushViewController:businessList2ViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: