[英]Cannot execute javascript function inside UIWebView?
I have two view controllers, viewcontroller 1 and view controller 2.viewController 1 contains a webview. 我有两个视图控制器,viewcontroller 1和view controller2。viewController1包含一个webview。 I push view controller 2 on top of view controller 1 when i click a link inside the webview.
当我单击Webview内的链接时,我将View Controller 2推到View Controller 1的顶部。 After barcode is scanned in viewcontroller 2 , I pass the scanned data as a function to viewcontroller1.
在viewcontroller 2中扫描条形码后,我将扫描的数据作为函数传递给viewcontroller1。 Then i pop viewController 2 from the top of the screen.
然后,我从屏幕顶部弹出viewController 2。 I am not able to access the webview property in viewcontroller 1 and display the barcode as a javascript alert.
我无法访问viewcontroller 1中的webview属性,并将条形码显示为javascript警报。 Appreciate your help in this.
感谢您的帮助。
Here is viewController 1 这是viewController 1
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ADMSViewController : UIViewController <UIWebViewDelegate>
{
}
@property (nonatomic,retain) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic) BOOL restoringState;
- (IBAction)backButtonPressed:(id)sender;
-(void)pasteVinInTextBox : (NSString *)vin;
@end
Here is its corrosponding .m file 这是其对应的.m文件
#import "ADMSViewController.h"
@implementation ADMSViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_webView setDelegate:self];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
}
//- (void)didReceiveMemoryWarning //{ // [super didReceiveMemoryWarning]; // // Dispose of any resources that can be recreated. //}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated]; }
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated]; }
-(void)webViewDidStartLoad:(UIWebView *)webView {
[_activityIndicator startAnimating]; }
- (void)viewDidUnload {
[super viewDidUnload]; }
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[_activityIndicator stopAnimating]; }
- (IBAction)backButtonPressed:(id)sender {
[_webView goBack]; }
-(void)pasteVinInTextBox:(NSString *)vin {
NSLog(@"%@",vin);
_webView = [[UIWebView alloc]init];
[_webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"]; }
@end
Here is the barcodeScanner.m file 这是barcodeScanner.m文件
#import "ADMSBarcodeScanner.h"
@implementation ADMSBarcodeScanner
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// ADD: present a barcode reader that scans from the camera feed
ZBarReaderViewController *reader = [[ZBarReaderViewController alloc] init];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationLandscapeRight);
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
change this line 改变这条线
[_webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"];
As 如
[_webView stringByEvaluatingJavaScriptFromString:@"alert(\"Trigger the JS!\");"];
it's working for me. 它为我工作。
webViewDidFinishLoad: Sent after a web view finishes loading a frame. webViewDidFinishLoad:在Web视图完成框架加载后发送。
- (void)webViewDidFinishLoad:(UIWebView *)webView
call like 像
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[self.webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"];
}
this link you understand clear uiwebview-javascript-and-objective-c 这个链接你明白uiwebview-javascript-and-objective-c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.