与 UIActivityViewController 共享 UiWebView 内容

Share UiWebView contents with UIActivityViewController

我正在寻找分享 UIWebView 内容的代码。 我在 @IBAction 中得到了这个:

let activityViewController = UIActivityViewController(
  activityItems: [textField.text as NSString],
  applicationActivities: nil)

presentViewController(activityViewController, animated: true, completion: nil)

我需要做什么而不是textField.text

NSURL *url = self.webView.request.URL ? 
self.webView.request.URL : self.request.URL;
    if (url != nil) {
        NSArray *activities = @[[SVWebViewControllerActivitySafari new], [SVWebViewControllerActivityChrome new]];

        if ([[url absoluteString] hasPrefix:@"file:///"]) {
            UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
            [dc presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
        } else {
            UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"], url] applicationActivities:activities];

#ifdef __IPHONE_8_0
            if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1 &&
                UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {
                UIPopoverPresentationController *ctrl = activityController.popoverPresentationController;
                ctrl.sourceView = self.view;
                ctrl.barButtonItem = sender;
            }
#endif

            [self presentViewController:activityController animated:YES completion:nil];
        }
    }

看看SVWebViewController

要将 textField.text 替换为网页视图的 URL,您可以使用以下代码:

let activityViewController = UIActivityViewController (
        activityItems: [(webView.request?.URL.absoluteString)! as NSString],
        applicationActivities: nil
)

presentViewController(activityViewController, animated: true, completion: nil)

其中 webView 是一个 UIWebView 实例。

p.s。在使用此代码之前,请确保网络视图已完成加载。 (提示:在 func webViewDidFinishLoad(webView: UIWebView!) 中与 UIWebView 委托一起使用它)