点击一个textView里的link导航至程序内可返回的自定义webView
来源:互联网 发布:单片机驱动led灯电路 编辑:程序博客网 时间:2024/06/06 18:13
id currentViewController;
在AppDelegate.m里
@implementation UIApplication (Private)
- (BOOL)customOpenURL:(NSURL*)url
{
beautyAppDelegate *MyWatcher = [[UIApplicationsharedApplication] delegate];
if (MyWatcher.currentViewController) {
[MyWatcher.currentViewControllerhandleURL:url];
return YES;
}
return NO;
}
@end
- (void)applicationDidBecomeActive:(UIApplication *)application {
Method customOpenUrl = class_getInstanceMethod([UIApplicationclass], @selector(customOpenURL:));
Method openUrl = class_getInstanceMethod([UIApplicationclass], @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"
typedefenum {
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 = [superinit]) )
{
backButton = [[UIBarButtonItemalloc] initWithImage:[UIImageimageNamed:@"back.png"]style:UIBarButtonItemStylePlaintarget:selfaction:@selector(goBack)];
forwardButton = [[UIBarButtonItemalloc] initWithImage:[UIImageimageNamed:@"forward.png"]style:UIBarButtonItemStylePlaintarget:selfaction:@selector(goForward)];
actionButton = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(doAction)];
toolbar = [UIToolbar new];
toolbar.barStyle =UIBarStyleDefault;
toolbar.tintColor = [UIColorlightGrayColor];
[toolbarsizeToFit];
CGFloat toolbarHeight = [toolbarframe].size.height;
CGRect mainViewBounds = self.view.bounds;
[toolbarsetFrame: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.viewaddSubview:webView];
[self.viewaddSubview:toolbar];
UIBarButtonItem *flexItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction: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.toolbarsetItems:items animated:NO];
[webViewloadRequest:[NSURLRequestrequestWithURL:url]];
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[superviewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[webViewstopLoading];
[UIApplicationsharedApplication].networkActivityIndicatorVisible =NO;
}
#pragma mark -
#pragma mark WebViewActions
- (void)reload
{
[webViewreload];
[selfupdateToolbar:BUTTON_STOP];
}
- (void)stop
{
[webViewstopLoading];
[selfupdateToolbar:BUTTON_RELOAD];
}
- (void) goBack
{
[webViewgoBack];
}
- (void) goForward
{
[webViewgoForward];
}
- (void) doAction
{
UIActionSheet *actionSheet = [[UIActionSheetalloc] initWithTitle:[self.urlabsoluteString]
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Open with Safari",nil];
[actionSheetshowInView: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([UIApplicationclass], @selector(customOpenURL:));
Method openUrl = class_getInstanceMethod([UIApplicationclass], @selector(openURL:));
method_exchangeImplementations(customOpenUrl, openUrl);
[[UIApplicationsharedApplication] openURL:self.url];
}
}
#pragma mark -
#pragma mark UIWebView
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return true;
}
- (void)webViewDidStartLoad:(UIWebView *)aWebView
{
[UIApplicationsharedApplication].networkActivityIndicatorVisible =YES;
[selfupdateToolbar:BUTTON_STOP];
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[UIApplicationsharedApplication].networkActivityIndicatorVisible =NO;
self.title = [aWebViewstringByEvaluatingJavaScriptFromString:@"document.title"];
[selfupdateToolbar:BUTTON_RELOAD];
self.url = aWebView.request.mainDocumentURL;
}
- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
[UIApplicationsharedApplication].networkActivityIndicatorVisible =NO;
}
-(void)updateToolbar:(ToolbarButton)button
{
NSMutableArray *items = [toolbar.itemsmutableCopy];
UIBarButtonItem *newItem;
if (button == BUTTON_STOP) {
UIActivityIndicatorView *activityView = [[UIActivityIndicatorViewalloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activityView startAnimating];
newItem = [[UIBarButtonItemalloc] initWithCustomView:activityView];
[activityView release];
}
else {
newItem = [[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefreshtarget:selfaction:@selector(reload)]autorelease];
}
[itemsreplaceObjectAtIndex:12 withObject:newItem];
[toolbarsetItems: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
{
[webViewrelease];
[urlrelease];
[toolbarrelease];
[backButtonrelease];
[forwardButtonrelease];
[actionButtonrelease];
[superdealloc];
}
@end
- 点击一个textView里的link导航至程序内可返回的自定义webView
- [Android]自定义可点击的TextView
- 点击返回的TextView
- Android之TextView------LINK的点击事件
- 实现TextView中link的点击效果
- FinestWebView:可自定义的WebView
- Android自动识别<a>标签的自定义TextView(可自定义点击事件)
- 自定义导航栏的返回按钮,扩大点击区域,不改变图片的尺寸比例,同时可设置title和image的位置关系
- 当webView作为recyclerView的一个布局,导致点击webView返回顶部的问题。
- 实现可点击展开的 TextView
- 实现可点击展开的 TextView
- 带动画的点击可展开TextView
- 实现可点击展开的 TextView
- TextView展示Html内可点击图片
- 自定义可点击的ImageSpan并在TextView中内置“View“
- 自定义可点击的ImageSpan并在TextView中内置“View“
- 自定义可点击的ImageSpan并在TextView中内置“View“
- 自定义可滑动、可点击的开关
- android在不加载图片的前提下获得图片的宽高
- html的常用标记
- servlet是什么? getRequestDispatcher()与sendRedirect()区别
- css ie6,7,8,firefox兼容解决方法
- 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
- 点击一个textView里的link导航至程序内可返回的自定义webView
- matlab ROI选取产生二进制mask
- 各种库编译方法
- 使用CRectTracker类进行对象动态定位
- Javascript的匿名函数
- 我的觉悟
- 黑马程序员 Java GUI
- CentOS里查看内存的使用
- myEclipse8.6 配置struts-2.2.3.1 (详细图文)