0

我正在开发一个 iOS 应用程序,我在其中实现推送通知。

在函数中,AppDelegate更具体地说didRegisterForRemoteNotificationsWithDeviceToken,在我注册推送通知后,我发出一个 http post 请求,将用户凭据(cfuuid、os 等)和推送令牌发送到我的服务器。

当服务器是应用程序时,一切都很顺利。但是,如果由于某种原因无法访问服务器,则应用程序的 UI 会挂起大约 30 秒(直到连接超时),而我唯一看到的是白屏。

我如何将“网络”与 UI 分开?我想答案是使用另一个线程。

我怎么能这样做?我在里面做的唯一一件事didRegisterForRemoteNotificationsWithDeviceToken就是使用 ASIHTTPRequest 库将凭据发送到服务器。

需要在不同线程中执行的代码如下所示:

NSString *jsonString;
    //jsonString = [[NSString alloc] initWithFormat:@"{\"deviceUUID\":\"%@\",\"os\":\"ios\", \"active\":\"%d\", \"pushToken\":\"%@\"}",deviceUUID,active,token];
    jsonString = [[NSString alloc] initWithFormat:@"{\"deviceUUID\":\"%@\",\"os\":\"ios\", \"pushToken\":\"%@\"}",deviceUUID,token];
    NSLog(@"%@",jsonString);

    //NSString *urlStr= [[NSString alloc] initWithFormat:CITYINFO_SERVER_URL,@"push_notifications/register"];
    NSString *urlStr= [[NSString alloc] initWithFormat:CITYINFO_SERVER_URL,@"register.php"];

    //send json file , using ASIHttpClass
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.timeOutSeconds = TIME_OUT_SECONDS;
    [request setRequestMethod:@"PUT"];

    //NSString *credentials= [self encodeCredentials];
    //[request addRequestHeader:@"Authorization" value:[[NSString alloc] initWithFormat:@"Basic %@",credentials]];
    [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];


    [request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    [request startSynchronous];

    if([request responseStatusCode]==200){
        NSLog(@"Server reached. Response Status : 200");
        return true;
    } else {
        NSLog(@"Server could not be reached");
        return false;
4

4 回答 4

1
[request startSynchronous];

绝不。做。同步。联网。在主线程上。


将它移动到另一个线程,或者更好的是,使请求异步。

于 2013-05-10T09:16:14.527 回答
0

利用[request startAsynchronous];

代替[request startSynchronous];

于 2013-05-10T09:16:20.023 回答
0

而不是发送同步请求,只需发送异步请求

[request startAsynchronous]; 

并在委托方法的某个时间间隔后调用正在访问服务的方法。

于 2013-05-10T09:16:38.170 回答
0

我认为,您正在使用主线程将设备令牌发送到服务器,请使用辅助线程来实现此功能

试试这个代码: -

[self performSelectorInBackground:@selector(pushNotifictionJson) withObject:nil];



-(void) pushNotifictionJson
{
    NSString *jsonString;
    //jsonString = [[NSString alloc] initWithFormat:@"{\"deviceUUID\":\"%@\",\"os\":\"ios\", \"active\":\"%d\", \"pushToken\":\"%@\"}",deviceUUID,active,token];
    jsonString = [[NSString alloc] initWithFormat:@"{\"deviceUUID\":\"%@\",\"os\":\"ios\", \"pushToken\":\"%@\"}",deviceUUID,token];
    NSLog(@"%@",jsonString);

    //NSString *urlStr= [[NSString alloc] initWithFormat:CITYINFO_SERVER_URL,@"push_notifications/register"];
    NSString *urlStr= [[NSString alloc] initWithFormat:CITYINFO_SERVER_URL,@"register.php"];

    //send json file , using ASIHttpClass
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.timeOutSeconds = TIME_OUT_SECONDS;
    [request setRequestMethod:@"PUT"];

    //NSString *credentials= [self encodeCredentials];
    //[request addRequestHeader:@"Authorization" value:[[NSString alloc] initWithFormat:@"Basic %@",credentials]];
    [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];


    [request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    [request startSynchronous];

    if([request responseStatusCode]==200){
        NSLog(@"Server reached. Response Status : 200");
        return true;
    } else {
        NSLog(@"Server could not be reached");
        return false;
    }
}

和第二种方法

use [request startAsynchronous];

instead of [request startSynchronous];
于 2013-05-10T09:17:05.810 回答