今天来分享一下开发flutter遇到的一两个问题:
问题一:
flutter打开原生的WKWebView页面,想要禁止用户长按操作。
亲测无效的各种:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.documentElement.style.webkitTouchCallout='none';", completionHandler: nil)
webView.evaluateJavaScript("document.documentElement.style.webkitUserSelect='none';", completionHandler: nil)
}
if (@available(iOS 11.0, *)){
for (UIView* subview in wkWebView.scrollView.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"WKContentView")])
{
for (UIGestureRecognizer* longPress in subview.gestureRecognizers) {
if ([longPress isKindOfClass:UILongPressGestureRecognizer.class]) {
[subview removeGestureRecognizer:longPress];
return;
}
}
}
}
}
for (UIView* subview in wkWebView.scrollView.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"WKContentViewMinusAccessoryView")])
{
for (UIGestureRecognizer* longPress in subview.gestureRecognizers) {
if ([longPress isKindOfClass:UILongPressGestureRecognizer.class]) {
[subview removeGestureRecognizer:longPress];
}
}
}
}
//禁止选择 css 配置相关
NSString*css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
//css 选中样式取消
NSMutableString*javascript = [NSMutableStringstring];
[javascript appendString:@"var style = document.createElement('style');"];
[javascript appendString:@"style.type = 'text/css';"];
[javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
[javascript appendString:@"style.appendChild(cssContent);"];
[javascript appendString:@"document.body.appendChild(style);"];
[javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];//禁止选择
[javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按
//javascript 注入
WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
WKUserContentController*userContentController = [[WKUserContentControlleralloc] init];
[userContentController addUserScript:noneSelectScript];
WKWebViewConfiguration*configuration = [[WKWebViewConfigurationalloc] init];
configuration.userContentController = userContentController;
// 最后加载进入js
[self.wkWebView.configuration.userContentControlleraddUserScript:noneSelectScript];
下面是解决方法
- 简要描述:开发环境:Xcode:11.2 ,iOS:13.2。然后自己创建一个UILongPressGestureRecognizer替换掉系统的,即可。
第一步:
private var longP : UILongPressGestureRecognizer?
第二步:
let longP_ = UILongPressGestureRecognizer.init(target: self, action: #selector(longTapAction))
longP = longP_
longP!.delegate = self
WKWeb.addGestureRecognizer(longP!)
第三步:实现代理方法
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == longP {
return true
} else {
return false
}
}
- 说明:
/// 此方法指定只有gestureRecognizer被系统判定失败时,才会执行otherGestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
/// 此方法指定只有otherGestureRecognizer被系统判定失败时,才会执行
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
问题二:
flutter在自动横竖屏切换的问题
问题描述:在开发flutter的时候,有的视频播放页面需要进行横屏切换,结果只显示了一半。
下面是解决方法
- 简要描述:开发环境同上,视频播放用的是WKWebView
第一步:添加监听
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
第二步:
@objc func deviceOrientationDidChange() {
WKWeb.frame = UIScreen.main.bounds
}
问题三:
禁止截屏,录屏
// 禁止截屏
weak var weakSelf = self
NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: OperationQueue.main) { (_) in
print("接到用户截屏的通知....")
weakSelf?.notificationScreenshot()
}
// 禁止录屏
if #available(iOS 11.0, *) {
if UIScreen.main.isCaptured {
NotificationCenter.default.addObserver(forName: UIScreen.capturedDidChangeNotification, object: nil, queue: OperationQueue.main) { (_) in
weakSelf?.notificationScreenshot()
}
}
} else {
// Fallback on earlier versions
}
- 说明,一定要在AppDelegate中注册监听,否则回调有时候不好使。