marcuswestin / WebViewJavascriptBridge

An iOS/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews/WebViews
http://marcuswest.in
MIT License
14.29k stars 2.98k forks source link

Why the html content be loaded twice? #10

Closed roamerxv closed 12 years ago

roamerxv commented 12 years ago

I code the test program. the .h file is

// // ViewController.h // test // // Created by remote roamer on 12-5-3. // Copyright (c) 2012年 MyCompanyName. All rights reserved. //

import <UIKit/UIKit.h>

import "WebViewJavascriptBridge.h"

import "AppDelegate.h"

@class AppDelegate;

@interface ViewController : UIViewController < WebViewJavascriptBridgeDelegate > { IBOutlet UIWebView * webView; }

@property(retain,nonatomic) UIWebView * webView;

@property(retain,nonatomic) WebViewJavascriptBridge * javascriptBridge;

@property(nonatomic,retain) NSBundle* bundle ;

@end

the .m file :

// // ViewController.m // test // // Created by remote roamer on 12-5-3. // Copyright (c) 2012年 MyCompanyName. All rights reserved. //

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController @synthesize webView; @synthesize bundle; @synthesize javascriptBridge;

-(void) javascriptBridge:(WebViewJavascriptBridge )bridge receivedMessage:(NSString )message fromWebView:(UIWebView *)webView {

}

