我正在开发一个应用程序,每当我的应用程序安装在iDevice上时,我必须通过post Web服务在服务器中注册该设备,如何做到这一点,这是迄今为止我所做的:
-(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;
}
然后我写了以下内容,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[self registerDeviceTokenWithServer:devToken];
}
接下来的方法
-(void)registerDeviceTokenWithServer :(NSString*)deviceToken{
[NSThread detachNewThreadSelector:@selector(registerDeviceInBackground:)
toTarget:self withObject:deviceToken];
}
现在我需要通过post call Web服务通过以下方法通过设备令牌注册移动设备,如何做到这一点,
-(void)registerDeviceInBackground :(NSString*)deviceToken
{
I need to write the code here a post call method.
}
如果有任何代码帮助,我们将不胜感激。
答案 0 :(得分:0)
你可以这样写:
-(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
}
});
});
}
以上代码将异步发布设备令牌。
希望这会有所帮助.. :)