H5與原生交互的坑

視頻播放

1.使用H5進行視頻播放且自動使用系統播放器,在全屏變為小屏,并返回上個界面的時候,出現視頻聲音沒有關閉的情況。
解決辦法:

讓webview調用一個 about:blank的url。就可以停止視頻播放。
-(void) viewDidDisappear:(BOOL)animated
{
NSLog(@"媒體打開窗口被隱藏");
[webViewer loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
}

H5界面過長,滑動流暢,在點擊菜單滑動到指定位置的時候,出現白屏,需要滑動,才能顯示內容的時候。

解決辦法:
http://www.lxweimin.com/p/171b8b7761cb

可以判斷是否使用了UIWebOverflowScrollView,給webVIew添加點擊手勢,在點擊方法中,對scrollView進行重新布局。代碼如下:
//增加手勢 解決白屏問題
UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.testWeb addGestureRecognizer:myTap];
myTap.delegate = self;
myTap.cancelsTouchesInView = NO;

//手勢方法
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
CGPoint gesturePoint = [sender locationInView:self.view];
NSLog(@"handleSingleTap!gesturePoint:%f,y:%f",gesturePoint.x,gesturePoint.y);
UIView *scrollView = self.testWeb;
while ([scrollView.subviews count]) {
scrollView = scrollView.subviews[0];
if ([scrollView isMemberOfClass:NSClassFromString(@"UIWebOverflowScrollView")]) {
CGRect frame = scrollView.superview.frame;
frame.origin.x = 0;
scrollView.frame = frame;
break;
}
}
}

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
    return YES;
    }

當使用webView與H5進行交互的時候,出現傳遞多個參數的情況,使用JSExportAs

解決方法

例子如下:
JSExportAs(onJSInvokeResult, - (void)onJSInvokeResult:(NSInteger)type Result:(NSString*)title);

WKWebView與原生交互

http://www.cnblogs.com/jiang-xiao-yan/p/5345893.html
使用

window.webkit.messageHandlers.<name>.postMessage(<messageBody>)

來定義方法調用

UIWebView去除系統復制粘貼功能

- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Disable user selection
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
// Disable callout
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];}

[reason:accessing _cachedSystemAnimationFence requires the main thread 需要訪問主線程]

問題出現的可能

  在輸入法彈出的情況下,點擊按鈕調用js方法,進行拍照,可能在子線程中進行調用,從而出現上述錯誤,需要放到主線程中進行調用

dispatch_async(dispatch_get_main_queue(), ^{
    [weakSelf.WebViewController self.pickController animated:YES completion:NULL];
});
dispatch_async(dispatch_get_main_queue(), ^{
    [picker dismissViewControllerAnimated:YES completion:^{
        // 改變狀態欄的顏色  改變為白色
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }];
});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容