webView导入html5(滴滴打车)
2015-07-31 16:45
239 查看
最近做的项目需要把滴滴打车嵌入当前app中,主要使用的就是webView导入页面,下面就说一下导入的过程:
1. 创建一个UIViewController,将webView拖进页面。
2. 生成访问滴滴打车网页的url(只传必要参数就可以:经纬度,渠道号(需和滴滴签订协议才可以获取)):
“http://webapp.diditaxi.com.cn/?city=&maptype=wgs84&fromlat=%f&fromlng=%f&fromaddr=&toaddr=&toshop=&channel=%@",latitude,longitude,quDaoHao”
3. 定位,获取当前所在城市以及经纬度,主要使用CoreLocation.framework。一些是相关的步骤以及主要代码:
1)导入CoreLocation.framework,添加代理CLLocationManagerDelegate,定义一个CLLocationManager *_locManager,然后再进行初始化,代码如下:
2)重载方法locationManager,获取当前经纬度以及所在城市名称。代码如下:
3. webView通过生成的url,导入网页页面。代码如下:
注:如果url中含有中文字符,那么NSURL返回的就会是nil,这首需要讲中文转换成utf-8,使用方法:stringByAddingPercentEscapesUsingEncoding,反之,将utf-8格式转换成NSString,使用方法:stringByReplacingPercentEscapesUsingEncoding。
补充:重新修改了获取定位部分的代码,因为在ios 8版本之后,使用CLLocationManager定位时,需要获取定位服务的权限。所以,在初始化CLLocationManager变量部分添加了如下代码:
欢迎大家提出意见,谢谢!
1. 创建一个UIViewController,将webView拖进页面。
2. 生成访问滴滴打车网页的url(只传必要参数就可以:经纬度,渠道号(需和滴滴签订协议才可以获取)):
“http://webapp.diditaxi.com.cn/?city=&maptype=wgs84&fromlat=%f&fromlng=%f&fromaddr=&toaddr=&toshop=&channel=%@",latitude,longitude,quDaoHao”
3. 定位,获取当前所在城市以及经纬度,主要使用CoreLocation.framework。一些是相关的步骤以及主要代码:
1)导入CoreLocation.framework,添加代理CLLocationManagerDelegate,定义一个CLLocationManager *_locManager,然后再进行初始化,代码如下:
- (void)viewDidLoad { [super viewDidLoad]; if (!_locManager) { if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { //[[ShareTool shared] msgBox:@"您关闭了的定位功能,将无法收到位置信息,建议您到系统设置打开定位功能!"]; [XYMPromptView showInfo:@"您关闭了的定位功能,将无法收到位置信息,建议您到系统设置打开定位功能" bgColor:[UIColor blackColor].CGColor inView:[(AppDelegate *)[UIApplication sharedApplication].delegate window] isCenter:NO vertical:1]; } else { //开启定位 _locManager = [[CLLocationManager alloc] init];//创建位置管理器 _locManager.delegate=self; [_locManager setDesiredAccuracy:kCLLocationAccuracyBest]; _locManager.distanceFilter=1000.0f;//设置距离筛选器 if (_locManager != nil) { [_locManager startUpdatingLocation]; //在ios 8.0下要授权 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) [_locManager requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了. } } } // Do any additional setup after loading the view. }
2)重载方法locationManager,获取当前经纬度以及所在城市名称。代码如下:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLLocationCoordinate2D loc = [newLocation coordinate]; lat = loc.latitude; lon = loc.longitude;//获取经纬度 //获取当前城市,不是必须参数,可不获取 CLGeocoder* geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { if (!error) { if (placemarks.count > 0) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; //city =[NSString stringWithFormat:@"%@",placemark.locality]; //city = [NSString stringWithFormat:@"%@%@",placemark.administrativeArea,placemark.locality]; city =placemark.locality; NSLog(@"_lastCity is %@ ",city); } } [_locManager stopUpdatingLocation]; }]; }
3. webView通过生成的url,导入网页页面。代码如下:
-(void) initWebView:(NSString*) urlTmp { if([urlTmp hasPrefix:@"http"])//网络url { NSString* tmp = [urlTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:tmp]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; } else//urlTmp是html格式内容的字符串 { urlTmp = [urlTmp stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; urlTmp = [urlTmp stringByReplacingOccurrencesOfString:@"+" withString:@" "]; [webView loadHTMLString:urlTmp baseURL:nil]; } }
注:如果url中含有中文字符,那么NSURL返回的就会是nil,这首需要讲中文转换成utf-8,使用方法:stringByAddingPercentEscapesUsingEncoding,反之,将utf-8格式转换成NSString,使用方法:stringByReplacingPercentEscapesUsingEncoding。
补充:重新修改了获取定位部分的代码,因为在ios 8版本之后,使用CLLocationManager定位时,需要获取定位服务的权限。所以,在初始化CLLocationManager变量部分添加了如下代码:
//在ios 8.0下要授权 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) [_locManager requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了.
欢迎大家提出意见,谢谢!
相关文章推荐
- mobile web开发 图片展示控件PhotoSwipe android手势放大功能
- iOS开发——Xcode基本操作
- springmvc中@RequestMapping 和 controller 小结
- Android Notification功能的实现
- 小韵游戏开发学习笔记——第五期:认识Unity基本组件(二)
- android 杂记6:Android xml资源文件的一些东西
- 斯坦福大学iOS8公开课笔记01-IOS8结构和MVC模式
- 关于iOS多线程,你看我就够了(已更新)
- iOS学习之KVO、KVC
- 【Android应用开发技术:媒体开发】MediaPlayer基本原理
- Android LaunchMode and StartActivityForResult
- android rectF
- 在源码中编译自己的Android project
- Unity3d开发过程记录随笔
- 微信公众账号 点击返回页面刷新页面得到两个相同的code导致程序报错的问题
- Android远程服务一:android AIDL远程服务调用
- Unity中实现全局管理类的几种方式
- Android 列表单一展开隐藏视图
- cocos2d-x博客网站推荐和牛逼的教程
- Object-C 中的日期(NSDate)