1,在AppDelegate.h里定义一个

id currentViewController;

在AppDelegate.m里

@implementation UIApplication (Private)

- (BOOL)customOpenURL:(NSURL*)url

{

beautyAppDelegate *MyWatcher = [[UIApplication sharedApplication] delegate];

if (MyWatcher.currentViewController) {

[MyWatcher.currentViewController handleURL:url];

return YES;

}

return NO;

}

@end

- (void)applicationDidBecomeActive:(UIApplication *)application {

Method customOpenUrl = class_getInstanceMethod([UIApplication class],@selector(customOpenURL:));

Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));

method_exchangeImplementations(openUrl, customOpenUrl);

}

在某个viewController里 AppDelegate.currentViewController = self;

在viewController里定义一个 -(void)handleURL:(NSURL*)url,在这个函数里加载一个自定义的webView;

当viewController里有某个链接url用户点击时就会回调AppDelegate的- (BOOL)customOpenURL:(NSURL*)url;

自定义的webView代码如下:

WebViewController.h里

#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController <UIActionSheetDelegate, UIWebViewDelegate> {

UIWebView *webView;

NSURL *url;

UIToolbar* toolbar;

UIBarButtonItem *backButton;

UIBarButtonItem *forwardButton;

UIBarButtonItem *actionButton;

}

@property (nonatomic, retain) UIWebView *webView;

@property (nonatomic, retain) NSURL *url;

@property (nonatomic, retain) UIToolbar* toolbar;

@property (nonatomic, retain) UIBarButtonItem *backButton;

@property (nonatomic, retain) UIBarButtonItem *forwardButton;

@property (nonatomic, retain) UIBarButtonItem *actionButton;

- (id) initWithURL:(NSURL*)u;

- (void) doAction;

- (void)goBack;

- (void)goForward;

- (void)reload;

- (void)stop;

@end

WebViewController.m里:

#import <objc/runtime.h>

#import "beautyAppDelegate.h"

#import "WebViewController.h"

typedef enum {

BUTTON_RELOAD,

BUTTON_STOP,

} ToolbarButton;

@interface WebViewController (Private)

- (void)updateToolbar:(ToolbarButton)state;

@end;

@implementation WebViewController

@synthesize webView;

@synthesize url;

@synthesize toolbar, backButton, forwardButton, actionButton;

- (id) initWithURL:(NSURL *)u

{

if ( (self = [super init]) )

{

backButton    = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"]style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];

forwardButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward.png"]style:UIBarButtonItemStylePlain target:self action:@selector(goForward)];

actionButton  = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:selfaction:@selector(doAction)];

toolbar           = [UIToolbar new];

toolbar.barStyle  = UIBarStyleDefault;

toolbar.tintColor = [UIColor lightGrayColor];

[toolbar sizeToFit];

CGFloat toolbarHeight = [toolbar frame].size.height;

CGRect mainViewBounds = self.view.bounds;

[toolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),

CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 2.0) + 2.0,

CGRectGetWidth(mainViewBounds),

toolbarHeight)];

webView                 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 380)];

webView.delegate        = self;

webView.scalesPageToFit = YES;

url = [u copy];

[self.view addSubview:webView];

[self.view addSubview:toolbar];

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *items = [NSArray arrayWithObjects: flexItem, backButton, flexItem, flexItem, flexItem,forwardButton,

flexItem, flexItem, flexItem, flexItem, flexItem, flexItem,

actionButton, flexItem, flexItem, flexItem,actionButton, flexItem, nil];

[self.toolbar setItems:items animated:NO];

[webView loadRequest:[NSURLRequest requestWithURL:url]];

}

return self;

}

- (void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

}

- (void)viewWillDisappear:(BOOL)animated

{

[webView stopLoading];

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

#pragma mark -

#pragma mark WebViewActions

- (void)reload

{

[webView reload];

[self updateToolbar:BUTTON_STOP];

}

- (void)stop

{

[webView stopLoading];

[self updateToolbar:BUTTON_RELOAD];

}

- (void) goBack

{

[webView goBack];

}

- (void) goForward

{

[webView goForward];

}

- (void) doAction

{

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[self.url absoluteString]

delegate:self

cancelButtonTitle:@"Cancel"

destructiveButtonTitle:nil

otherButtonTitles:@"Open with Safari", nil];

[actionSheet showInView:self.navigationController.view];

[actionSheet release];

}

- (void)actionSheet:(UIActionSheet *)as clickedButtonAtIndex:(NSInteger)buttonIndex

{

if (as.cancelButtonIndex == buttonIndex) return;

if (buttonIndex == 0) {

// swizzle methods, from here we want to open Safari

Method customOpenUrl = class_getInstanceMethod([UIApplication class],@selector(customOpenURL:));

Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));

method_exchangeImplementations(customOpenUrl, openUrl);

[[UIApplication sharedApplication] openURL:self.url];

}

}

#pragma mark -

#pragma mark UIWebView

- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

return true;

}

- (void)webViewDidStartLoad:(UIWebView *)aWebView

{

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

[self updateToolbar:BUTTON_STOP];

}

- (void)webViewDidFinishLoad:(UIWebView *)aWebView

{

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

self.title = [aWebView stringByEvaluatingJavaScriptFromString:@"document.title"];

[self updateToolbar:BUTTON_RELOAD];

self.url = aWebView.request.mainDocumentURL;

}

- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error

