问题
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];
}
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.
回答1:
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];
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);
where:
- (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:@"+"];
}
回答2:
Here is easier solution
NSURL *url = [NSURL URLWithString: [[yourURLinString
stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]]];
Parse your string url this way and it should work.
来源:https://stackoverflow.com/questions/23630541/error-in-nsurlsessiondownloadtask