views:

98

answers:

2

I find some sample code about PNS,article here

and I also create an UISwitch to enable PNS

how to give a method to control the PNS ?

This is how I declare cell

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]];
 }
} 

now I'm just use a cell's text label color change to representation the switch is call the method

SO... can I use it to control PNS enable or not ???

Thanks for any comments and answers !

+2  A: 

You can activate the PNS for your app with registerForRemoteNotificationTypes: or deactivate it with unregisterForRemoteNotifications. See http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/registerForRemoteNotificationTypes: for more information:

AlexVogel
How to put this into - (void)pushNotification:(id)sender ?can you give some sample code ?thx
WebberLai
+1  A: 

For all the following to work, you should have registered with Apple for Push notification services as a Notification provider.

According to user's choice of input from Switch control, you can call

unregisterForRemoteNotifications

or

registerForRemoteNotificationTypes

.

If user wants to unregister from Notification, this is possible by invoking unregisterForRemoteNotifications method.

Once again if you wanted to register for the notification, you can use registerForRemoteNotificationTypes method on your Application object.

For more info you can refer this link.

UPDATE:

You can call it this way:

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

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

You can use the links I have referred for more info.

Krishnan
I declare this two method at appdelegate.m,I still can call this at other place ?
WebberLai
If you are referring to "unregisterForRemoteNotifications" and "registerForRemoteNotificationTypes", these two of them can be called on your shared Application instance. I have updated the answer on how this call can be made.
Krishnan
Thanks ,I just figure out the PNS certification issues.I will try this now if it work's I will tell you !
WebberLai
may I ask why every time I turn on the switch,and go to other view,and back to the view with switch,it will become turn off ? how to record the last status I click the switch ?
WebberLai
Hi, You can get the state of switch when the View disappears(viewdiddisappear), and store in NSUserDefaults or in a global variable and set the Switch to stored state, when the view again appears.
Krishnan
I nerver try this before ,I will give it a shoot
WebberLai