使用UIActivityViewController共享UiWebView内容

时间:2015-03-24 08:49:32

标签: ios swift uiwebview uiactivityviewcontroller

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

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

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

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

2 个答案:

答案 0 :(得分:4)

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

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

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

其中webViewUIWebView个实例。

P.S。在使用此代码之前,请确保Web视图已完成加载。 (提示:在func webViewDidFinishLoad(webView: UIWebView!)UIWebView委托)

中使用它

答案 1 :(得分:1)

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