2

I am developing an app which should support iOS5,iOS6,iOS7.

In my app i want to encode my NSString Data using base64encoding.

Following is the code I am using for encoding

NSString *userData = @"data";
NSString *base64EncodedString = [[userData dataUsingEncoding:NSUTF8StringEncoding] 
base64EncodedStringWithOptions:0];

The above code is running fine in iOS7, but in iOS6 the application crashes,with the following error:

[NSConcreteMutableData base64EncodedStringWithOptions:]

Is there any other way to encode data Or any function which support iOS6 & iOS7.

Can anyone help me out with this?

Thanks in Advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nilesh .S. Joshi
  • 567
  • 2
  • 9
  • 26
  • 1
    What does the Xcode documentation say about the availability of base64EncodedStringWithOptions? – El Tomato Feb 05 '14 at 07:13
  • You need to find (or write) another base64 encoding algorithm since the one you are using is only for iOS 7 or later. – rmaddy Feb 05 '14 at 07:18
  • See initWithBase64Encoding . – El Tomato Feb 05 '14 at 07:21
  • Oh, okay... Sorry about that. The Xcode documentation says 'Available in OS X v10.6 and later,' though. – El Tomato Feb 05 '14 at 07:25
  • @ElTomato Are you looking at the Mac documentation? – David Rönnqvist Feb 05 '14 at 07:26
  • It's Xcode. It says "Although this method was only introduced publicly for iOS 7, it has existed since iOS 4; you can use it if your application needs to target an operating system prior to iOS 7. This method behaves like initWithBase64EncodedString:options:, but ignores all unknown characters." I never use base64Encoding. So I'm sure you guys know more about this stuff. See under Deprecated in OS X v10.9. – El Tomato Feb 05 '14 at 07:28
  • possible duplicate of [-\[NSConcreteMutableData base64EncodedStringWithOptions:\]: unrecognized selector sent to instance 0x776e920'](http://stackoverflow.com/questions/20254982/nsconcretemutabledata-base64encodedstringwithoptions-unrecognized-selector) – Martin R Feb 05 '14 at 07:40

1 Answers1

1

Can't comment for some reason, so I'll link to an old StackOverflow answer that provides a plausible solution. Basically, you convert the NSString to NSData and encode the NSData.

+ (NSString *)toBase64String:(NSString *)string {
    NSData *data = [string dataUsingEncoding: NSUnicodeStringEncoding];
    NSString *ret = [NSStringUtil base64StringFromData:data length:[data length]];
    return ret;
}

Of course, you could check for iOS7 and use initWithBase64EncodedString:options: for that case.

Community
  • 1
  • 1
user1349663
  • 595
  • 1
  • 7
  • 21