我正在尝试找到共享UIWebView
内容的代码。
我在@IBAction
中得到了这个:
let activityViewController = UIActivityViewController(
activityItems: [textField.text as NSString],
applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: nil)
我需要做什么而不是textField.text
?
答案 0 :(得分:4)
要将textField.text
替换为网络视图的网址,您可以使用以下代码:
let activityViewController = UIActivityViewController (
activityItems: [(webView.request?.URL.absoluteString)! as NSString],
applicationActivities: nil
)
presentViewController(activityViewController, animated: true, completion: nil)
其中webView
是UIWebView
个实例。
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];
}
}