Iphone Sdk:是否可以使用UISwitch启用和禁用PNS(推送通知服务)?

时间:2010-10-06 11:57:37

标签: iphone sdk push-notification apple-push-notifications uiswitch

我找到了一些关于PNS的示例代码,article here

我还创建了一个UISwitch以启用PNS

如何给出一种控制PNS的方法?

这就是我宣告单元格的方式

cell.textLabel.text = @"PNS";
  [cell.textLabel setTextColor:[UIColor grayColor]];
  pushNotificationSwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
  [cell addSubview:pushNotificationSwitch];
  cell.accessoryView = pushNotificationSwitch;
  [(UISwitch *)cell.accessoryView addTarget:self action:@selector(pushNotification:) forControlEvents:UIControlEventValueChanged];
 }


- (void)pushNotification:(id)sender{
 if (pushNotificationSwitch.on==YES) {
  UITableViewCell *cell = (UITableViewCell*)pushNotificationSwitch.superview;
  [cell.textLabel setTextColor:[UIColor blackColor]];
 }
 else {
  UITableViewCell *cell = (UITableViewCell*)pushNotificationSwitch.superview;
  [cell.textLabel setTextColor:[UIColor grayColor]];
 }
} 

现在我只是使用单元格的文本标签颜色更改来表示交换机调用方法

SO ...我可以用它来控制PNS启用与否???

感谢您的任何意见和解答!

2 个答案:

答案 0 :(得分:5)

要使以下所有方法正常运行,您应该向Apple注册推送通知服务作为通知提供商。

根据用户对Switch控制输入的选择,您可以调用

  

unregisterForRemoteNotifications

  

registerForRemoteNotificationTypes

如果用户想要从通知中取消注册,可以通过调用unregisterForRemoteNotifications方法来实现。

如果您想再次注册通知,可以在Application对象上使用registerForRemoteNotificationTypes方法。

有关详细信息,请参阅此link

<强>更新

您可以这样称呼它:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

您可以使用我推荐的链接获取更多信息。

答案 1 :(得分:2)

您可以使用registerForRemoteNotificationTypes:为您的应用激活PNS,或使用unregisterForRemoteNotifications停用它。 有关详细信息,请参阅http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/registerForRemoteNotificationTypes: