2014-03-30 28 views
0

我正在编写一个iOS应用程序,它使用URL请求发送和接收数据的PHP文档。这是我的代码。锁定时的Objective-C URL请求iPhone

NSString *myRequestString = [NSString stringWithFormat:@"userid=%@&recieverid=%@&messege=%@&type=%@&password=%@", ownID, _friendID, content_encoded, msg_type, password]; 
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"something.php"]]; 
    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    [request setHTTPBody: myRequestData]; 

    //My activiy Indicator starts here 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Now send a request and get Response 
     NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 
     NSString *result = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; 

     //Here some internal coding happens... 

     dispatch_async(dispatch_get_main_queue(), ^(void) { 

      //Stopping activity indicator 

     }); 
    }); 

但是,如果用户锁定了他的电话,而他(在其他应用程序,如Snapchat等,以及可能的)发送数据的应用程序冻结当用户返回并已重新打开它。 我想知道如果应用程序连接到服务器,并且用户关闭不会导致此错误发生的应用程序,这是否是更好的方式。

谢谢:) 安东 对不起我可怜的英语我不是母语的人。

+1

这有什么好做的Xcode。 – mah

+0

为什么不能?在objective-c中没有东西需要编码吗? – user3207681

+2

Xcode是一个IDE,它与程序中的错误无关,与您的程序与某些远程服务器的交互无关。把它叫做Xcode就好像叫它与Mac有关(因为你大概是用Mac开发的),或者称之为地球相关的,因为在开发的时候,你在地球上。 – mah

回答

6

我建议:

  1. 指定后台任务标识,由约翰·伍兹的建议,因此,如果用户离开应用程序的请求的中间,它会尝试在后台继续网络请求。使用sendAsynchronousRequest而不是将sendSynchronousRequest调度到后台。

  2. 确保你正确地检测和处理错误(因为它不是完全清楚问题是否问题在于你的问题的代码或随后处理的任何处理)。

  3. 无关,但我会避免使用bytes相关的NSData方法。

这样:

// I'm guessing that you're percent encoding `messege` [sic], but I'd do it for all of those parameters (notably password) 

NSString *myRequestString = [NSString stringWithFormat:@"userid=%@&recieverid=%@&messege=%@&type=%@&password=%@", ownID, _friendID, content_encoded, msg_type, password]; 

// Use `dataUsingEncoding` rather than bytes rendition: 
// 
//  NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 

NSData *myRequestData = [myRequestString dataUsingEncoding:NSUTF8StringEncoding]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"something.php"]]; 
[request setHTTPMethod: @"POST"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
[request setHTTPBody: myRequestData]; 

// start background task 

UIBackgroundTaskIdentifier __block task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
    [[UIApplication sharedApplication] endBackgroundTask:task]; 
    task = UIBackgroundTaskInvalid; 
}]; 

// activity indicator starts here 

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    if (!data) { 
     NSLog(@"%s: sendAsynchronousRequest error: %@", __PRETTY_FUNCTION__, connectionError); 
    } else { 

     // use NSData rendition rather than bytes rendition: 
     // 
     //  NSString *result = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; 

     NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

     // here some internal coding happens... 
    } 

    // stop activity indicator here 

    // stop background task 

    if (task != UIBackgroundTaskInvalid) { 
     [[UIApplication sharedApplication] endBackgroundTask:task]; 
     task = UIBackgroundTaskInvalid; 
    } 
}]; 
+0

谢谢:)这很好! – user3207681

+0

谢谢你完美的作品! – user3207681