UIWebView: переопределить действия, выполняемые при нажатии на ссылку - PullRequest
0 голосов
/ 02 декабря 2011

Я создал UIWebView и передаю ему локальный HTML-файл, который он прекрасно открывает. Страница состоит из очень большого изображения с несколькими точками доступа. Когда щелкают по одной из этих горячих точек, я хочу запускать пользовательские методы в своем коде. Я переопределил то, что я должен в моем коде (как вы можете видеть ниже), но - (BOOL) webView: ShouldStartLoadWithRequest .... не срабатывает. Я должен получить «Клик» в моей консоли, но ничего не происходит. Вот файлы .h и .m:

    //
    //  MapController.h

    #import <UIKit/UIKit.h>

    @interface MapViewController : UIViewController <UIWebViewDelegate>
    {
        IBOutlet UIWebView *webView;
        NSString *currentProgram;
    }

    @property (nonatomic, retain) IBOutlet UIWebView *webView;
    @property (nonatomic, retain) NSString *currentProgram;

    -(void)initWithProgram:(NSString *)program;
    -(void)stageClicked;
    @end

    //
    //  MapController.m

    #import "MapViewController.h"

    @implementation MapViewController

    @synthesize currentProgram, webView;

    -(void) initWithProgram:(NSString *)program
    {
        self.currentProgram = program;
    }

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        //GenerateURL
        NSMutableString *mapPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] mutableCopy];
        [mapPath appendString:[@"/" mutableCopy]];
        [mapPath appendString:[currentProgram mutableCopy]];
        [mapPath appendString:[@"/Map" mutableCopy]];
        NSMutableString *htmlFilePath = [mapPath mutableCopy];
        [htmlFilePath appendString:[@"/index.html" mutableCopy]];
        NSLog(@"Map Path: %@",mapPath);
        NSMutableString *HTMLData = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:nil];
        [mapPath replaceOccurrencesOfString:@"/" withString:@"//"options:0 range:NSMakeRange(0, [mapPath length])];
        [mapPath replaceOccurrencesOfString:@" " withString:@"%20"options:0 range:NSMakeRange(0, [mapPath length])];
        [self.webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",mapPath]]];
        //[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
            [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }

    - (void) viewWillAppear:(BOOL)animated
    {
        self.title = @"Map";


    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    //WebView Override

    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
    {
        NSLog(@"Click");
        NSURL *url = [request URL];
        if (![[url scheme] hasPrefix:@"Local"]) {
            [[UIApplication sharedApplication] openURL:url];
            return NO;
        }
        else{
            [self stageClicked];
        }
    return YES;
    }

    - (void) stageClicked {

        UIAlertView *nwAlert = [[UIAlertView alloc] initWithTitle:@"Info" 
                                                                                                              message:@"Link Clicked!" 
                                                                         delegate:self 
                                                        cancelButtonTitle:@"OK" 
                                        otherButtonTitles:nil];
[nwAlert show];
[nwAlert release];

    }

    @end

Может ли кто-нибудь объяснить, почему это происходит, или, если быть более точным, почему это не так? И конечно, как это исправить.

Спасибо

...