最近做的項目需要把滴滴打車嵌入當前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]; //調用了這句,就會彈出允許框了.
歡迎大家提出意見,謝謝!