IOS 8 本地推送补充

简介:

-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    _window.backgroundColor = [UIColor whiteColor];

    [_window makeKeyAndVisible];

    ScrollContentViewController * scrollcontentView = [[ScrollContentViewController alloc]init];

    UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:scrollcontentView];

    _window.rootViewController = navigationController;

    UIView * statusBarView = [[UIView alloc]initWithFrame:CGRectMake(0, -20, 320, 64)];

    statusBarView.backgroundColor = [UIColor colorWithRed:0 green:122/255.0f blue:247/255.0f alpha:1];

    [navigationController.navigationBar addSubview:statusBarView];


    [application setStatusBarHidden:NO];

    [application setStatusBarStyle:UIStatusBarStyleLightContent];

    //注册推送(ios 8

    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])

    {

        [[UIApplication   sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeAlert |UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];

    }


    return YES;


-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

    //接受本地推送

    NSLog(@"%@",notification);

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定"otherButtonTitles: nil];

    [alert show];

    //图标上的数字减1

    application.applicationIconBadgeNumber -=1;

    

    //解除本地推送

    //获得uiapplication

    UIApplication * app = [UIApplication sharedApplication];

    //获取本地推送数组

    NSArray * localArray = [app scheduledLocalNotifications];

    //声明本体通知对象

    UILocalNotification * localNotification;

    if (localArray)

    {

        for (UILocalNotification * noti in  localArray)

        {

            NSDictionary * dict = noti.userInfo;

            if (dict)

            {

                NSString * inKey = [dict objectForKey:@"key"];

                if ([inKey isEqualToString:@"对应的key"])

                {

                    if (localNotification)

                    {

                        localNotification = nil;

                    }

                    break;

                }

            }

        }

        //判断是否找到已经存在的相同key的推送

        if (!localNotification)

        {

            //不存在初始化

            localNotification = [[UILocalNotification alloc]init];

        }

        if (localNotification)

        {

            //不推送 取消推送

            [app cancelLocalNotification:localNotification];

            return;

        }

    }


}


.....

- (void)viewDidLoad {.....}

-(void)SendNotification:(UIButton *)sender

{  //创建本地推送

    NSDate * now = [NSDate date];

    UILocalNotification * reminderNotification = [[UILocalNotification alloc]init];

    //设置推送时间

    [reminderNotification setFireDate:[now dateByAddingTimeInterval:10]];

    //设置时区

    [reminderNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    //设置userinfo 方便在之后需要撤销的时候使用

    reminderNotification.userInfo = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];

    //设置推送内容

    [reminderNotification setAlertBody:@"Don't forget to Show Out !"];

    [reminderNotification setAlertAction:@"Show Out"];

    [reminderNotification setCategory:@"alert"];

    //设置推送声音

    [reminderNotification setSoundName:UILocalNotificationDefaultSoundName];

    //显示在icon上的红色圈子的数子

    [reminderNotification setApplicationIconBadgeNumber:1];

    //添加推送到UIApplication

    [[UIApplication   sharedApplication]scheduleLocalNotification:reminderNotification];

    

    NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication]currentUserNotificationSettings]);

    [[UIApplication sharedApplication] isRegisteredForRemoteNotifications ];

    

    UIAlertView * successAlert = [[UIAlertView   alloc]initWithTitle:@"Reminder" message:@"Your Reminder has been Scheduled" delegate:nilcancelButtonTitle:@"OK Thanks ! " otherButtonTitles: nil];

    [successAlert show];



 


}

IOS8定位问题 


 /**

 *1:先在info.plist中添加NSLocationAlwaysUsageDescription设置为字符串类型,为YES;

 *2:info.plist中添加NSLocationWhenInUseUsageDescription设置为字符串类型,为YES;

 *3:创建CLLocationManager对象

 *4    //创建对象

 *           self.locationManager=[[CLLocationManager alloc]init];

 *           //设置代理

 *          self.locationManager.delegate=self;

 *           //请求

 *          [self.locationManager  requestWhenInUseAuthorization];

 *           //类型

 *           self.locationManager.desiredAccuracy=kCLDistanceFilterNone;

 *           //开始

 *          [self.locationManager  startUpdatingLocation];

 *5:写代理方法-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

 */

    //创建对象

    self.locationManager=[[CLLocationManager alloc]init];

    //设置代理

    self.locationManager.delegate=self;

    //请求

    [self.locationManager  requestAlwaysAuthorization];

    //类型

    self.locationManager.desiredAccuracy=kCLDistanceFilterNone;

    //开始定位

    [self.locationManager  startUpdatingLocation];


    

