iPhone开发基础学习 在程序里设置Push

移动开发 iOS
本文介绍的是iPhone开发基础学习 在程序里设置Push ,很详细的为大家讲解,我们先来看恩日。

iPhone开发基础学习 在程序里设置Push是本文要介绍的内容,最近做项目有一个需求,要在程序得系统设置里进行push的设置。在网上搜了几天资料没找着啥。今天忽然心血来潮跟踪系统注册push时得代码,居然发现有可行得解决方法,思路如下:

1、在iphone得framework里的UIApplication.h中有以下函数:

  1. @interface UIApplication (UIRemoteNotifications)  
  2. - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  3. - (void)unregisterForRemoteNotifications __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);       
  4. // calls -registerForRemoteNotificationTypes with UIRemoteNotificationTypeNone  
  5.  
  6. // returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity  
  7. - (UIRemoteNotificationType)enabledRemoteNotificationTypes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  8. @end 

2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]获取到允许得push推送类型。然后再调用registerForRemoteNotificationTypes进行修改。若要关闭程序得push服务,可调用unregisterForRemoteNotifications.

3、补充:以上想法以实现。补充部分代码。settingsData为tableview的数据源数组

a、获取系push设置,用于显示给用户

  1. //push设置  
  2.  
  3. NSMutableArray * pushOptions = [[NSMutableArray alloc] init];  
  4.  
  5. UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];  
  6.  
  7.  
  8. NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  9.                                      @"声音", @"name",  
  10.                                      @"0",   @"status",  
  11.                                      nil];  
  12. if (notificationType & UIRemoteNotificationTypeSound) {  
  13.     [soundNotice setValue:@"1" forKey:@"status"];  
  14. }          
  15. [pushOptions addObject:soundNotice];  
  16. [soundNotice release];  
  17.  
  18.  
  19. NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  20.                                      @"提醒", @"name",  
  21.                                      @"0",   @"status",  
  22.                                      nil];  
  23. if (notificationType & UIRemoteNotificationTypeAlert) {  
  24.     [alertNotice setValue:@"1" forKey:@"status"];  
  25. }          
  26. [pushOptions addObject:alertNotice];  
  27. [alertNotice release];  
  28.  
  29.  
  30. NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  31.                                      @"标记", @"name",  
  32.                                      @"0",   @"status",  
  33.                                      nil];  
  34. if (notificationType & UIRemoteNotificationTypeBadge) {  
  35.     [badgeNotice setValue:@"1" forKey:@"status"];  
  36. }          
  37. [pushOptions addObject:badgeNotice];  
  38. [badgeNotice release];  
  39.  
  40.  
  41. NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:  
  42.                               @"通知设置", @"groupName",  
  43.                               pushOptions,    @"data",  
  44.                               nil];  
  45. [self.settingsData addObject:pushConfig];  
  46. [pushOptions release];  
  47. [pushConfig release]; 

b、获取用户设置的数据放入pushdata,然后向系统提交设置

  1. NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];  
  2. NSInteger length = [pushData count];  
  3.  
  4. UIRemoteNotificationType myType = 0;  
  5.  
  6. for (NSInteger i =0; i< length; i++) {  
  7.     if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1) {  
  8.         switch (i) {  
  9.             case 0:        myTypemyType = myType|UIRemoteNotificationTypeSound;    break;  
  10.             case 1:        myTypemyType = myType|UIRemoteNotificationTypeAlert;    break;  
  11.             case 2:        myTypemyType = myType|UIRemoteNotificationTypeBadge;    break;  
  12.             default:    break;  
  13.         }  
  14.     }  
  15. }  
  16.  
  17. if (myType != 0) {  
  18.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];  
  19. }else {  
  20.     [[UIApplication sharedApplication] unregisterForRemoteNotifications];  

希望以上思路对有这方面需求得人有帮助。以上方案我暂未用于代码实现。若有问题。请留言共同商讨。

小结:iPhone开发基础学习 在程序里设置Push的内容介绍完了,希望本文对你有所帮助1更多相关内容请参考编辑推荐。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-18 14:33:32

2011-07-25 16:47:01

HTTP Server PUSH

2012-04-26 13:30:05

iPhoneApp Store发布程序

2011-07-07 17:04:33

iPhone Action Objective-

2011-07-18 14:59:20

iPhone Objective-

2011-07-25 18:07:29

iPhone Push Notificati

2011-08-10 16:44:56

iPhone代理设计模式

2011-03-08 16:57:13

proftpd

2011-08-08 10:10:14

iPhone开发 图片 方法

2011-08-01 18:27:58

iPhone开发 UISearchBa

2011-08-15 10:06:22

iPhone开发nib 文件

2011-08-09 17:29:29

iPhone文件屏幕

2011-07-18 09:35:29

iPhone 框架

2011-08-18 10:39:46

iPhone开发界面

2011-08-05 14:48:06

iPhone应用 异步队列

2011-07-20 17:10:05

iPhone iAd

2011-08-08 14:57:46

iPhone Autoreleas Property

2011-07-26 14:18:20

2011-07-18 13:56:19

2011-08-08 15:56:18

iPhone 震动 NSUserDefa
点赞
收藏

51CTO技术栈公众号