ShouldStartLoadWithRequest not being called in my iPhone app

I have a view controller with a UIWebView controller. I am trying to get javascript inside the html content of a web view to pass some information to my objective code. I came across a number of examples on the Internet that set window.location to a javascript function and then capture the event generated by setting the view controller as a delegate of the web view and intercepting it in the shouldStartLoadWithRequest function. Unfortunately, I cannot get it to work, because for me shouldStartLoadWithRequest is never called, although I set the web view delegate.

My code is as follows:

Interface:

@interface StoryTextViewController : UIViewController <UIWebViewDelegate>
{
    IBOutlet UIWebView *storyWebView;   
    NSString *information;
}

@property (nonatomic, retain) IBOutlet UIWebView *storyWebView;
@property (nonatomic, retain) NSString *information;

@end

* * , StoryTextViewController.

:

#import "StoryTextViewController.h"

@implementation StoryTextViewController

@synthesize storyWebView;
@synthesize information;

- (NSString *) contructHTMLText
{
    NSString *HTMLText = @"";

    NSArray *words = [information componentsSeparatedByString:@" "];
    NSString *header =
    @"<html>\n"
    "<head>\n"
    "<script type=\"text/javascript\">\n"

    "function marktext(theSpanID)\n"
    "{\n"
    "   var theSpan=document.getElementById(theSpanID);\n"
    "   if (theSpan.className == \"noHilite\")\n"
    "   {\n"
    "       theSpan.className = \"hilite\";\n"
    "       window.location = \"true\";\n"
    "   }\n"
    "   else\n"
    "   {\n"
    "       theSpan.className = \"noHilite\";\n"
    "       window.location = \"false\";\n"
    "   }\n"
    "}\n"

    "</script>\n"

    "<style type=\"text/css\">\n"

    ".hilite\n"
    "{\n"
    "   background-color:red;\n"
    "   color:white;\n"
    "   font: bold 60px arial,sans-serif\n"
    "}\n"

    ".noHilite\n"
    "{\n"
    "   background-color:white;\n"
    "   color:black;\n"
    "   font: 60px arial,sans-serif\n"
    "}\n"

    "</style>\n"

    "</head>\n"
    "<body>\n"
    "<div class=\"storyText\">\n";

    NSString *tailer = 
    @"</div>\n"
    "</body>\n"
    "</html>\n";

    HTMLText = [HTMLText stringByAppendingString:header];
    for (NSInteger i = 0; i < [words count]; i ++)
    {
        NSString *tag = [NSString stringWithFormat:@"   <span id=\"mytext%d\" class=\"noHilite\" onclick=\"marktext(\'mytext%d\')\">%@</span>&nbsp;\n", i, i, (NSString *)[words objectAtIndex:i]];
        HTMLText = [HTMLText stringByAppendingString:tag];
    }
    HTMLText = [HTMLText stringByAppendingString:tailer];
    NSLog(HTMLText);
    return HTMLText;
}

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        storyWebView.delegate = self;
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *HTMLData = [self contructHTMLText];
    [storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]];  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that not essential, such as cached data
}

- (void)dealloc {
    [super dealloc];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{   
    // handle event here    
    return true;
}

@end

, constructHTML * - html javascript, , , css high-lighting. , , - . , - , , , , , shouldStartLoadWithRequest, , .

, , , .

+3
4

if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    storyWebView.delegate = self;
}

, storyWebView .

:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *HTMLData = [self contructHTMLText];
    // empty statement
    // the trick is that if the view is not loaded from the .nib yet
    // it will be loaded and all connections established

    if (self.view);

    // here is setting delegate
    storyWebView.delegate = self;
    [storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]];  
}
+4

viewDidLoad, initWithNibName:bundle:. , nib - "" .

+1
  • window.location URL-, ? marktext true false. shouldStartLoadWithRequest , URL - . , , . URL-, FALSE shouldStartLoadWithRequest, URL- - . URL- .

  • SO. window.location.

0

I also see this problem because I installed

articleWeb.userInteractionEnabled = NO;`

you better check your code, install

articleWeb.userInteractionEnabled = YES;`
0
source

Source: https://habr.com/ru/post/1711763/


All Articles