点击一个textView里的link导航至程序内可返回的自定义webView

来源:互联网 发布:单片机驱动led灯电路 编辑:程序博客网 时间:2024/06/06 18:13
1,在AppDelegate.h里定义一个 

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



原创粉丝点击