iphoneobjective-ciosurbanairship.comafnetworking

UrbanAirship/Push notifications registration: NSURLErrorDomain: bad url


I've got this really strange response to an MSMutableURLRequest:

Error Domain=NSURLErrorDomain Code=-1000 "bad URL"

The URL is composed thusly. Yes I know I'm putting my testing device's token online. I've changed a digit for privacy.

self.pushTokenData = deviceToken;

NSURL *correctURL = [NSURL URLWithString:@"https://go.urbanairship.com/api/device_tokens/2d1535dfcbfd859b4f55e6b74db24c71cf6082b503d38583b0b20f2816c24ca8"];

NSString *password = [@"redacted" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:correctURL];

NSLog(@"httpClient url: %@", [httpClient baseURL]);
[httpClient setAuthorizationHeaderWithUsername:@"redacted" password:password];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"PUT" path:nil parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
    NSLog(@"Success: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
    NSLog(@"Failure: %@", error);
}];
[operation start];

This URL works fine in safari. Any ideas?


Solution

  • NSMutableDictionary *payload = [NSMutableDictionary dictionary];
    [payload setValue:[NSArray arrayWithObjects:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], [[UIDevice currentDevice] model], [[NSLocale currentLocale] localeIdentifier], nil] forKey:@"tags"];
    [payload setValue:[[NSTimeZone localTimeZone] name] forKey:@"tz"];
    
    NSString *deviceTokenString = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    AFHTTPClient *urbanAirshipClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/device_tokens/]];
    urbanAirshipClient.parameterEncoding = AFJSONParameterEncoding;
    [urbanAirshipClient setAuthorizationHeaderWithUsername:<# UA Application Key #> password:<# UA Application Secret #>];
    [urbanAirshipClient putPath:[NSString stringWithFormat:@"device_tokens/%@", deviceTokenString] parameters:payload success:<# Success Block #> failure:<# Failure Block #>];
    

    Try this.