【发布时间】:2017-01-04 05:20:35
【问题描述】:
我正在使用 CKEditor。用户更改编辑器中的文本后,我需要以某种方式“保存”用户更改。
我已经尝试过搜索,但没有找到我需要的东西。
请大家帮忙,尝试在我的应用中安装 CKEditor 对我来说是一段漫长而艰难的旅程。
【问题讨论】:
标签: javascript ios objective-c webview ckeditor
我正在使用 CKEditor。用户更改编辑器中的文本后,我需要以某种方式“保存”用户更改。
我已经尝试过搜索,但没有找到我需要的东西。
请大家帮忙,尝试在我的应用中安装 CKEditor 对我来说是一段漫长而艰难的旅程。
【问题讨论】:
标签: javascript ios objective-c webview ckeditor
我找到了问题的解决方案。
用户修改WKWebView中的CKEditor editor1标签中的信息后,需要在webView上运行如下方法。
我必须添加“textToHTML”方法,因为检索到的 HTML 并将其放置到字符串中将诸如“
- (IBAction)saveButtonItemPressed:(UIBarButtonItem *)sender {
// Save HTML contents of Editor Window
[self getEditorHTMLContents:^(NSString *result) {
NSString *editorContents1 = [self textToHtml:result];
NSLog(@"%@",editorContents1);
}];
}
-(void)getEditorHTMLContents:(void(^)(NSString* result))onFinish {
__block NSString *content;
// Script to get content of Editor1
NSString *script = @"(CKEDITOR.instances['editor1'].getData());";
[self.webView evaluateJavaScript:script completionHandler:^(id _Nullable result, NSError * _Nullable error) {
content = (NSString *)result;
if (error) {
NSLog(@"%@",error);
}
if (onFinish) onFinish(content);
}];
}
- (NSString*)textToHtml:(NSString*)htmlString {
if (!htmlString) return @"ERROR";
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&" withString:@"&" ];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<" withString:@"<" ];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@">" withString:@">" ];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@""" withString:@""""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'" withString:@"'" ];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"</p><p>" withString:@"\n"];
// htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br />"];
while ([htmlString rangeOfString:@" "].length > 0) {
htmlString = [htmlString stringByReplacingOccurrencesOfString:@" " withString:@" "];
}
return htmlString;
}
【讨论】: