Encoding strings for well formed URL's

Discussion in 'iOS Programming' started by nashyo, May 19, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    There appears to be some distrust in the iOS SDK, specifically the NSString class method that escapes strings.

    Using the following resource as guidance I have created a category class that is an extension of NSString.

    http://madebymany.com/blog/url-encoding-an-nsstring-on-ios

    .h
    Code:
    #import <Foundation/Foundation.h>
    
    @interface NSString (URLEncoding)
    -(NSString*)urlEncodingUsingEncoding:(NSStringEncoding)encoding;
    @end
    .m
    Code:
    @implementation NSString (URLEncoding)
    -(NSString*)urlEncodingUsingEncoding:(NSStringEncoding)encoding
    {
        return (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)self, NULL, (CFStringRef)@"!*'\"();;@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding));
    }
    @end
    When I import this class and attempt to create an instance, I cannot call the class method 'alloc'. Why hasn't this been inherited?

    Further, I have tried abandoning the use of this category class and simply calling some C functions as so:

    Code:
    NSString *escapedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (__bridge CFStringRef)grade,	 NULL,	 (CFStringRef)@"!’\"();:@&=+$,/?%#[]% ", kCFStringEncodingISOLatin1);
    However, I'm getting issues with this also. The NSLog that follows this piece of code has a warning from Xcode (slightly above it actually in the empty space between) 'data argument not used by format string':

    NSLog(@"Test", escapedString);

    Such a simple task, yet so many errors. What am I doing wrong here?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008

Share This Page