-(void) webViewDidStartLoad:(UIWebView *)webView { NSLog(@"loading html start"); //[MBProgressHUD showHUDAddedTo:self.webView animated:YES]; }

-(void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"load html finish."); //[MBProgressHUD hideHUDForView:self.webView animated:YES]; }

-(void) webView:(UIWebView )webView didFailLoadWithError:(NSError )error { NSLog(@"load html error:%@",error); //[MBProgressHUD hideHUDForView:self.webView animated:YES]; }

@end

When I run it , the logger info in console is :

2012-05-03 20:11:06.777 test[14860:f803] initWithNibName 2012-05-03 20:11:06.791 test[14860:f803] viewDidLoad 2012-05-03 20:11:06.813 test[14860:f803] loading html start 2012-05-03 20:11:06.836 test[14860:f803] loading html start 2012-05-03 20:11:06.837 test[14860:f803] load html finish. 2012-05-03 20:11:06.840 test[14860:f803] load html finish.

Why the html connent loaded twice?

ps: I‘m using the xcode 4.3.2 and the sdk is 5.1

implementation commented 12 years ago

It's the way how WebViewJavaScriptBridge works. As you can see it here: https://github.com/marcuswestin/WebViewJavascriptBridge/blob/master/WebViewJavascriptBridge/WebViewJavascriptBridge.m#L79

It uses JS to redirect it's own iFrame, that will be processed by native Objective-C code in this method: https://github.com/marcuswestin/WebViewJavascriptBridge/blob/master/WebViewJavascriptBridge/WebViewJavascriptBridge.m#L142

03.05.2012, в 16:21, roamerxv написал(а):

I code the test program. the .h file is

// // ViewController.h // test // // Created by remote roamer on 12-5-3. // Copyright (c) 2012年 MyCompanyName. All rights reserved. //

import <UIKit/UIKit.h>

import "WebViewJavascriptBridge.h"

import "AppDelegate.h"

@class AppDelegate;

@interface ViewController : UIViewController { IBOutlet UIWebView * webView; }

@property(retain,nonatomic) UIWebView * webView;

@property(retain,nonatomic) WebViewJavascriptBridge * javascriptBridge;

@property(nonatomic,retain) NSBundle* bundle ;

@end

the .m file :

// // ViewController.m // test // // Created by remote roamer on 12-5-3. // Copyright (c) 2012年 MyCompanyName. All rights reserved. //

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController @synthesize webView; @synthesize bundle; @synthesize javascriptBridge;

-(void) javascriptBridge:(WebViewJavascriptBridge )bridge receivedMessage:(NSString )message fromWebView:(UIWebView *)webView {

}

-(void) webViewDidStartLoad:(UIWebView *)webView { NSLog(@"loading html start"); //[MBProgressHUD showHUDAddedTo:self.webView animated:YES]; }

-(void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"load html finish."); //[MBProgressHUD hideHUDForView:self.webView animated:YES]; }

-(void) webView:(UIWebView )webView didFailLoadWithError:(NSError )error { NSLog(@"load html error:%@",error); //[MBProgressHUD hideHUDForView:self.webView animated:YES]; }

  • (id)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle )nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization NSLog(@"initWithNibName"); //self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeWithDelegate: (AppDelegate *)([UIApplication sharedApplication]).delegate]; self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeWithDelegate:self];

    } return self; }

  • (void)viewDidLoad { [super viewDidLoad]; NSLog(@"viewDidLoad");

    //self.webView.delegate = self; self.webView.delegate = self.javascriptBridge; self.bundle = [NSBundle mainBundle]; [webView loadHTMLString:@"aaa" baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];

    // Do any additional setup after loading the view, typically from a nib. }

  • (void)viewDidUnload {

    [super viewDidUnload]; // Release any retained subviews of the main view. }

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }

@end

When I run it , the logger info in console is :

2012-05-03 20:11:06.777 test[14860:f803] initWithNibName 2012-05-03 20:11:06.791 test[14860:f803] viewDidLoad 2012-05-03 20:11:06.813 test[14860:f803] loading html start 2012-05-03 20:11:06.836 test[14860:f803] loading html start 2012-05-03 20:11:06.837 test[14860:f803] load html finish. 2012-05-03 20:11:06.840 test[14860:f803] load html finish.

Why the html connent loaded twice?


Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/10

roamerxv commented 12 years ago

thank you for replay。

It's mean that html content be loaded twice can not avoid ?

implementation commented 12 years ago

It doesn't get loaded twice - only almost empty iframe changes it's location.

03.05.2012, в 21:36, roamerxvreply@reply.github.com написал(а):

thank you for replay。

It's mean that html content be loaded twice can not avoid ?


Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/10#issuecomment-5493290

roamerxv commented 12 years ago

But I not use some empty iframe in my code. Just only one webView in the view that designed by xib file

marcuswestin commented 12 years ago

Maybe it loads multiple resources, eg favicon.ico?

-- while mobile

On May 3, 2012, at 5:21 AM, roamerxvreply@reply.github.com wrote:

I code the test program. the .h file is

// // ViewController.h // test // // Created by remote roamer on 12-5-3. // Copyright (c) 2012年 MyCompanyName. All rights reserved. //

import <UIKit/UIKit.h>

import "WebViewJavascriptBridge.h"

import "AppDelegate.h"

@class AppDelegate;

@interface ViewController : UIViewController { IBOutlet UIWebView * webView; }

@property(retain,nonatomic) UIWebView * webView;

@property(retain,nonatomic) WebViewJavascriptBridge * javascriptBridge;

@property(nonatomic,retain) NSBundle* bundle ;

@end

the .m file :

// // ViewController.m // test // // Created by remote roamer on 12-5-3. // Copyright (c) 2012年 MyCompanyName. All rights reserved. //

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController @synthesize webView; @synthesize bundle; @synthesize javascriptBridge;

-(void) javascriptBridge:(WebViewJavascriptBridge )bridge receivedMessage:(NSString )message fromWebView:(UIWebView *)webView {

}

-(void) webViewDidStartLoad:(UIWebView *)webView { NSLog(@"loading html start"); //[MBProgressHUD showHUDAddedTo:self.webView animated:YES]; }

-(void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"load html finish."); //[MBProgressHUD hideHUDForView:self.webView animated:YES]; }

-(void) webView:(UIWebView )webView didFailLoadWithError:(NSError )error { NSLog(@"load html error:%@",error); //[MBProgressHUD hideHUDForView:self.webView animated:YES]; }

  • (id)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle )nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization NSLog(@"initWithNibName"); //self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeWithDelegate: (AppDelegate *)([UIApplication sharedApplication]).delegate]; self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeWithDelegate:self];

    } return self; }

  • (void)viewDidLoad { [super viewDidLoad]; NSLog(@"viewDidLoad");

    //self.webView.delegate = self; self.webView.delegate = self.javascriptBridge; self.bundle = [NSBundle mainBundle]; [webView loadHTMLString:@"aaa" baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];

    // Do any additional setup after loading the view, typically from a nib. }

  • (void)viewDidUnload {

    [super viewDidUnload]; // Release any retained subviews of the main view. }

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }

@end

When I run it , the logger info in console is :

2012-05-03 20:11:06.777 test[14860:f803] initWithNibName 2012-05-03 20:11:06.791 test[14860:f803] viewDidLoad 2012-05-03 20:11:06.813 test[14860:f803] loading html start 2012-05-03 20:11:06.836 test[14860:f803] loading html start 2012-05-03 20:11:06.837 test[14860:f803] load html finish. 2012-05-03 20:11:06.840 test[14860:f803] load html finish.

Why the html connent loaded twice?


Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/10

roamerxv commented 12 years ago

Hi.I used the WeViewbJavascriptBridge in my project , then found the html reload twice . So I write the simple test project , not used any resources and faviicon.ico that just only a simple window . project is uploaded . https://github.com/roamerxv/WebJavascriptBridgeTester

Can u help me to find the problems? thank you very much.

marcuswestin commented 12 years ago

Im sorry, but I don't have time to debug this right now. I believe that event may fire every time you send a message, in which case the second load may be the JavaScript sending the "I'm ready" message.

-- while mobile

On May 3, 2012, at 7:47 PM, roamerxvreply@reply.github.com wrote:

Hi.I used the WeViewbJavascriptBridge in my project , then found the html reload twice . So I write the simple test project , not used any resources and faviicon.ico that just only a simple window . project is uploaded . https://github.com/roamerxv/WebJavascriptBridgeTester

Can u help me to find the problems? thank you very much.


Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/10#issuecomment-5503278

roamerxv commented 12 years ago

ok。tank for your replay. I take it by myself

marcuswestin commented 12 years ago

Sorry :( best of luck!!

-- while mobile

On May 4, 2012, at 4:45 AM, roamerxvreply@reply.github.com wrote:

ok。tank for your replay. I take it by myself


Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/10#issuecomment-5508722

marcuswestin commented 12 years ago

Closing - feel free to reopen.