CKEditor iOS获取按钮事件的HTML编辑器内容

时间:2016-08-27 19:21:32

标签: javascript ios objective-c webview ckeditor

我正在使用CKEditor。在用户更改编辑器中的文本后,我需要以某种方式" Save"用户的变化。

我试过搜索,没有找到我需要的东西。

有人请求帮助,对我来说,尝试在我的应用中使用CKEditor是一段漫长而艰难的旅程。

1 个答案:

答案 0 :(得分:1)

我找到了问题的解决方案。

用户修改位于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:@"&lt;" withString:@"<" ];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&gt;" withString:@">" ];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&quot;" withString:@""""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&#039;" withString:@"'" ];

    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"</p><p>" withString:@"\n"];
    //  htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br />"];
    while ([htmlString rangeOfString:@"&nbsp;&nbsp;"].length > 0) {
        htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&nbsp;&nbsp;" withString:@"  "];
    }
    return htmlString;
}