1

I have two view controllers, viewcontroller 1 and view controller 2.viewController 1 contains a webview. I push view controller 2 on top of view controller 1 when i click a link inside the webview. After barcode is scanned in viewcontroller 2 , I pass the scanned data as a function to viewcontroller1. Then i pop viewController 2 from the top of the screen. I am not able to access the webview property in viewcontroller 1 and display the barcode as a javascript alert. Appreciate your help in this.

Here is 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

#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

#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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • See this answer: http://stackoverflow.com/a/1585854/188331 – Raptor Jan 30 '14 at 03:29
  • Please elaborate "not working" – Raptor Jan 30 '14 at 04:09
  • google has a main text box. I am loading that page into the UIWebView. Just assume that when i click the back button it opens the admsbarcode scanner view controller. after i scan it i would like to paste the text inside the text box. – user3240231 Jan 30 '14 at 04:28

2 Answers2

0

change this line

 [_webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"]; 

As

 [_webView stringByEvaluatingJavaScriptFromString:@"alert(\"Trigger the JS!\");"]; 

it's working for me.

Ayaz
  • 1,398
  • 15
  • 29
0

webViewDidFinishLoad: Sent after a web view finishes loading a frame.

- (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

codercat
  • 22,873
  • 9
  • 61
  • 85