码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 8 本地推送补充

时间:2016-02-26 19:09:17      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:ios地图   ios   

-

- (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);

}



本文出自 “ZhuoKing” 博客,转载请与作者联系!

IOS 8 本地推送补充

标签:ios地图   ios   

原文地址:http://9951038.blog.51cto.com/9941038/1745318

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!