NSURLSessionDownloadTask中的错误 [英] Error in NSURLSessionDownloadTask

查看:521
本文介绍了NSURLSessionDownloadTask中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使应用处于后台状态,我仍试图更新用户在后台的位置,因此我触发了以下php脚本的位置更新:

I am trying to update the user's location on the backoffice even while the app is in backgound, so I trigger a location update calling the following php script:

-(void)locateUserAtLocation:(CLLocation*)location{
NSDictionary* dict=[self getCurrentAppAndUrlDictionary];
NSString* app=[dict objectForKey:@"app"];
float latitude=location.coordinate.latitude;
float longitude=location.coordinate.longitude;
NSString* language=[[NSLocale currentLocale] localeIdentifier];
NSString* nickName=[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
NSString* myUdid= [udid sharedUdid];
NSString *insertUrlString =[NSString stringWithFormat: @"http://www.miafoto.it/iPhone/inarrivo/taxi/insertUserNew.php?udid=%@&token=%@&nickname=%@&app=%@&language=%@&Latitude=%f&Longitude=%f&new=1",myUdid, token, nickName, app, language, latitude, longitude];
NSLog(@"url=%@", insertUrlString);

 NSURLSessionDownloadTask *insertTask = [[self backgroundSession] downloadTaskWithURL:    [NSURL URLWithString:insertUrlString]];
[insertTask resume];
}

但是我得到了错误: 后台下载的URL方案无效:(空).有效的方案是http或http 并且没有在前台或后台发送url.我在网络上搜索时,没有找到解决此问题的命中方法.我还将此问题提交给了Apple支持.

but I get error: Invalid URL scheme for background downloads: (null). Valid schemes are http or http and no url is sent either in foreground or background. I searched on the web I found no hits addressing this issue. I also submitted the issue to the Apple support.

推荐答案

您可能有一些保留的字符,这些字符会阻止NSURL对象正确地实例化,即URLWithString可能返回nil.

You might have some reserved characters that prevent the NSURL object from instantiating correctly, i.e. URLWithString is probably returning nil.

NSString *insertUrlString =[NSString stringWithFormat: @"http://www.miafoto.it/iPhone/inarrivo/taxi/insertUserNew.php?udid=%@&token=%@&nickname=%@&app=%@&language=%@&Latitude=%f&Longitude=%f&new=1",myUdid, token, nickName, app, language, latitude, longitude];

您可以通过检查NSURL来确认这一点:

You can confirm this by checking the NSURL:

NSURL *url = [NSURL URLWithString:insertUrlString];
NSAssert(url, @"problem instantiating NSURL: %@", insertUrlString);

这些字符串中是否有空格或其他保留字符?百分百摆脱这些值总是更安全的.就个人而言,我将参数添加到字典中,然后有一个可以对值进行百分比转义的函数,例如:

Do any of those strings have spaces or other reserved characters in them? It's always safer to percent escape these values. Personally, I add my parameters to a dictionary, and then have a function that will percent escape the values, e.g.:

NSDictionary *parameters = @{@"udid"      : myUdid,
                             @"token"     : token,
                             @"nickname"  : nickName,
                             @"app"       : app,
                             @"language"  : language,
                             @"Latitude"  : @(latitude),
                             @"Longitude" : @(longitude),
                             @"new"       : @"1"};

NSString *insertUrlString = [NSString stringWithFormat: @"http://www.miafoto.it/iPhone/inarrivo/taxi/insertUserNew.php?%@", [self encodeParameters:parameters]];

NSURL *url = [NSURL URLWithString:insertUrlString];
NSAssert(url, @"problem instantiating NSURL: %@", insertUrlString);

其中:

- (NSString *)encodeParameters:(NSDictionary *)parameters
{
    NSMutableArray *parameterArray = [NSMutableArray arrayWithCapacity:[parameters count]];

    for (NSString *key in parameters) {
        NSString *string;
        id value = parameters[key];

        if ([value isKindOfClass:[NSData class]]) {
            string = [[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding];
        } else if ([value isKindOfClass:[NSString class]]) {
            string = value;
        } else if ([value isKindOfClass:[NSNumber class]]) {
            string = [value stringValue];
        } else {                         // if you want to handle other data types, add that here
            string = [value description];
        }
        [parameterArray addObject:[NSString stringWithFormat:@"%@=%@", key, [self percentEscapeString:string]]];
    }

    return [parameterArray componentsJoinedByString:@"&"];
}

- (NSString *)percentEscapeString:(NSString *)string
{
    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                 (CFStringRef)string,
                                                                                 (CFStringRef)@" ",
                                                                                 (CFStringRef)@":/?@!$&'()*+,;=",
                                                                                 kCFStringEncodingUTF8));
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

这篇关于NSURLSessionDownloadTask中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