天天看點

IOS 8 本地推送補充

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:

 //更新位置的代理方法

-(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/1615470,如需轉載請自行聯系原作者

繼續閱讀