2016-08-27 95 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; 
}