#pragma mark 代理方法

//此方法会在用户授权状态改变时调用

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    switch (status)

    {

        case kCLAuthorizationStatusNotDetermined:

            if ([self.locationManager  respondsToSelector:@selector(requestWhenInUseAuthorization)])

            {

                [self.locationManager requestAlwaysAuthorization];

            }

            break;

        default:

            break;

    }

}




 //更新位置的代理方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations

{   //use locations

    NSLog(@"=========%@",locations);

    //根据经纬度解析成位置

    CLGeocoder *geocoder=[[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:locations[0] completionHandler:^(NSArray*placemark,NSError *error)

     {

         CLPlacemark *mark=[placemark objectAtIndex:0];

        

       NSString *  title=[NSStringstringWithFormat:@"%@%@%@",mark.subLocality,mark.thoroughfare,mark.subThoroughfare];

       NSString   * subTitle=[NSString stringWithFormat:@"%@",mark.name];//获取subtitle的信息

         

         NSLog(@"``````%@~~~~~~~%@_______",title,subTitle);

         

     } ];

}

//定位失败信息

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    

    NSLog(@"--------%@---------",error);

}










本文转自 卓行天下  51CTO博客,原文链接:http://blog.51cto.com/9951038/1745318,如需转载请自行联系原作者
目录
相关文章
|
iOS开发
IOS - iOS 12 的“隐式推送”功能怎么用?
IOS - iOS 12 的“隐式推送”功能怎么用?
332 0
IOS - iOS 12 的“隐式推送”功能怎么用?
|
传感器 iOS开发
iOS 14.2正式版推送,多项改进,建议更新
近日苹果推送了iOS/iPadOS 14.2的正式版,虽然更新包只有700多兆,但内容还是非常丰富的。
116 0
iOS 14.2正式版推送,多项改进,建议更新
|
开发工具 iOS开发 开发者
iOS13即将到来,iOS推送Device Token适配详解
关于提前适配iOS13 苹果推送DeviceToken的通知 随着苹果iOS13系统即将发布,个推提前推出DeviceToken适配方案,以确保新版本的兼容与APP推送服务的正常使用。iOS13的一个重要变化是"[deviceTokendescription]" 会受不同运行环境及系统的影响而发生变化,如果未及时做好适配工作,会导致SDK绑定到错误的DeviceToken,从而影响APN推送。
3517 0
|
开发工具 Android开发 iOS开发
搞定iOS推送SDK集成,看这篇文章就够了!
一次偶然的机会,公司的项目要用到推送,我自己本来就很懒,不愿意去弄整套APNS的流程,刚好之前跟朋友聊起过他们的产品中集成了个推的Android推送,说是体验还可以,那这次我就试一下他们的iOS推送。
3020 0
|
开发工具 iOS开发
iOS推送SDK集成对比
由于自己的app想要一个推送功能,又由于调用系统的方法集成太麻烦,所以想找一个推送的SDK来做。市面上可以实现该功能的SDK有多种,比如极光,信鸽,个推,MobPush(ShareSDK他们家的)。
2278 0
|
存储 iOS开发 数据格式
iOS刚进入后台接受推送、打开推送调转到相应的界面
刚进入后台的时候消息处理时候我用了本地推送!可以看我前边写的博客,怎么处理刚进入后台接收推送的案例,链接 /**  注释:打开推送的三种方式:-peter  1、apns的时候,结束进程退出后台:启动的时候可在 didFinishLaunchingWithOptions的launchOptions中...
1186 0