Powered By Blogger

Thursday, May 23, 2013

How to resolve apple's UDID issue for Push Notification in iPhone?

Hi,
Everyone,

Rumors say that Apple starts to prohibit the use of UDID in iOS applications from iOS 6.0 and onwards. You can use below solution for  < & >=  iOS 6.0 .

- The device token (used by APNS) and the UDID are two different things. They have nothing to do with each other.

- You obtain the device token for push notifications in your app delegate's application:didRegisterForRemoteNotificationsWithDeviceToken: method.


- The documentation notes the following:
The device token is different from the uniqueIdentifier property of UIDevice because, for security and privacy reasons, it must change when the device is wiped.


#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

NSString *deviceID = nil;

#pragma mark -------------------
#pragma mark UIApplication methods
#pragma mark -------------------

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (![[NSUserDefaults standardUserDefaults] valueForKey:@"UUID"]) {
        if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
            self.deviceID = [[self GetUUID] retain];
        }
        else{
            NSUUID* udid= [UIDevice currentDevice].identifierForVendor;
            self.deviceID = [[udid UUIDString] retain];
        }
        [[NSUserDefaults standardUserDefaults] setValue:self.deviceID forKey:@"UUID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else{
        self.deviceID = [[[NSUserDefaults standardUserDefaults] valueForKey:@"UUID"] retain];
    }
}

#pragma mark -------------------
#pragma mark Push notification methods
#pragma mark -------------------

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    //NSLog(@"My token is: %@", deviceToken);
    NSString *devicetoken = [[[[deviceToken description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];
    [self updateToken:devicetoken];
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    //NSLog(@"Failed to get token, error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // get state
    UIApplicationState state=[application applicationState];
    if(state== UIApplicationStateActive)
        self.badgeCount=[[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] intValue];
    else
        self.badgeCount= [UIApplication sharedApplication].applicationIconBadgeNumber;
}

- (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

Thanks, 
Nilesh M. Prajapati

No comments:

Post a Comment