2016-08-27 140 views
0

我正在与CKEditor合作。在用户更改编辑器中的文本后,我需要以某种方式“保存”用户更改。CKEditor iOS获取按钮事件中的HTML编辑器内容

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

有人请帮忙,对于我试图在我的应用程序中获得CKEditor,这是一个漫长而艰难的旅程。

回答

0

我找到了解决问题的办法。

用户修改位于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:@"&amp;" 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; 
}