我正在编写一个iOS应用程序,该应用程序使用对PHP文档的URL请求来发送和接收数据。这是我的代码。

    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等其他应用程序中使用),则该应用程序会在用户返回时冻结并必须重新打开它。
我想知道如果应用程序连接到服务器并且用户关闭了不会导致此错误的应用程序,那么是否还有更好的方法。

谢谢 :)
安东
很抱歉我的英语不好,我不是母语人士。

最佳答案

我建议:


根据John Woods的建议,指定一个后台任务标识符,以便如果用户将应用程序放在请求中间,它将尝试在后台继续网络请求。
使用sendAsynchronousRequest而不是将sendSynchronousRequest调度到后台。
确保正确检测和处理错误(因为我不清楚问题是否出在您的问题代码中,还是稍后对它进行任何处理)。
无关,但我避免使用与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;
    }
}];

关于ios - 锁定iPhone时的Objective-C URL请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22745133/

10-14 20:32
查看更多