如何调用js函数设置WebView支持js的Alert,Confirm,Prompt函数的弹出提示框

ANDROID(440)
1.首先继承android.webkit.WebChromeClient实现MyWebChromeClient。
2.在MyWebChromeClient.java中覆盖onJsAlert,onJsConfirm,onJsPrompt三个方法。
3.在初始化Webview时设置调用webview.setWebChromeClient(new MyWebChromeClient());
4.在Webview载入的html中使用window.alert,window.confirm,window.prompt方法,系统弹出的将是自定义实现的对应对话框。
MyWebChromeClient.java的代码如下:
import android.app.AlertD
import android.content.DialogI
import android.content.DialogInterface.OnCancelL
import android.content.DialogInterface.OnClickL
import android.content.DialogInterface.OnKeyL
import android.graphics.B
import android.os.M
import android.util.L
import android.view.KeyE
import android.webkit.JsPromptR
import android.webkit.JsR
import android.webkit.WebChromeC
import android.webkit.WebV
import android.widget.EditT
* /archives//199.html
//****************************************************************************
public class MyWebChromeClient extends WebChromeClient {
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
return super.onCreateWindow(view, dialog, userGesture, resultMsg);
* 覆盖默认的window.alert展示界面,避免title里显示为“:来自file:////”
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(&对话框&)
.setMessage(message)
.setPositiveButton(&确定&, null);
// 不需要绑定按键事件
// 屏蔽keycode等于84之类的按键
builder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
Log.v(&onJsAlert&, &keyCode==& + keyCode + &event=&+ event);
// 禁止响应按back键的事件
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
result.confirm();// 因为没有绑定事件,需要强行confirm,否则页面会变黑显示不了内容。
// return super.onJsAlert(view, url, message, result);
public boolean onJsBeforeUnload(WebView view, String url,
String message, JsResult result) {
return super.onJsBeforeUnload(view, url, message, result);
* 覆盖默认的window.confirm展示界面,避免title里显示为“:来自file:////”
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(&对话框&)
.setMessage(message)
.setPositiveButton(&确定&,new OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
result.confirm();
.setNeutralButton(&取消&, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
builder.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
result.cancel();
// 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
builder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
Log.v(&onJsConfirm&, &keyCode==& + keyCode + &event=&+ event);
// 禁止响应按back键的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
// return super.onJsConfirm(view, url, message, result);
* 覆盖默认的window.prompt展示界面,避免title里显示为“:来自file:////”
* window.prompt('请输入您的域名地址', &#');
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, final JsPromptResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(&对话框&).setMessage(message);
final EditText et = new EditText(view.getContext());
et.setSingleLine();
et.setText(defaultValue);
builder.setView(et)
.setPositiveButton(&确定&, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm(et.getText().toString());
.setNeutralButton(&取消&, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
// 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
builder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
Log.v(&onJsPrompt&, &keyCode==& + keyCode + &event=&+ event);
// 禁止响应按back键的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
// return super.onJsPrompt(view, url, message, defaultValue,
// result);
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
public void onRequestFocus(WebView view) {
super.onRequestFocus(view);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:614388次
积分:10153
积分:10153
排名:第1609名
原创:243篇
转载:905篇
评论:23条
(4)(6)(11)(15)(12)(8)(2)(11)(31)(21)(33)(51)(24)(13)(11)(14)(14)(8)(8)(14)(35)(21)(56)(97)(15)(14)(2)(15)(28)(14)(80)(72)(53)(17)(10)(8)(221)(40)(16)(6)(18)拒绝访问 |
| 百度云加速
请打开cookies.
此网站 () 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(b43d7-ua98).
重新安装浏览器,或使用别的浏览器WKWebViewでJavaScript(Alert,Confirm,Prompt)の処理 - Qiita
からリンクからリンク
この投稿にどのような問題がありますか? スパムです
攻撃的または迷惑な内容を含んでいます
不適切な内容を含んでいます
送信いただいたご意見への返信は行っておりません。返信の必要な内容については、 からお問い合わせください。JS弹框(alert,confirm,prompt) - 博客频道 - CSDN.NET
Inuyasha1121的专栏
分类:javascript弹窗
警告框经常用于确保用户可以得到某些信息。
当警告框出现后,用户需要点击确定按钮才能继续进行操作。
alert(&文本&)
确认框用于使用户可以验证或者接受某些信息。
当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。
如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。
confirm(&文本&)
提示框经常用于提示用户在进入页面前输入某个值。
当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
prompt(&文本&,&默认值&)
1.1.1,警告框
1.1.1.1,警告框
&script type=&text/javascript&&
function disp_alert()
alert(&我是警告框!!&)
&input type=&button& onClick=&disp_alert()& value=&显示警告框& /&
1.1.1.2,带有折行的警告框
&script type=&text/javascript&&
function disp_alert()
alert(&再次向您问好!在这里,我们向您演示& + '\n' + &如何向警告框添加折行。&)
&input type=&button& onClick=&disp_alert()& value=&显示警告框& /&
1.1.2,确认框
&script type=&text/javascript&&
function show_confirm()
var r=confirm(&按下一个按钮!&);
if (r==true)
alert(&按下 确定!&);
alert(&按下 取消!&);
&input type=&button& onClick=&show_confirm()& value=&展示一个确认框& /&
1.1.3,提示框
&script type=&text/javascript&&
function disp_prompt()
var name=prompt(&请输入您的名字&,&Bill Gates&)
if (name!=null && name!=&&)
document.write(&你好!& + name + & 今天过得怎么样?&)
&input type=&button& onClick=&disp_prompt()& value=&显示提示框& /&
Inuyasha1121
排名:第8811名
(20)(70)(10)(1)(1)(33)(4)(1)(3)(5)(6)(8)(3)(3)(5)(1)(1)(3)(1)(3)(1)(1)(6)(1)(1)(1)(12)(1)(1)(1)(17)(7)(17)(1)(3)(2)(13)(1)(1)(4)(2)(3)(1)(1)(1)(1)(4)(1)(1)(1)(2)(1)(1)(0)(5)(2)(1)(1)(1)(1)(1)(1)(1)(0)(1)(1)(1)(3)(1)(3)(1)(2)(1)(1)(3)(9)(6)(1)(1)(2)(1)(1)(3)(6)(1)(1)(3)(3)(7)(2)(1)(1)(4)(1)(1)(1)(1)(1)(1)(0)(1)(2)(2)(1)(1)(0)(2)(2)(1)(5)(1)(11)(1)(0)(0)(1)(0)(1)(1)(1)(0)(1)(1)(0)(0)(1)(1)(1)(1)(1)(10)(10)(3)(2)(1)(1)(1)(1)iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
最近做一个项目,开始是使用WebView与JS交互的,由于内存管理方面WebView欠佳。WKWebVIew的内存线程管理好,所以选择使用 WKWebVIew(使用WKWebView 的缺点在于,这个控件加载的H5页面不支持ajax请求,所以需要自己把网络请求在OC上实现)。
一、首先说下应该注意的问题:
1.要获取拍照或相册的图片,如果是iOS 10,需要设置访问权限(在 Info-plist 中设置)
相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
相册权限: Privacy - Photo Library Usage Description 是否允许此App访问你的媒体资料库?
2.WebView和WKWebView和JS互调的方法和使用的传参类型不同
WebView 使用 (window.iosModel.getImage(JSON.stringify(parameter));//JSON.stringify(参数字符串) 这个方法是 把字符串转换成json字符串 parameter是参数字符串
)传值给 OC
WKWebView 使用 (window.webkit.messageHandlers.iosModel.postMessage(parameter))
3.需要特别注意的是:WKWebView 不执行JS写的 ajax请求(WKWebView 可能由于基于 WebKit,并不会执行 C socket 相关的函数对 HTTP 请求进行处理)如果有网络请求,需要自己用OC实现
4.使用的时候不要忘记挂上使用到的代理,和导入代理
@interface ViewController ()
5.屏蔽WebView的可选菜单(即不会出现拷贝、全选等弹出菜单)在加载完成后的代理中执行以下两段JS
// 导航完成时,会回调(也就是页面载入完成了)
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
NSLog(@&66===%s&, __FUNCTION__);
// 禁用选中效果
[self.webView evaluateScript:@&document.documentElement.style.webkitUserSelect='none'& completionHandler:nil];
[self.webView evaluate:@&document.documentElement.style.webkitTouchCallout='none'& completionHandler:nil];
二、代码:
JS与iOS交互
JS页面获取iOS系统图片
var getIOSImage = function(){
var parameter = {'title':'JS调OC','describe':'这里就是JS传给OC的参数'};
// 在下面这里实现js 调用系统原生api iosDelegate
//JSON.stringify(参数字符串) 这个方法是 把字符串转换成json字符串
window.iosDelegate.getImage(JSON.stringify(parameter));// 实现数据的 json 格式字符串
// 这里是 iOS调用js的方法
function setImageWithPath(arguments){
document.getElementById('changeImage').src = arguments['imagePath'];
document.getElementById('iosParame').innerHTML = arguments['iosContent'];
#import &ViewController.h& #import
#import &SaveImage_Util.h& @interface ViewController ()
@property (nonatomic, strong) WKWebView *webV @property (nonatomic, strong) UIProgressView *progressV @end @implementation ViewController { int indextN// 交替图片名字 UIImage *getI//获取的图片 } - (void)viewDidLoad { [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeN self.automaticallyAdjustsScrollViewInsets = NO; WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; // 设置偏好设置 config.preferences = [[WKPreferences alloc] init]; // 默认为0 config.preferences.minimumFontSize = 10; // 默认认为YES config.preferences.javaScriptEnabled = YES; // 在iOS上默认为NO,表示不能自动通过窗口打开 config.preferences.javaScriptCanOpenWindowsAutomatically = NO; // web内容处理池 config.processPool = [[WKProcessPool alloc] init]; // 通过JS与webview内容交互 config.userContentController = [[WKUserContentController alloc] init]; // 注入JS对象名称AppModel,当JS通过AppModel来调用时, // 我们可以在WKScriptMessageHandler代理中接收到 [config.userContentController addScriptMessageHandler:self name:@&iosModel&]; //通过默认的构造器来创建对象 self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config]; // 导航代理 self.webView.navigationDelegate = // 与webview UI交互代理 self.webView.UIDelegate = NSURL *path = [[NSBundle mainBundle] URLForResource:@&testJS& withExtension:@&html&]; [self.webView loadRequest:[NSURLRequest requestWithURL:path]]; [self.view addSubview:self.webView]; // 添加KVO监听 [self.webView addObserver:self forKeyPath:@&loading& options:NSKeyValueObservingOptionNew context:nil]; [self.webView addObserver:self forKeyPath:@&title& options:NSKeyValueObservingOptionNew context:nil]; [self.webView addObserver:self forKeyPath:@&estimatedProgress& options:NSKeyValueObservingOptionNew context:nil]; // 添加进入条 self.progressView = [[UIProgressView alloc] init]; self.progressView.frame = self.view. [self.view addSubview:self.progressView]; self.progressView.backgroundColor = [UIColor blackColor]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@&后退& style:UIBarButtonItemStyleDone target:self action:@selector(goback)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@&前进& style:UIBarButtonItemStyleDone target:self action:@selector(gofarward)]; } - (void)goback { if ([self.webView canGoBack]) { [self.webView goBack]; } } - (void)gofarward { if ([self.webView canGoForward]) { [self.webView goForward]; } } #pragma mark - WKScriptMessageHandler // 通过这个方法获取 JS传来的json字符串 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { if ([message.name isEqualToString:@&iosModel&]) { // 打印所传过来的参数,只支持NSNumber, NSString, NSDate, NSArray, // NSDictionary, and NSNull类型 NSLog(@&JS传来的json字符串 : %@&, message.body); NSDictionary *jsDictionary = message. if ([jsDictionary[@&means&] isEqualToString:@&获取系统图片&]) { [self beginOpenPhoto]; } } } #pragma mark - KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@&loading&]) { NSLog(@&loading&); } else if ([keyPath isEqualToString:@&title&]) { self.title = self.webView. } else if ([keyPath isEqualToString:@&estimatedProgress&]) { NSLog(@&progress: %f&, self.webView.estimatedProgress); self.progressView.progress = self.webView.estimatedP } if (!self.webView.loading) { [UIView animateWithDuration:0.5 animations:^{ self.progressView.alpha = 0; }]; } } #pragma mark - WKNavigationDelegate // 请求开始前,会先调用此代理方法 // 与UIWebView的 // - (BOOL)webView:(UIWebView *)webView // shouldStartLoadWithRequest:(NSURLRequest *)request // navigationType:(UIWebViewNavigationType)navigationT // 类型,在请求先判断能不能跳转(请求) - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction: (WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSString *hostname = navigationAction.request.URL.host.lowercaseS if (navigationAction.navigationType == WKNavigationTypeLinkActivated && ![hostname containsString:@&.&]) { // 对于跨域,需要手动跳转, 用系统(Safari)打开 [[UIApplication sharedApplication] openURL:navigationAction.request.URL]; // 不允许web内跳转 decisionHandler(WKNavigationActionPolicyCancel); } else { self.progressView.alpha = 1.0; decisionHandler(WKNavigationActionPolicyAllow); } } // 在响应完成时,会回调此方法 // 如果设置为不允许响应,web内容就不会传过来 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler { decisionHandler(WKNavigationResponsePolicyAllow); } // 开始导航跳转时会回调 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation { } // 接收到重定向时会回调 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation { } // 导航失败时会回调 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error { } // 页面内容到达main frame时回调 - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation { } // 导航完成时,会回调(也就是页面载入完成了) - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation { NSLog(@&66===%s&, __FUNCTION__); // 禁用选中效果 [self.webView evaluateJavaScript:@&document.documentElement.style.webkitUserSelect='none'& completionHandler:nil]; [self.webView evaluateJavaScript:@&document.documentElement.style.webkitTouchCallout='none'& completionHandler:nil]; } // 导航失败时会回调 - (void)webView:(WKWebView *)webView didFailNavigation: (null_unspecified WKNavigation *)navigation withError:(NSError *)error { } /* 对于HTTPS的都会触发此代理,如果不要求验证,传默认就行 如果需要证书验证,与使用AFN进行HTTPS证书验证是一样的 */ - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge completionHandler: (void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler { completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); } // 9.0才能使用,web内容处理中断时会触发 /* - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView { } */ #pragma mark - WKUIDelegate - (void)webViewDidClose:(WKWebView *)webView { } /* 在JS端调用alert函数时,会触发此代理方法。JS端调用alert时所传的数据可以通过message拿到 在原生得到结果后,需要回调JS,是通过completionHandler回调 */ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&alert& message:message preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@&确定& style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { completionHandler(); }]]; [self presentViewController:alert animated:YES completion:NULL]; NSLog(@&%@&, message); } // JS端调用confirm函数时,会触发此方法 // 通过message可以拿到JS端所传的数据 // 在iOS端显示原生alert得到YES/NO后 // 通过completionHandler回调给JS端 - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&confirm& message:message preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@&确定& style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ completionHandler(YES); }]]; [alert addAction:[UIAlertAction actionWithTitle:@&取消& style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { completionHandler(NO); }]]; [self presentViewController:alert animated:YES completion:NULL]; NSLog(@&%@&, message); } // JS端调用prompt函数时,会触发此方法 // 要求输入一段文本 // 在原生输入得到文本内容后,通过completionHandler回调给JS - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler { UIAlertController *alert = [UIAlertController alertControllerWithTitle:prompt message:defaultText preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.textColor = [UIColor redColor]; }]; [alert addAction:[UIAlertAction actionWithTitle:@&确定& style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { completionHandler([[alert.textFields lastObject] text]); }]]; [self presentViewController:alert animated:YES completion:NULL]; } // 获取图片 - (void)beginOpenPhoto { // 主队列 异步打开相机 dispatch_async(dispatch_get_main_queue(), ^{ [self takePhoto]; }); } #pragma mark 取消选择照片代理方法 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark //打开本地照片 - (void) localPhoto { UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoL imagePicker.delegate = [self presentViewController:imagePicker animated:YES completion:nil]; } #pragma mark //打开相机拍照 - (void) takePhoto { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeC if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc]init]; picker.delegate = picker.allowsEditing = YES; picker.sourceType = sourceT picker.modalTransitionStyle = UIModalTransitionStyleCoverV [self presentViewController:picker animated:YES completion:nil]; } else { NSLog(@&模拟器中不能打开相机&); [self localPhoto]; } } // 选择一张照片后进入这里 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; // 当前选择的类型是照片 if ([type isEqualToString:@&public.image&]) { // 获取照片 getImage = [info objectForKey:@&UIImagePickerControllerOriginalImage&]; NSLog(@&===Decoded image size: %@&, NSStringFromCGSize(getImage.size)); // obtainImage 压缩图片 返回原尺寸 indextNumb = indextNumb == 1?2:1; NSString *nameStr = [NSString stringWithFormat:@&Varify%d.jpg&,indextNumb]; [SaveImage_Util saveImage:getImage ImageName:nameStr back:^(NSString *imagePath) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@&图片路径:%@&,imagePath); /** * 这里是IOS 调 js 其中 setImageWithPath 就是js中的方法 setImageWithPath(),参数是字典 */ NSString *callJSString = [NSString stringWithFormat:@&%@({\&imagePath\&:\&%@\&,\&iosContent\&:\&获取图片成功,把系统获取的图片路径传给js 让html显示\&})&,@&setImageWithPath&,imagePath]; [self.webView evaluateJavaScript:callJSString completionHandler:^(id resultObject, NSError * _Nullable error) { if (!error) { NSLog(@&OC调 JS成功&); } else { NSLog(@&OC调 JS 失败&); } }]; }); }]; [picker dismissViewControllerAnimated:YES completion:nil]; } } @end
下面是图片处理的 工具类
SaveImage_Util.h
JS和iOS交互
Created by user on 16/10/14.
Copyright & 2016年 user. All rights reserved.
@interface SaveImage_Util : NSObject
#pragma mark
保存图片到document
+ (BOOL)saveImage:(UIImage *)saveImage ImageName:(NSString *)imageName back:(void(^)(NSString *imagePath))
SaveImage_Util.m
JS和iOS交互
Created by user on 16/10/14.
Copyright & 2016年 user. All rights reserved.
#import &SaveImage_Util.h&
@implementation SaveImage_Util
#pragma mark
保存图片到document
+ (BOOL)saveImage:(UIImage *)saveImage ImageName:(NSString *)imageName back:(void(^)(NSString *imagePath))back
NSString *path = [SaveImage_Util getImageDocumentFolderPath];
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSString *documentsDirectory = [NSString stringWithFormat:@&%@/&, path];
// Now we get the full path to the file
NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
NSFileManager *fileManager = [NSFileManager defaultManager];
//如果文件路径存在的话
BOOL bRet = [fileManager fileExistsAtPath:imageFile];
NSLog(@&文件已存在&);
if ([fileManager removeItemAtPath:imageFile error:nil])
NSLog(@&删除文件成功&);
if ([imageData writeToFile:imageFile atomically:YES])
NSLog(@&保存文件成功&);
back(imageFile);
if (![imageData writeToFile:imageFile atomically:NO])
[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if ([imageData writeToFile:imageFile atomically:YES])
back(imageFile);
return YES;
return NO;
#pragma mark
从文档目录下获取Documents路径
+ (NSString *)getImageDocumentFolderPath
NSString *patchDocument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
return [NSString stringWithFormat:@&%@/Images&, patchDocument];
以上内容仅供参考,部分内容来之网络,如有重复,请联系修改,谢谢!}

我要回帖

更多关于 js设置函数只执行一次 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信