简体   繁体   English

在UIWebView的Objective-c中注入JQuery函数

[英]Injecting JQuery function in Objective-c in UIWebView

I have this function in jquery saved as doc2.js in my Target and i had copy in my bundle resources as follow: 我在jquery中将此函数另存为target.doc中的doc2.js,并且已在捆绑资源中复制了以下内容:

doc2.js: doc2.js:

  $(document).ready(function(){
  $(".flip").click(function(){
  $(".panel").slideToggle("slow");
     });
       });

at my Xcode i have the following: 在我的Xcode中,我有以下内容:

 UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
 [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"new" ofType:@"html"]isDirectory:NO]]];
 webView.delegate=self;
 [self.view addSubview:webView];

and this method : 和这种方法:

-(void)webViewDidFinishLoad:(UIWebView *)webView {
 NSString *jqueryCDN = @"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
 NSData *jquery = [NSData dataWithContentsOfURL:[NSURL URLWithString:jqueryCDN]];
 NSString *jqueryString = [[NSMutableString alloc] initWithData:jquery encoding:NSUTF8StringEncoding];
 [webView stringByEvaluatingJavaScriptFromString:jqueryString];
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"doc2" ofType:@"js" inDirectory:NO];
 NSData *fileData = [NSData dataWithContentsOfFile:filePath];
 NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
 [webView stringByEvaluatingJavaScriptFromString:jsString];}

In my html file: 在我的html文件中:

<!DOCTYPE html>
 <html>
    <head>        
         <script type="text/javascript" src="jquery.js"></script>
         <style type="text/css"> 
             div.panel,p.flip
            {
                 margin:0px;
                 padding:5px;
                 text-align:center;
                 background:#e5eecc;
                 border:solid 1px #c3c3c3;
             }
             div.panel
             {
                 height:120px;
                 display:none;
             }
             </style>
     </head>
     <body>    
         <div class="panel">
            <p>Any Thing.</p>
            <p>Any Thing.</p>
         </div>        
         <p class="flip">Show/Hide Panel</p>
            </body>
 </html>

this code should handle the UIWebView but its not working with me I think that my jQuery function is not completed or something like that Any idea ?? 这段代码应该可以处理UIWebView,但是不能和我一起工作。我认为我的jQuery函数没有完成或类似的工作。

You may want to try a different approach: Either include a local copy or download jquery from a cdn. 您可能想尝试另一种方法:包括本地副本或从CDN下载jquery。 Doing both seems unnecessary. 两者都似乎没有必要。

Moreover: Just define a javascript function in your doc2.js, include the file in the HTML and call it directly within Cocoa. 此外:只需在doc2.js中定义一个javascript函数,将文件包含在HTML中,然后直接在Cocoa中调用即可。 Make sure you exposed your functions to Cocoa with 确保您使用

isSelectorExcludedFromWebScript

Calling is possible with 可以拨打电话

evaluateWebScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM