Your delegate code is horrible

176 views
Skip to first unread message

Gargoyle

unread,
Apr 30, 2012, 1:17:26 PM4/30/12
to Easy APNs
Somebody should look into cleaning up the delegate code. It's pretty
bad. The enabled/disabled thing is WAY more verbose than needed. For
example, here's my entire method:


- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const NSDictionary *const info = [[NSBundle mainBundle]
infoDictionary];

// Check what Notifications the user has turned on. We registered
for all three, but they may have manually disabled some or all of
them.
const NSUInteger rntypes = [[UIApplication sharedApplication]
enabledRemoteNotificationTypes];

const UIDevice *const dev = [UIDevice currentDevice];

const NSString *const deviceToken = [[[[devToken description]

stringByReplacingOccurrencesOfString:@"<" withString:@""]

stringByReplacingOccurrencesOfString:@">" withString:@""]

stringByReplacingOccurrencesOfString: @" " withString: @""];

NSString *urlString = [[NSString stringWithFormat:@"http://
your.website.com/apns.php?task=register&appversion=%@&devicetoken=
%@&devicename=%@&devicemodel=%@&deviceversion=%@&pushbadge=
%@&pushalert=%@&pushsound=%@",
[info
valueForKey:@"CFBundleShortVersionString"],
deviceToken, dev.name, dev.model,
dev.systemVersion,
(rntypes &
UIRemoteNotificationTypeBadge) ? @"enabled" : @"disabled",
(rntypes &
UIRemoteNotificationTypeAlert) ? @"enabled" : @"disabled",
(rntypes &
UIRemoteNotificationTypeSound) ? @"enabled" : @"disabled"]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
self.connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self startImmediately:NO];
if (self.connection) {
[self.connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSRunLoopCommonModes];
[self.connection start];
}
}


Notice, for example, that your notification types are actually a
simple bitfield. Just check it directly, don't do all those string
checks.
Reply all
Reply to author
Forward
0 new messages