Iphone 如何将设备令牌和应用程序版本发送到服务器

Iphone 如何将设备令牌和应用程序版本发送到服务器,iphone,Iphone,我已实现将设备令牌和应用程序版本发送到serverm。它在模拟器(硬编码数据)中工作正常,但在设备中不工作。 任何形式的帮助都将不胜感激。 先谢谢你 这是密码 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken { // Get Bundle Info for Remote Registration (h

我已实现将设备令牌和应用程序版本发送到serverm。它在模拟器(硬编码数据)中工作正常,但在设备中不工作。 任何形式的帮助都将不胜感激。 先谢谢你

这是密码

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {


    // Get Bundle Info for Remote Registration (handy if you have more than one app)
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

    // Prepare the Device Token for Registration (remove spaces and < >)
    NSString *deviceToken = [[[[devToken description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSMutableString *urlString = [[BASE_URL mutableCopy] autorelease];
    [urlString appendFormat:@"traceDeviceTokenId?tokenid=%@&version=%@",deviceToken, appVersion];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSString *postLength = [NSString stringWithFormat:@"%d", [urlString length]];       
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
    [request setHTTPMethod:@"POST"]; 

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"text/xml; charset=utf-16" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[urlString dataUsingEncoding:NSUTF16StringEncoding]];
    NSLog(@"Request xml>>>>>> %@", urlString);

    NSError *error; 
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *responseXml = [[NSString alloc] initWithData:urlData encoding:NSUTF16StringEncoding];
    NSLog(@"Response xml>>>>>> = %@", responseXml);

}
-(void)应用程序:(UIApplication*)应用程序DIDregisterForRemotionTificationswithDeviceToken:(NSData*)devToken{
//获取用于远程注册的捆绑包信息(如果您有多个应用程序,则很方便)
NSString*appVersion=[[NSBundle mainBundle]infoDictionary]objectForKey:@“CbundLeverVersion”];
//准备要注册的设备令牌(删除空格和<>)
NSString*deviceToken=[[[devToken描述]
StringByReplacingOfString:@“with String:@”]
StringByReplacingOfString:@“和字符串:@]”发生;
NSMutableString*urlString=[[BASE_URL mutableCopy]autorelease];
[urlString appendFormat:@“traceDeviceTokenId?令牌ID=%@&version=%@”,deviceToken,appVersion];
NSURL*url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*postLength=[NSString stringWithFormat:@“%d”,[urlString长度]];
NSMutableURLRequest*请求=[[[NSMutableURLRequest alloc]init]autorelease];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[RequestSetValue:@“text/xml;charset=utf-16”用于HttpHeaderField:@“内容类型”];
[请求setHTTPBody:[urlString dataUsingEncoding:NSUTF16StringEncoding];
NSLog(@“请求xml>>>>%@”,urlString);
n错误*错误;
NSURLResponse*响应;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSString*responseXml=[[NSString alloc]initWithData:urlData编码:NSUTF16StringEncoding];
NSLog(@“响应xml>>>>=%@”,响应xml);
}
-(无效)应用程序:(UIApplication*)应用程序DIDGregisterForRemotionTificationswithDeviceToken:(NSData*)设备Token
{ 
NSString*STRWITHOUTPACES=[NSString stringWithFormat:@“%@”,deviceToken];
strWithoutSpaces=[strWithoutSpaces StringByReplacingOfString:@“with string:@”的发生率];
strWithoutSpaces=[strWithoutSpaces StringByReplacingOfString:@“with string:@”的发生率];
[self-createRequestWithDeviceToken:strDeviceToken];
}
-(void)createRequestWithDeviceToken:(NSString*)deviceToken
{    
[[UIApplication sharedApplication]设置网络活动指示器可见:是];
NSString*strURL=[[NSString alloc]initWithFormat:@“%@@”,APNS_设备注册_URL,设备登录];
[self-prepareConnectionForWebService:strURL delegateHandler:self];
[strURL释放];
}
-(void)prepareConnectionForWebService:(NSString*)WebService URL delegateHandler:(id)对象
{
//创建HTTP请求
objRequest=[[RequestCreation alloc]init];
[self.objRequest initRequestWithURL:webserviceurlwithmethodtype:@“GET”withContentType:@“text/html”和bodydata:nil];
//将URLConnection与请求一起放置。
objConnection=[[[ConnectionCreation alloc]init]自动释放];
self.objConnection.delegate=对象;
[self.objConnection initWithRequest:self.objRequest.objURLRequest];
[objRequest发布];
}
-(void)initWithRequest:(NSMutableURLRequest*)requestedURL
{
self.urlConnection=[NSURLConnection connectionWithRequest:requestedURL委托:self];
if(self.urlConnection)
{
receivedData=[[NSMutableData alloc]init];
}
其他的
{
//i通知用户连接失败
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“连接失败”消息:@“无法连接”委托:nil cancelButtonTitle:@“确定”其他ButtonTitles:nil];
[警报显示];
[警报发布];
}
}
我刚刚粘贴了当前为我的应用程序运行的代码。只需检查代码以了解哪里出了问题。使用小方法断开用DidRegisterForRemotionTificationsSwithDeviceToken编写的代码以正确理解。 希望这有帮助

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{ 

    NSString *strWithoutSpaces  = [NSString stringWithFormat:@"%@",deviceToken];
    strWithoutSpaces = [strWithoutSpaces stringByReplacingOccurrencesOfString:@" " withString:@""];    
    strWithoutSpaces = [strWithoutSpaces stringByReplacingOccurrencesOfString:@"<" withString:@""];
    strDeviceToken = [strWithoutSpaces stringByReplacingOccurrencesOfString:@">" withString:@""];

    [self createRequestWithDeviceToken:strDeviceToken];     
}

- (void) createRequestWithDeviceToken:(NSString*)deviceToken
{    
        [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
        NSString *strURL = [[NSString alloc] initWithFormat:@"%@%@",APNS_DEVICE_REGISTRATION_URL, deviceToken];
        [self prepareConnectionForWebService:strURL delegateHandler:self];
        [strURL release];

}

- (void) prepareConnectionForWebService:(NSString*)webServiceURL delegateHandler:(id)object
{
    //Create HTTP request
    objRequest = [[RequestCreation alloc] init];
    [self.objRequest initRequestWithURL:webServiceURL withMethodType:@"GET" withContentType:@"text/html" andBodyData:nil];

    //Placing URLConnection with request.
    objConnection = [[[ConnectionCreation alloc]init] autorelease];
    self.objConnection.delegate = object;
    [self.objConnection initWithRequest:self.objRequest.objURLRequest];
    [objRequest release];
}

- (void) initWithRequest:(NSMutableURLRequest *)requestedURL
{
    self.urlConnection = [NSURLConnection connectionWithRequest:requestedURL delegate:self];
    if(self.urlConnection)
    {
        receivedData=[[NSMutableData alloc] init];
    }
    else 
    {
        //infiorm the user that the connection is failed
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Connection Failure" message:@"Unable to connect" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
}