JavaScript 未从 Objective C 执行

Posted

技术标签:

【中文标题】JavaScript 未从 Objective C 执行【英文标题】:JavaScript not executed from Objective C 【发布时间】:2014-03-12 19:02:11 【问题描述】:

所以我有一个 WebView,它可以捕获网站中 URL 的变化以打开相机。 图片完成后,我想将其发回:

[self.webView stringByEvaluatingjavascriptFromString:[NSString stringWithFormat:@"addImageios(%@)", base64EncodedImage]];

它不起作用,我猜是因为 self.webView 不是 webview。 我对这些代表和事情一团糟,我已经习惯了 android。 好吧,我的代码在这里。

WebViewController.h

#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UIToolbar *ToolBar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *MenuButton;

@end

WebViewController.m

#import "WebViewController.h"
@interface WebViewController ()
@end
@implementation WebViewController
@synthesize webView = _webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) 
    // Custom initialization

return self;


- (void)webViewDidFinishLoad:(UIWebView *)webView 
NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"%@", js_result);


- (void)viewDidLoad

[super viewDidLoad];

_webView.delegate = self;

NSURL *url = [NSURL URLWithString:@"http://some_url"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestURL];


// Do any additional setup after loading the view.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *URL= [prefs stringForKey:@"URL"];



- (void)didReceiveMemoryWarning

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.



- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
NSURL *url = request.URL;
NSString *filePath= [url path];
//NSString *myString = [url absoluteString];
if ([filePath rangeOfString:@"TAKEPICWITHCAMERA"].location != NSNotFound) 
    //TAKE PIC WITH CAMERA
    [self openCamera];
    return NO;




return YES;


- (void)openCamera 

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];



#pragma mark - Image Picker Controller delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
NSString *base64EncodedImage = [self encodeToBase64String:chosenImage];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"addImageiOS(%@)", base64EncodedImage]];
[picker dismissViewControllerAnimated:YES completion:NULL];



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 

[picker dismissViewControllerAnimated:YES completion:NULL];


- (NSString *)encodeToBase64String:(UIImage *)image 
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];


@end

这一定是我错过了一些小事。

【问题讨论】:

【参考方案1】:

可能只是在您的 JavaScript 调用中缺少引号:

@"addImageiOS(\"%@\")"

【讨论】:

以上是关于JavaScript 未从 Objective C 执行的主要内容,如果未能解决你的问题,请参考以下文章

Objective C 的 Javascript 语法高亮显示

从 Objective C 执行 Javascript 代码

$_POST 未从 Javascript 的 Fetch() 中检索数据

文件未从github主机正确加载

Objective C将数据发送到phonegap并调用javascript函数

Javascript未从动态生成的表单提交数据