NSURLSessionDownloadTask出错

时间:2014-05-13 11:53:46

标签: xcode ios7.1 nsurlsessiondownloadtask

我正在尝试更新用户在后台的位置,即使应用程序处于后台,所以我触发了一个位置更新,调用以下php脚本:

-(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方案无效:( null)。有效方案是http或http 并且在前台或后台都没有发送网址。我在网上搜索,我发现没有解决这个问题的命中。我还将问题提交给Apple支持。

2 个答案:

答案 0 :(得分:1)

您可能有一些保留字符阻止NSURL对象正确实例化,即URLWithString可能正在返回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

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

这些字符串中是否有空格或其他保留字符?逃避这些价值的百分比总是更安全。就个人而言,我将我的参数添加到字典中,然后有一个将百分比转义为值的函数,例如:

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:@"+"];
}

答案 1 :(得分:0)

这是更容易的解决方案

 NSURL *url = [NSURL URLWithString: [[yourURLinString
 stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]
 stringByTrimmingCharactersInSet:[NSCharacterSet
 whitespaceAndNewlineCharacterSet]]];

以这种方式解析你的字符串url,它应该可以工作。