您的位置:首页 > 移动开发 > IOS开发

H5页面打开自动播放,H5加载,H5和iOS 交互

2016-01-18 11:22 573 查看
-(void)createWebView
{

UIWebView * webView = [[UIWebView
alloc]initWithFrame:CGRectMake(0,
0, UIScreenWidth,
UIScreenHeight+64)];
webView.backgroundColor = [UIColor
clearColor];
webView.scrollView.scrollEnabled =
YES;
webView.delegate =
self;
[self.view
addSubview:webView];

webView.scalesPageToFit =
YES;//自动对页面进行缩放以适应屏幕
webView.allowsInlineMediaPlayback =
YES;
webView.autoresizesSubviews =
YES; //自动调整大小
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth);
webView.suppressesIncrementalRendering =YES;
webView.keyboardDisplayRequiresUserAction =
YES;
webView.mediaPlaybackAllowsAirPlay =
YES; //播放视频的背景音频
webView.mediaPlaybackRequiresUserAction =
NO;
// 那么mediaPlaybackRequiresUserAction设置为YES默认,从而防止无需用户交互任何媒体播放。
// 如果设置mediaPlaybackRequiresUserAction为NO,音频和视频在你的UIWebView可以在它自动启动。
webView.opaque =
NO;
webView.contentMode =
UIViewContentModeRedraw;

NSString * urlStr = [NSString
stringWithFormat:@"%@",@“https://www.baidu.com/”];
NSLog(@"%@",urlStr);
NSURL *url =[NSURL
URLWithString:urlStr];
NSURLRequest *request =[NSURLRequest
requestWithURL:url];
[webView loadRequest:request];

}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {

//H5 和 iOS
交互
// NSString *requestString = [[request URL] absoluteString];//获取请求的绝对路径.
// NSArray *components = [requestString componentsSeparatedByString:@":"];//提交请求时候分割参数的分隔符
// if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
// //过滤请求是否是我们需要的.不需要的请求不进入条件
// if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])
// {
// NSString *message = [NSString stringWithFormat:@"%@", [components objectAtIndex:2]];
// NSLog(@"%@",message);
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JS向APP提交数据" message:message delegate:self cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
// [alert show];
// }
// return NO;
// }
return
YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: