How to Prevent Build Warning: " NSData may not respond to 'dataWithBase64EncodedString:' " - iphone

The following code produces this build warning:
NSData may not respond to
'dataWithBase64EncodedString:'
The code:
NSString * message = #"string string string";
NSData *data= [NSData dataWithBase64EncodedString:(NSString *)message];
How do I fix this to remove this warning?

Removing the warning is the least of your worries - NSData doesn't respond to that method and this code will crash if you run it!
See the docs here for the default available methods on NSData.
However, you're probably looking for this page which has an implementation of dataWithBase64EncodedString in a category (see the very last post on the thread!)

NSData does not have dataWithBase64EncodedString: method. If you use some custom NSData category with this method you should import header where it is defined.
Edit: So if you're using code from this link then you can just create your own .h and .m files and copy that code into them:
// MBBase64.h
#interface NSData (MBBase64)
+ (id)dataWithBase64EncodedString:(NSString *)string; // Padding '=' characters are optional. Whitespace is ignored.
- (NSString *)base64Encoding;
#end
//MBBase64.m
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#implementation NSData (MBBase64)
...
#end
And then import MBBase64.h header wherever you want to use +dataWithBase64EncodedString: method.

Just to clarify the previous answers:
In the NSFoundation API, NSData does not have a dataWithBase64EncodedString: method. If your copying code in which it does, then that code has extended NSData by adding to it a category that contains the method.
You can add arbitrary methods to any class using a category. If someone has used a category in their example code, you cannot use that code unless you also get the header and implementation files that define the category. If the original author did not make those available then you are out of luck.
Base64 encoding isn't one of the API provided encodings for strings so you'll probably have to implement that yourself or find some code by someone who has.

Related

Convert formatted HTML text string to NSString parts

I want to decode HTML string and store it in NSString.
following is the html string for one of the response from google direction api.
teststring is Turn <b>right</b> onto <b>Kennington Park Rd/A3</b><div style="font-size:0.9em">Continue to follow A3</div><div style="font-size:0.9em">Entering toll zone in 1.7 km at Newington Causeway/A3</div><div style="font-size:0.9em">Go through 2 roundabouts</div>
I want to store this html string in Array of 4 different NSStrings with following 4 NSStrings (removing all information for size colour)
Turn right onto Kennington Park Rd/A3
Continue to follow A3
Entering toll zone in 1.7 km at Newington Causeway/A3
Go through 2 roundabouts
I have used following method to convert html string to plain text.(html_response is the response from server and stringByConvertingHTMLToPlainText is the method defined in custom class file)
testString = (NSString*) [html_response stringByConvertingHTMLToPlainText];
NSLog(#"%#",testString);
but it converts whole string instead of breaking it in parts.
output at the console is
Turn right onto Kennington Park Rd/A3Continue to follow A3 Entering toll zone in 1.7 km at Newington Causeway/A3 Go through 2 roundabouts
So do I need to write custom method to do this? Any help will be greatly appreciated.
There's a very nice NSString category that you can use, it's a part of the project MWFeedParser. More specifically, you look for the file NSString+HTML.
The NSString+HTML category adds the following methods to the NSString Class,
- (NSString *)stringByStrippingTags;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
You could then try something like:
NSString *summary = [[[htmlString stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingHTMLEntities];
Hope it helps!, good luck :)

iPhone - OARequestHeader warnings

I am trying to upload a picture to TwitPic using GSTwitPicEngine.
I have included all the requirements but it seems that some functions used in OARequestHeader class are not recognized. Here is an example:
[chunks addObject:[NSString stringWithFormat:#"realm=\"%#\"", [realm encodedURLParameterString]]];
The "encodedURLParameterString" function is not recognized. realm variable is a NSString.
Do you have any idea what I have done wrong?
Thanks,
Andrei
YEP!
You are missing one file or missed to include it (it's part of OAuthConsumer).
Simply download
NSString+URLEncoding.h
and
NSString+URLEncoding.m
from http://oauth.googlecode.com/svn/code/obj-c1/OAuthConsumer/NSString+URLEncoding.h
and http://oauth.googlecode.com/svn/code/obj-c1/OAuthConsumer/NSString+URLEncoding.m
and add the #import line into the file, where the error appeared:
#import "NSString+URLEncoding.h"
This will add "encodedURLParameterString" to NSStrings selectors.

get element value

I have a NSString like that? (in iPhone application)
NSString *xmlStr = "<?xml version=1.0 encoding=UTF-8>
<information>
<name>John</name>
<id>435346534</id>
<phone>045635456</phone>
<address>New York</address>
</information>"
How I can get elements value?
(Do i need convert to XML format and get elements value? or split string? any way please tell me?)
Thank you guys.
If you want to use split string, you can use tokenization of strings using "componentsSeparatedByString" method. This is a more cumbersome method of course, rather than the recommended XMLParser
To get the name.
NSArray *xmlStr_first_array = [xmlStr componentsSeparatedByString: #"<name>"];
NSString *xmlStr_split = [NSString stringWithFormat:#"%#",[xmlStr_first_array objectAtIndex:1]];
NSArray *xmlStr_second_array = [xmlStr_split componentsSeparatedByString: #"</name>"];
NSString *name = [NSString stringWithFormat:#"%#",[xmlStr_second_array objectAtIndex:0]];
The most obvious solution is to use an XML parser to retrieve the values from each element.
You could use the excellent TBXML for this task. It provides a really simple interface where it wouldn't take more than a few lines to retrieve the desired values. The drawback to using this small library is that it (as far as I know) loads the entire XML data into memory. In this particular case, however, that is not problem at all.
There's of course also the option of using the NSXMLParser, though this is an event-driven parser, and thus a bit less simple to use.
Your string is in xml format already and you need to parse it to retrieve data. There're several options available - for example you can use NSXMLParser class or libxml library.
Edit: XMLPerformance sample project shows how to use both approaches and compare their performance.

Convert NSData [UIImage] to a NSString

I am aware this question has been asked several times, but I was unable to find a definate answer that would best fit my situation.
I want the ability to have the user select an image from the library, and then that image is converted to an NSData type. I then have a requirement to call a .NET C# webservice via a HTTP get so ideally I need the resulting string to be UTF8 encoded.
This is what I have so far:
NSData *dataObj = UIImageJPEGRepresentation(selectedImage, 1.0);
[picker dismissModalViewControllerAnimated:YES];
NSString *content = [[NSString alloc] initWithData:dataObj encoding:NSUTF8StringEncoding];
NSLog(#"%#", content);
The NSLog statement simply produces output as:
2009-11-29 14:13:33.937 TestUpload2[5735:207] (null)
Obviously this isnt what I hoped to achieve, so any help would be great.
Kind Regards
You can't create a UTF-8 encoded string out of just any arbitrary binary data - the data needs to actually be UTF-8 encoded, and the data for your JPEG image obviously is not. Your binary data doesn't represent a string, so you can't directly create a string from it - -[NSString initWithData:encoding:] fails appropriately in your case.
Assuming you're using NSURLConnection (although a similar statement should be true for other methods), you'll construct your NSMutableURLRequest and use -setHTTPBody: which you need to pass an NSData object to. I don't understand why you would be using a GET method here since it sounds like you're going to be uploading this image data to your web service - you should be using POST.

Parsing a .txt or html file into a string in the iPhone SDK

So, what I'm trying to do, is take a .txt or html file, being able to search through it, and grab a piece of text from file, place it into a string and finally adding it into a textView.
Each couple of piece of text will be divided like this:
001:001 Text1
001:002 Text2
001:003 Text3
002:001 Text1a
002:002 Text1b
... and so on
So essentially you would search the text for those numbers, and it would grab the text only. Is there a way to do that using objective C and using it on a iPhone app?
Use
NSString *pathToDefaultPlist = [[NSBundle mainBundle] pathForResource:#"TextFile" ofType:#"text"];
to load text file. Then:
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
this function to load the text file in String. Then use NSString function to divide the token :)
In addition to the [NSString stringWithContentsOfFile:encoding:error] to get the text of the file, your textView should have a setStringValue method.
so I'd do something like this:
NSString *pathToTextFile;
NSError *readError;
NSString *fileData = [NSString stringWithContentsOfFile:pathToTextFile
encoding:(appropriate encoding for your file)
error:*readError]
[textView setStringValue:fileData];
This might need a little massaging, I'm at work and don't have my Mac to verify the method signatures etc. But that's the general idea.