我正在开发一个iOS应用程序,我正在实现推送通知。
在AppDelegate
中,更具体地说,在didRegisterForRemoteNotificationsWithDeviceToken
函数中,在我注册推送通知后,我发出一个http post请求,向用户的服务器发送用户凭据(cfuuid,os等。 。)和推送令牌。
当服务器是应用程序时一切顺利。 然而,如果由于任何原因服务器无法访问,应用程序的UI会挂起大约30秒(直到连接timeOut),我唯一看到的是白屏。
我如何将“网络”与用户界面分开?我想答案是使用另一个线程。
我怎么能这样做?我在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;
答案 0 :(得分:1)
[request startSynchronous];
的从不。做。同步。联网。在主线程上。
将其移至另一个线程,甚至更好,使请求异步。
答案 1 :(得分:0)
使用[request startAsynchronous];
而不是[request startSynchronous];
答案 2 :(得分:0)
而不是发送同步请求,只需发送异步请求
[request startAsynchronous];
并且在委托方法的一段时间间隔之后调用正在服务的方法。
答案 3 :(得分: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];