Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript function call in webview

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 623
    Comment on it

    Hello Readers,

    In order to call javascript functions within webview,following code snippet may be used:

    Specify path of the html file which contains the javascript function:

    NSString *path;
    NSBundle *bundle = [NSBundle mainBundle];
    path = [bundle pathForResource:@"FileName" ofType:@"html"]; 

    //Form the url with the path and load it in webview

    NSURL *fileUrl = [NSURL fileURLWithPath:path];
    [webViewVariable loadRequest:[NSURLRequest requestWithURL:fileUrl]];


    //Call the javascript function here

     NSString * funcCall = [NSString stringWithFormat:@"functionTest()"];// Name of the function that needs to be called goes here
    [webViewOutlet stringByEvaluatingJavaScriptFromString:funcCall];

    In swift the following code can be used:
           

     var path : NSString;
     var bundle = NSBundle.mainBundle()
     path = bundle.pathForResource("FileName", ofType: "html")!
     var fileUrl = NSURL.fileURLWithPath(path as String)
     var request =  NSURLRequest(URL: fileUrl)
     webViewOutlet.loadRequest(request)
     var funcCall = NSString(format:"functionTest()")
     webViewOutlet.stringByEvaluatingJavaScriptFromString(funcCall as String)
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: