#import <uikit uikit.h=""> @interface ViewController : UIViewController <uiwebviewdelegate> @property(nonatomic,retain) IBOutlet UIWebView *webview; @property(nonatomic,retain) IBOutlet UIButton *button; -(IBAction)IOS_JS:(id)sender; @end</uiwebviewdelegate></uikit>
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize webview;
- (void)viewDidLoad
{
[super viewDidLoad];
//设置webView
webview.backgroundColor = [UIColor clearColor];
//webview.scalesPageToFit =YES;
webview.delegate =self;
//找到jsIOS.html文件的路径
NSString *basePath = [[NSBundle mainBundle]bundlePath];
NSString *helpHtmlPath = [basePath stringByAppendingPathComponent:@"jsIOS.html"];
NSURL *url = [NSURL fileURLWithPath:helpHtmlPath];
//加载本地html文件
[webview loadRequest:[NSURLRequest requestWithURL:url]];
}
/*
* 点击事件
* 调用javaScript的方法postStr()并取得返回值
* 输出返回值到控制台
*/
-(IBAction)IOS_JS:(id)sender
{
NSString *str = [self.webview stringByEvaluatingJavaScriptFromString:@"postStr();"];
NSLog(@"JS返回值:%@",str);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
不使用webview加载的方式有办法实现吗
@lancely: 这个... 我再研究研究吧。