0

i am developing a app and whenever my app is installed on a iDevice, i must register that device, in the server through a post Web service, how to do that, This is what so far i have done:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    // iOS < 8 Notifications
    //[application registerForRemoteNotificationTypes:
    // (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
return YES;
}

and then i wrote the following,

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *devToken = [[[[deviceToken description]
                        stringByReplacingOccurrencesOfString:@"<"withString:@""]
                       stringByReplacingOccurrencesOfString:@">" withString:@""]
                      stringByReplacingOccurrencesOfString: @" " withString: @""];

[self registerDeviceTokenWithServer:devToken];

}

Next this method

-(void)registerDeviceTokenWithServer :(NSString*)deviceToken{

[NSThread detachNewThreadSelector:@selector(registerDeviceInBackground:)
                         toTarget:self withObject:deviceToken];
}

and now i need to register the mobile through the device token in the following method through a post call Web service, how to do that,

-(void)registerDeviceInBackground :(NSString*)deviceToken

{
   I need to write the code here a post call method.
}

If any code help is there, is well appreciated.

kool kims
  • 169
  • 2
  • 13

1 Answers1

0

You can write this:

-(void)registerDeviceTokenWithServer :(NSString*)deviceToken{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSURL *s = [NSURL URLWithString: @"www.apple.com/yoururl"];
        NSMutableURLRequest *requestURL = [NSMutableURLRequest requestWithURL:s cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.00];
        [requestURL setHTTPMethod:@"POST"];
        NSError *error = [[NSError alloc] init];
        if ([parameter isKindOfClass : [NSString class]]) {
            [requestURL setHTTPBody:[devToken dataUsingEncoding:NSUTF8StringEncoding]];
        }

        NSHTTPURLResponse *response;
        NSError *error1;
        NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error1];            
        dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:kNilOptions error:&error];
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([[dictionaryData objectForKey:@"status"] isEqualToString:@"success"]) {
                //Post successful
            }
            else if([[apiDataBack objectForKey:@"status"] isEqualToString:@"error"]){
                //error
            }
        });
    });
}

Above code will post the device token asynchronously.

Hope this helps.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73