ios8下LocationManager及Nsnotification使用

发布时间:2020-06-10 19:58:40 作者:Demidroid
来源:网络 阅读:856
  1. 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

}


推荐阅读:
  1. Android源码个个击破之LocationManager
  2. iOS8 推送 获取 devicetoken

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

使用 nsnotification ios8

上一篇:华为桌面云(FA)交付前期准备工作

下一篇:运用php上传大文件配置方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》