Javascript 我的iPhone应用程序中未调用shouldStartLoadWithRequest

Javascript 我的iPhone应用程序中未调用shouldStartLoadWithRequest,javascript,iphone,objective-c,cocoa-touch,event-handling,Javascript,Iphone,Objective C,Cocoa Touch,Event Handling,我有一个带有UIWebView控制器的视图控制器。我试图让javascript包含在web视图的html内容中,以便将一些信息传递给我的目标c代码。我在网上遇到了许多示例,这些示例在javascript函数中设置window.location,然后通过将视图控制器设置为web视图的委托并在shouldStartLoadWithRequest函数中捕获它来捕获生成的事件。不幸的是,我无法让它工作,因为对我来说,shouldStartLoadWithRequest从未被调用,即使我正在设置web视图

我有一个带有UIWebView控制器的视图控制器。我试图让javascript包含在web视图的html内容中,以便将一些信息传递给我的目标c代码。我在网上遇到了许多示例,这些示例在javascript函数中设置window.location,然后通过将视图控制器设置为web视图的委托并在shouldStartLoadWithRequest函数中捕获它来捕获生成的事件。不幸的是,我无法让它工作,因为对我来说,shouldStartLoadWithRequest从未被调用,即使我正在设置web视图的委托

我的代码如下:

界面:

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

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

@end
@interface StoryTextViewController:UIViewController
{
ibuiwebview*故事webview;
NSString*信息;
}
@属性(非原子,保留)IBUIWebView*storyWebView;
@属性(非原子,保留)NSString*信息;
@结束
在界面中,*信息变量由调用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's 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
#导入“StoryTextViewController.h”
@StoryTextViewController的实现
@综合故事网络视图;
@综合信息;
-(NSString*)构造HTMLText
{
NSString*HTMLText=@;
NSArray*words=[由字符串分隔的信息组件:@”“;
NSString*标题=
@“\n”
“\n”
“\n”
“函数标记文本(窗格ID)\n”
“{\n”
“var theSpan=document.getElementById(theSpan);\n”
“如果(theSpan.className=\'noHilite\”)\n
“{\n”
“theSpan.className=\“hilite\”;\n”
“window.location=\“true\”;\n”
“}\n”
“否则\n”
“{\n”
“theSpan.className=\“noHilite\”;\n”
“window.location=\“false\”;\n”
“}\n”
“}\n”
“\n”
“\n”
“.hilite\n”
“{\n”
“背景色:红色;\n”
“颜色:白色;\n”
字体:粗体60px arial,无衬线\n
“}\n”
“.noHilite\n”
“{\n”
“背景色:白色;\n”
“颜色:黑色;\n”
字体:60px arial,无衬线\n
“}\n”
“\n”
“\n”
“\n”
“\n”;
NSString*tailer=
@“\n”
“\n”
“\n”;
HTMLText=[HTMLText stringByAppendingString:header];
对于(NSInteger i=0;i<[字数];i++)
{
NSString*标记=[NSString stringWithFormat:@“%@\n”,i,i,(NSString*)[words objectAtIndex:i]];
HTMLText=[HTMLText stringByAppendingString:tag];
}
HTMLText=[HTMLText stringByAppendingString:tailer];
NSLog(HTMLText);
返回HTMLText;
}
//指定的初始值设定项。覆盖以执行加载视图之前所需的设置。
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
if(self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){
storyWebView.delegate=self;
}
回归自我;
}
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad
{
[超级视图下载];
NSString*HTMLData=[self-constructHTMLText];
[storyWebView加载HtmlString:HTMLData baseURL:[NSURL URLWithString:@”“];
}
-(无效)未收到记忆警告{
[super-didReceiveMemoryWarning];//如果没有superview,则释放该视图
//释放任何不重要的内容,例如缓存数据
}
-(无效)解除锁定{
[super dealoc];
}
-(BOOL)webView:(UIWebView*)webView应加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型
{   
//在这里处理事件
返回true;
}
@结束
基本上,constructHTML函数接受*信息变量中的文本,并使用一些html和javascript将文本中的每个单词包装起来,这样每当单击一个单词时,就会在其上应用css高亮度。我想做的是数一数那些引人注目的词。我通过尝试在函数中传递一些东西来实现这一点,每当单击一个单词时就会调用该函数,但正如我所说的,应该被激发的shouldStartLoadWithRequest方法永远不会执行

我见过很多人做这种事情,但我似乎不明白为什么它不适合我
  • window.location
    不应该是您要访问的目标URL的值吗?在
    marktext
    函数中,将其设置为
    true
    false
    shouldStartLoadWithRequest
    仅在接收到URL请求并即将转到某个位置时才会被调用。不确定将位置设置为布尔值是否足以触发导航事件。您可以将其设置为魔法URL,然后从
    shouldStartLoadWithRequest
    例程返回FALSE,如果它要求导航到特定的魔法URL并阻止它实际移动到某个地方。然后可以允许有效的URL通过

  • 还可能需要查看对的响应。
    窗口位置可能存在一些计时问题


  • 您可以在设置委托时设置断点

    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: @""]];  
    }
    

    我也看到了这个问题,因为我已经设置了

    articleWeb.userInteractionEnabled = NO;`
    
    你最好检查你的代码,设置

    articleWeb.userInteractionEnabled = YES;`
    

    您应该在
    viewDidLoad
    中设置委托,而不是在
    initWithNibName:bundle:
    中设置委托。或者,打开nib文件并控制从web视图到文件所有者的拖动,然后从下拉列表中选择“委派”

    实际上,我正在尝试将window.locaton设置为字符串“true”或“false”,这并不重要,因为我的代码不适用于任何字符串。我也看了你发给我的链接,但这不是我需要的。他的问题是shouldStartLoadWithRequest对他来说太好了,就像我的情况一样,它根本没有被调用!不确定为什么要将位置值设置为“真”或“假”,这就像在URL栏中键入“”