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

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,然后再进行初始化,代码如下:
- (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];  //调用了这句,就会弹出允许框了.


欢迎大家提出意见,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: