打开外部链接
/// 打开外部链接
+ (void)openScheme:(NSString *)scheme {
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
if (@available(iOS 10.0, *)) {
[application openURL:URL options:@{} completionHandler:^(BOOL success) {
NSLog(@"Open %@: %d",scheme,success);
}];
} else {
// Fallback on earlier versions
BOOL success = [application openURL:URL];
NSLog(@"Open %@: %d",scheme,success);
}
}
Safari打开链接
[self openScheme:@"https://www.baidu.com/"];
设置,打开定位权限设置
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "GACityModel.h"
@interface GALocationModel : NSObject <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager* locationManager;
@property (strong, nonatomic) NSString *dLongitude; // 经度
@property (strong, nonatomic) NSString *dLatitude; // 纬度
@property (strong, nonatomic) NSString *dCityName; // 城市名称
@property (strong, nonatomic) GACityModel *dCityModel; // 选择设置的城市
+ (id)sharedLocationModelMethod;
/// 设置城市
- (void)setSelectCityWithModel:(GACityModel *)tempCityModel;
/// 开始定位
- (void)startLocationAction;
/// 计算两个经纬度之间的距离
- (double)distanceBetweenOrderBy:(double)lat1 :(double)lng1 :(double)lat2 :(double)lng2;
@end
#import "GALocationModel.h"
static GALocationModel * sharedLocationModel = nil;
@implementation GALocationModel
+ (id)sharedLocationModelMethod {
@synchronized (self){
if (!sharedLocationModel) {
sharedLocationModel = [[GALocationModel alloc] init];
// 默认重庆主城区的经纬度
sharedLocationModel.dLongitude = @"106.33";
sharedLocationModel.dLatitude = @"29.35";
}
return sharedLocationModel;
}
return sharedLocationModel;
}
- (void)setSelectCityWithModel:(GACityModel *)tempCityModel {
if (tempCityModel) {
_dCityModel = tempCityModel;
}
}
- (void)startLocationAction {
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
// 定位功能可用
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 5.0;
}
[_locationManager startUpdatingLocation];
} else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
// 定位不能用
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[GAPublicClass openScheme:UIApplicationOpenSettingsURLString];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:cancel];
[alert addAction:ok];
UIViewController *viewC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[viewC presentViewController:alert animated:YES completion:nil];
}
}
#pragma mark - 定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
[_locationManager stopUpdatingLocation];
CLLocation *currentLocation = [locations lastObject];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
// 当前的经纬度
NSLog(@"当前的经纬度 %f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
_dLongitude = [NSString stringWithFormat:@"%.6f",currentLocation.coordinate.longitude];
_dLatitude = [NSString stringWithFormat:@"%.6f",currentLocation.coordinate.latitude];
// 地理反编码 可以根据坐标(经纬度)确定位置信息(街道 门牌等)
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
self.dCityName = placeMark.locality;
if (!self.dCityName) {
self.dCityName = @"无法定位当前城市";
}
if (!self.dCityModel) {
self.dCityModel = [GACityModel new];
self.dCityModel.dRegionName = self.dCityName;
NSNotification *tempNF = [[NSNotification alloc] initWithName:@"HomeLeftBarItemNF" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:tempNF];
}
} else if (error == nil && placemarks.count) {
NSLog(@"NO location and error return");
} else if (error) {
NSLog(@"loction error:%@",error);
}
}];
}
#pragma mark - 定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
}
- (double)distanceBetweenOrderBy:(double)lat1 :(double)lng1 :(double)lat2 :(double)lng2 {
CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
double distance = [curLocation distanceFromLocation:otherLocation];
return distance;
}
@end
#import <Foundation/Foundation.h>
@interface GACityModel : NSObject
@property (strong, nonatomic) NSString *dMark;
@property (strong, nonatomic) NSString *dRegionCode;
@property (strong, nonatomic) NSString *dRegionName;
- (id)initModelWithObject:(id)object;
@end