【问题标题】:How do I know when something has been pasted in a UIWebView?我怎么知道什么时候已经在 UIWebView 中粘贴了一些东西?
【发布时间】:2013-08-19 15:08:37
【问题描述】:

我有一个加载 HTML 字符串的 UIWebView,并且可以使用 Javascript 添加内容。

有什么方法可以知道什么时候(文本或图像)已粘贴到 Web 视图中。

NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlString" ofType:@"txt"];
NSString *htmlString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.ibWebView loadHTMLString:htmlString baseURL:nil];


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *jsToEncloseInEditableContainer = [NSString stringWithFormat:@"function contentedited(e){window.location='mpscontentedited://' + e.keyCode;};(function() {var body = document.body;var contentContainer =  document.createElement('div');contentContainer.id = 'content';contentContainer.setAttribute('contenteditable','true');contentContainer.style.fontFamily=' Helvetica';contentContainer.style.marginTop=\"15px\";contentContainer.onkeydown = contentedited;contentContainer.onpaste=contentedited;var node;while(node=body.firstChild) {contentContainer.appendChild(node);}body.appendChild(contentContainer);%@body=undefined;delete body;contentContainer=undefined;delete contentContainer;node=undefined;delete node;})();", @"contentContainer.focus();"];
    [self.ibWebView stringByEvaluatingJavaScriptFromString:jsToEncloseInEditableContainer];
}

我尝试过覆盖 UIResponder 的 paste: 方法,但它永远不会被调用。

- (void)paste:(id)sender
{
    NSLog(@"paste not called");
}

我也尝试过创建自定义菜单按钮

UIMenuController *menu = [UIMenuController sharedMenuController];
UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"Paste Custom" action:@selector(pasteCustom:)];
[menu setMenuItems:@[item]];
[menu setMenuVisible:YES];

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(pasteCustom:))
    {
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}

- (void)pasteCustom:(id)sender
{
    NSLog(@"paste custom");
    [super paste:sender]; // --> calling this causes a crash (unrecognized selector sent to instance)
}

我做错了什么?

我想知道什么时候粘贴了一些东西,或者我想拥有一个行为类似于默认粘贴按钮的自定义菜单按钮。

提前致谢。

【问题讨论】:

    标签: ios uiwebview uimenucontroller


    【解决方案1】:

    铸造不是魔术。

    我再说一遍:选角不是魔法!

    它不会改变对象的运行时类型。如果它没有以UIWebView 响应选择器,它也不会以UIMenuItem 响应它。您必须使用 JavaScript 检测事件并将您的 JavaScript 函数连接到 Objective-C。使用onpaste JavaScript 事件处理程序,然后执行类似于here 描述的操作:

    <!-- snippet from your HTML string -->
    <script type="text/javascript">
    
        function call_objc_paste(text)
        {
            window.location.href = "fakescheme://" + text;
        }
    
    </script>
    
    <input type="text" onpaste="call_objc_paste(this.value);" />
    
    // Objective-C code:
    webView.delegate = self;
    [webView loadHTMLString:htmlStr baseURL:nil];
    
    - (BOOL)           webView:(UIWebView *)wv
    shouldStartLoadWithRequest:(NSURLRequest *)rq
                navigationType:(UIWebViewNavigationType)navT
    {
        NSString *rqStr = rq.URL.absoluteString;
        NSString *scheme = @"fakescheme://";
        if ([rqStr hasPrefix:scheme]) {
            NSString *pastedText = [rqStr substringFromIndex:scheme.length];
            // potentially do somethin with pastedText, then
    
            return NO;
        }
    
        // it's not our fake URL scheme, just normal navigation
        return YES;
    }
    

    【讨论】:

    • 谢谢! :)。有没有办法我也可以对图像做同样的事情?
    • @vivek241 只是出于好奇,如何粘贴图像?使用哪个 HTML 元素?
    • 我正在寻找类似于 iOS 上原生电子邮件应用的解决方案。
    • @vivek241 我想没有简单/默认的解决方案。如果您寻找代码 sn-p,您可能会找到一些东西。然后尝试弄乱图像粘贴到的元素的事件处理程序(如果您不了解 JavaScript,这并不容易)。你可以得到一个图像 URL 或以 Base-64 编码的图像数据或其他东西。
    • 是否可以在 contenteditable 设置为 true 的情况下获取 div 标签的粘贴文本?
    【解决方案2】:

    为后代复活这个问题:

    在这里查看我的答案:Manipulate paste content in WKWebView 该解决方案使用方法 Swizzling,也可以用于这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 2012-12-10
      • 2017-09-16
      相关资源
      最近更新 更多