我正在使用 .js 文件来验证应用程序中的 .html 文件,在我的项目中添加了 .js&.html 文件,但 .js 文件未存储在包内容中,仅包含 .html 文件,我使用的是 ios 模拟器 5.0 ,
我的验证来源是..
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"]; //you can also use PDF files
NSLog(@"%@",urlAddress);
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
web.backgroundColor=[UIColor redColor];
}
- (IBAction)markHighlightedString:(id)sender {
// The JS File
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HighlightedString" ofType:@"js" inDirectory:@""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[web stringByEvaluatingJavaScriptFromString:jsString];
// The JS Function
NSString *startSearch = [NSString stringWithFormat:@"stylizeHighlightedString()"];
[web stringByEvaluatingJavaScriptFromString:startSearch];
}
- (IBAction)getHighlightedString:(id)sender {
// The JS File
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HighlightedString" ofType:@"js" inDirectory:@""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[web stringByEvaluatingJavaScriptFromString:jsString];
// The JS Function
NSString *startSearch = [NSString stringWithFormat:@"getHighlightedString()"];
[web stringByEvaluatingJavaScriptFromString:startSearch];
NSString *selectedText = [NSString stringWithFormat:@"selectedText"];
NSString * highlightedString = [web stringByEvaluatingJavaScriptFromString:selectedText];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Highlighted String"
message:highlightedString
delegate:nil
cancelButtonTitle:@"Oh Yeah"
otherButtonTitles:nil];
[alert show];
//[alert release]; // not required anymore because of ARC
}
- (IBAction)removeAllHighlights
{
// calls the javascript function to remove html highlights
[web stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"];
}
我在 NSLog(@"%@",filePath);//HighlightedString.js 中得到空值