ios8下LocationManager及Nsnotification使用
2014-09-29 08:58
281 查看
location service使用
ios8之前locationservice在应用开启
self.locationManager = [[CLLocatonManager alloc] init]
[self.locationManager startUpdatingLocation];
在iOS8上需要加上kCLAuthorizationStatusAuthorizedAlways,在app中的info.plist中添加NSLocationAlwaysUsageDescription 或者 NSLocationWhenInUseUsageDescription其中一个key,value的值可以任意填写,会在请求获取位置的对话框中显示
请求获取位置权限
// iOS 8 - request location services via requestWhenInUseAuthorization.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
// iOS 7 - We can't use requestWhenInUseAuthorization -- we'll get an unknown selector crash!
// Instead, you just start updating location, and the OS will take care of prompting the user
// for permissions.
[self.locationManager startUpdatingLocation];
}
实现代理方法,当获取到这个权限时
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// We only need to start updating location for iOS 8 -- iOS 7 users should have already
// started getting location updates
if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[manager startUpdatingLocation];
}
}
2.ios8设置获取通知的权限和之前有所不同
#ifdef __IPHONE_8_0 if(SYSTEM_VERSION_LESS_THAN(@"8.0")){ UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; } else{ UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:notificationSettings]; [application registerForRemoteNotifications]; } #else UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];#endif获取remotenotification
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ completionHandler(UIBackgroundFetchResultNewData); NSLog(@"remote notifation %@",userInfo);
}
//获取到推送的通知- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ }
设置setbadgeNumber
- (void)setApplicationBadgeNumber:(NSInteger)badgeNumber{ UIApplication *application = [UIApplication sharedApplication]; #ifdef __IPHONE_8_0 // compile with Xcode 6 or higher (iOS SDK >= 8.0) if(SYSTEM_VERSION_LESS_THAN(@"8.0")) { application.applicationIconBadgeNumber = badgeNumber; } else { if ([self checkNotificationType:UIUserNotificationTypeBadge]) { NSLog(@"badge number changed to %d", badgeNumber); application.applicationIconBadgeNumber = badgeNumber; } else NSLog(@"access denied for UIUserNotificationTypeBadge"); } #else // compile with Xcode 5 (iOS SDK < 8.0) application.applicationIconBadgeNumber = badgeNumber; #endif}
本文出自 “享受编程” 博客,请务必保留此出处http://demidroid.blog.51cto.com/2954092/1559228
ios8之前locationservice在应用开启
self.locationManager = [[CLLocatonManager alloc] init]
[self.locationManager startUpdatingLocation];
在iOS8上需要加上kCLAuthorizationStatusAuthorizedAlways,在app中的info.plist中添加NSLocationAlwaysUsageDescription 或者 NSLocationWhenInUseUsageDescription其中一个key,value的值可以任意填写,会在请求获取位置的对话框中显示
请求获取位置权限
// iOS 8 - request location services via requestWhenInUseAuthorization.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
// iOS 7 - We can't use requestWhenInUseAuthorization -- we'll get an unknown selector crash!
// Instead, you just start updating location, and the OS will take care of prompting the user
// for permissions.
[self.locationManager startUpdatingLocation];
}
实现代理方法,当获取到这个权限时
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// We only need to start updating location for iOS 8 -- iOS 7 users should have already
// started getting location updates
if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[manager startUpdatingLocation];
}
}
2.ios8设置获取通知的权限和之前有所不同
#ifdef __IPHONE_8_0 if(SYSTEM_VERSION_LESS_THAN(@"8.0")){ UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; } else{ UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:notificationSettings]; [application registerForRemoteNotifications]; } #else UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];#endif获取remotenotification
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ completionHandler(UIBackgroundFetchResultNewData); NSLog(@"remote notifation %@",userInfo);
}
//获取到推送的通知- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ }
设置setbadgeNumber
- (void)setApplicationBadgeNumber:(NSInteger)badgeNumber{ UIApplication *application = [UIApplication sharedApplication]; #ifdef __IPHONE_8_0 // compile with Xcode 6 or higher (iOS SDK >= 8.0) if(SYSTEM_VERSION_LESS_THAN(@"8.0")) { application.applicationIconBadgeNumber = badgeNumber; } else { if ([self checkNotificationType:UIUserNotificationTypeBadge]) { NSLog(@"badge number changed to %d", badgeNumber); application.applicationIconBadgeNumber = badgeNumber; } else NSLog(@"access denied for UIUserNotificationTypeBadge"); } #else // compile with Xcode 5 (iOS SDK < 8.0) application.applicationIconBadgeNumber = badgeNumber; #endif}
本文出自 “享受编程” 博客,请务必保留此出处http://demidroid.blog.51cto.com/2954092/1559228
相关文章推荐
- Android中LocationManager的简单使用,获取当前位置
- Android中LocationManager的简单使用,获取当前位置
- MMLocationManager简单使用
- iOS8之后CoreLocation定位的使用
- iOS-地图定位、CLLocationManager简单使用
- Android LocationManager 使用
- iOS8 中使用定位功能必须先获取用户授权,无论CLLocationManager还是MapKit
- shows user location在iOS8上不能使用地图定位
- [原创]Android中LocationManager的简单使用,获取当前位置
- Android中LocationManager的简单使用01
- Android中LocationManager的简单使用03
- shows user location在iOS8上不能使用地图定位
- Core Location Manager在iOS8中的改变
- Android中LocationManager的简单使用,获取当前位置(转http://www.javaeye.com/topic/363950)
- Android中LocationManager的简单使用02
- android LocationManager的使用
- iOS8 CLLocationManager 的改变导致 locationManager didUpdateLocations不能被调用的解决办法
- Core Location Manager Changes in iOS 8(iOS8获取不到地理位置)
- iOS CoreLocation使用(iOS8 回调不行的问题解决)
- Android中LocationManager的简单使用01