{

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

-(void)updateToolbar:(ToolbarButton)button

{

NSMutableArray *items = [toolbar.items mutableCopy];

UIBarButtonItem *newItem;

if (button == BUTTON_STOP) {

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

[activityView startAnimating];

newItem = [[UIBarButtonItem alloc] initWithCustomView:activityView];

[activityView release];

}

else {

newItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefreshtarget:self action:@selector(reload)] autorelease];

}

[items replaceObjectAtIndex:12 withObject:newItem];

[toolbar setItems:items animated:false];

[items release];

// workaround to change toolbar state

backButton.enabled = true;

forwardButton.enabled = true;

backButton.enabled = false;

forwardButton.enabled = false;

backButton.enabled = (webView.canGoBack) ? true : false;

forwardButton.enabled = (webView.canGoForward) ? true : false;

}

#pragma mark -

- (void)dealloc

{

[webView release];

[url release];

[toolbar release];

[backButton release];

[forwardButton release];

[actionButton release];

[super dealloc];

}

@end

本文转载至 http://blog.csdn.net/zhuangyou123/article/details/6936850

点击一个textView里的link导航至程序内可返回的自定义webView的更多相关文章

  1. TextView里的文 html

    一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...

  2. 为Textview里面的ImageSpan添加点击响应事件

    对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...

  3. Android TextView 嵌套图片及其点击,TextView 部分文字点击,文字多颜色

    1. TextView 中嵌套图片的方法 TextView textView... textView.setText("..."); textView.append(Html.fr ...

  4. Android TextView里显示两种颜色

    今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...

  5. 高效快捷解决一个TextView显示多种字体的控件SpannableTextView

    这个控件本人强烈推荐,它会使得布局非常的简单且高效: 下面这个布局如果是你,你会用多少层?多少控件生成? 告诉你吧,一个SpannableTextView控件就搞定了! 它把TextView和Span ...

  6. Android 获取imageview的图,在另一个imageview里显示。

    当我点击默认头像里的其中一个然后在点确定就在最上面的那个imageview里显示选择的头像.求大神. img1和img2都是ImageView,要把img1中的图片显示到img2中 前景(对应src属 ...

  7. 【移动端debug-6】如何做一个App里的web调试小工具

    原文链接:如何做一个App里的web调试小工具 我们知道现在hybrid app非常流行,在这样的app里,h5页面是应用非常广泛的.相对于以往在pc端开发的网页,放在app里的网页由于无法直接使用桌 ...

  8. 浅谈android中只使用一个TextView实现高仿京东,淘宝各种倒计时

    今天给大家带来的是只使用一个TextView实现一个高仿京东.淘宝.唯品会等各种电商APP的活动倒计时.近期公司一直加班也没来得及时间去整理,今天难得歇息想把这个分享给大家.只求共同学习,以及自己兴许 ...

  9. Android 一个TextView中设置多种不同大小的字体,设置超链接

    以前项目中要是遇到这样的UI设计,都是傻不拉唧的分为三个TextView来实现,今天在微信中无意中看了一篇公众号文章,发现原来只要一个TextView就可以搞定啦,人生最悲哀的事情莫过于工作了这么久啦 ...

随机推荐

  1. VBA学习笔记(4)--数组和单元格互相转换

    说明(2017.3.23): 1. VBA的数组还是很难用的,其实就是非常难用! 2. 要先定义一个数组,可以是空的,也可以里面写个数字作为数组长度. 3. 如果是空数组,可以后面redim重新定义数 ...

  2. draw sin

    draw sin Steps 导入包 生成X轴,Y轴的数据点 设置输出图大小,像素,前景色 指定线宽,线型,绘制曲线 设置坐标轴范围 显示图形. Code #!/usr/bin/env python ...

  3. # rp2833板卡更新u-boot.bin的步骤

    1 建立tftpserver,并验证tftpserver的正确性(切记),并将PC主机网址设置192.168.18.105: 2 将u-boot-am.bincopy到tftpserver的目录下,并 ...

  4. MySql 生成日期随机数

    select DATE_ADD(sd, INTERVAL FLOOR(1+ RAND() * ((ABS(UNIX_TIMESTAMP(ed) - UNIX_TIMESTAMP(sd))) - 1)) ...

  5. 可能是目前最完整的前端框架 Vue.js 全面介绍

    Vue.js 是一个JavaScriptMVVM库,是一套构建用户界面的渐进式框架. 摘要 2016年最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angul ...

  6. 数据库 proc编程九

    第一种动态sql EXEC SQL EXECUTE IMMEDIATE :psql; .仅适用于非select语句 .嵌入SQL语句中不能包含输入宿主变量 void main() { EXEC SQL ...

  7. 查询相应id下的数据

    ---恢复内容开始--- u方法这样的:带不起模板引擎 <a href="{:U('Del/wzxg','','')}/{$vo.id}">修改</a> 这 ...

  8. 关于Cocos2d-x事件处理机制

    事件处理步骤: 1.创建一个触摸事件监听器(单点触摸或多点触摸) 2.实现触摸事件的响应方法 3.添加事件监听器(场景优先或固定值优先) 4.当用户触摸时,事件分发器就会将事件分发给监听器进行响应 首 ...

  9. 第三百零一节,python操作redis缓存-管道、发布订阅

    python操作redis缓存-管道.发布订阅 一.管道 redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pi ...

  10. python json.dumps 中文字符乱码

      场景:微信公众号推送消息,中文乱码.  Date:2017-05-11 10:58:40.033000,\u4f60\u597d    解决方法: python dumps默认使用的ascii编码 ...