2014-03-12 80 views
0

所以我有一個WebView捕獲網站中的URL更改以打開相機。 一旦圖片做的,我想用它發送回:JavaScript沒有從目標C執行

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

它不工作,我的猜測是因爲self.webView不是web視圖。 我對這個代表和事情一團糟,我習慣了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 

一定是我錯過了一些小東西。

回答

0

難道是隻缺你的JavaScript調用報價:

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