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;
}
{
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;
}
相关文章推荐
- iOS 中 NSParameterAssert
- 2016苹果开发者企业号申请流程详解
- iOS 十大控件
- iOS label button等设置不同颜色的标题
- ios动画效果
- iOS 收回键盘的3个方法
- IOS支付宝支付出现6002问题的解决办法
- IOS将字符串去掉斜杠,并转化成NSData数据后,再解析成字典
- iOS The data couldn't be read because it isn't in the correct format
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS之旅--隐藏(去除)导航栏底部横线
- IOS开发中(null)与<null>的处理
- 盗墓笔记(iOS新特性) 2016-01-18
- iOS9新特性Bitcode
- iOS开发之Masonry(二)
- iOS左是master,右边是detail
- 不让苹果开发者账号折磨我
- iOS 遍历字符串得到每一个字
- iOS runtime 消息分发过程
- 'libxml/tree.h' file not found