webView导入html5(滴滴打车)

3 篇文章 0 订阅

最近做的项目需要把滴滴打车嵌入当前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];  //调用了这句,就会弹出允许框了.

欢迎大家提出意见,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值