NSURLConnection not downloading all data - nsurlconnection

How comes when i run this code, the data that gets downloaded seems to have cropped off some information from the first row?
NSURL *url = [NSURL URLWithString:#"http://ichart.finance.yahoo.com/table.csv? s=GOOG&d=6&e=27&f=2012&g=d&a=6&b=25&c=2011&ignore=.csv"];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
The data then gets sorted here:
NSString *strData = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
strData = [strData stringByReplacingOccurrencesOfString:#"Date,Open,High,Low,Close,Volume,Adj Close" withString:#""];
NSArray* arrUncleanDataLine = [strData componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSMutableArray *arrCleanDataLine = [[NSMutableArray alloc]init];
NSString *temp = [[NSString alloc]init];
The top line gets cropped off. Why is this? Is it because NSData can only hold a certain amount of data?

Because you yourself remove the first line "Date,Open,High,Low,Close,Volume,Adj Close" by this code:
strData = [strData stringByReplacingOccurrencesOfString:#"Date,Open,High,Low,Close,Volume,Adj Close" withString:#""];
So just comment this line.

I have fixed it. When i was downloading the data it was not appending the old data and adding it to the new data.

Related

App terminates when using NSURL with parameters

In my app I sent a request to my PHP file to download some info. Whenever I use the code below it will terminate:
-(void)downloadItems
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:#"commentsId"];
NSLog(#"The comments id is : %#",myString);
// Download the json file
NSString *urlstring = [NSString stringWithFormat:#"http:/MyWebSite.com/checkphp.php?title=%#", myString];
NSURL *jsonFileUrl = [NSURL URLWithString:urlstring];
NSLog(#"The qasida id is: %#", jsonFileUrl);
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
But if I remove myString from urlString like this:
NSURL *jsonFileUrl = [NSURL URLWithString:#"http:/myWebSite/checkphp.php?title=%#"];
the app will not terminate and it will retrieve data.
Can any one tell me what is wrong with my code?
Thanks

attachment not sent with email from ipad

I have an iPad that has a routine to create a pdf and send as an attachment to an email. It all seems to work with the email composer opening showing the pdf document attached. However when tested on an iPad, when the email is received there is no attachment. Any ideas?
[mailComposer addAttachmentData:data mimeType:#"application/pdf" fileName:#"pdffile.pdf"];
[self presentViewController:mailComposer animated:YES completion:nil];
Many thanks
Detail:
The pdf file is created and called pdffile.pdf. The following is the full email routine:
MFMailComposeViewController *mailComposer;
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setSubject:[NSString stringWithFormat: #"i-observe Lesson Observation for: %s", "date"]];
[mailComposer setMessageBody:[NSString stringWithFormat: #"i-observe Lesson Observation for: %s", "name"] isHTML:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:#"pdffile.pdf"];
NSMutableData *data=[NSMutableData dataWithContentsOfFile:file];
[mailComposer addAttachmentData:data mimeType:#"application/pdf" fileName:#"pdffile.pdf"];
[self presentViewController:mailComposer animated:YES completion:nil];
Try This Method:
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setSubject:#"Email with attached pdf"];
NSString *newFilePath = #"get path where the pdf reside";
NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
[mail addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"yourpdfname.pdf"];
NSString * body = #"";
[mail setMessageBody:body isHTML:NO];
[self presentModalViewController:mail animated:YES];
[mail release];
}
else
{
NSLog(#"Message cannot be sent");
}
The next solutions is based assuming that the pdf file is in your main bundle:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource: #"RealNameofFile" ofType: #"pdf"];
NSData *pdfD = [NSData dataWithContentsOfFile:myFile];
[mailViewController addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"Nametodisplayattached.pdf"];
make sure that the file has the same name as the table cell

Merging RTF Files With Cocoa

How would one do this? I know just merging NSData doesn't work.
Playing with NSAttributedString should work, something like the code shown below
This is a very quick and dirty way to merge many RTF files together
- (void)mergeRTF:(NSURL*)rtf1 :(NSURL*)rtf2 :(NSURL*)merged {
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithUnsignedInteger:NSUTF8StringEncoding]
forKey:NSCharacterEncodingDocumentOption];
NSDictionary *docAttrs = nil;
NSError* error = nil;
NSAttributedString *rtfText1 = [[NSAttributedString alloc] initWithURL:rtf1
options:options
documentAttributes:&docAttrs
error:&error];
NSAttributedString *rtfText2 = [[NSAttributedString alloc] initWithURL:rtf2
options:options
documentAttributes:&docAttrs
error:&error];
NSMutableAttributedString* whole = [[NSMutableAttributedString alloc] initWithAttributedString:rtfText1];
[whole appendAttributedString:rtfText2];
NSData* data = [whole RTFFromRange:NSMakeRange(0, whole.length) documentAttributes:nil];
[data writeToURL:merged atomically:YES];
}

Why does my code break for one call to this method, but not another?

I have a Mac application that is supposed to fetch Twitter followers and friends for a given username and then, in turn, ask Twitter for the user NAMES for each of those returned UserIDs. As you can see below, I have a method that will call the Twitter API to get friends/followers (which works fine). It is the userNameForUserID:delegate: method that is causing me problems. I used to do these requests synchronously and return the NSString for the username right then. That (now commented out) line always broke, so I tried doing it with an NSURLConnection asynchronously. Still doesn't work. I don't understand why
[[NSString alloc] initWithContentsOfURL:...] works for the fetchFollowers... method, but not when I do it the EXACT SAME way in the other...
I put a break point on the line that used to alloc init the NSString with contents of URL, and when I step into it, it doesn't break, return, throw an exception, crash...nothing. It's as if that line just got stepped over (but my application is still blocked.
Any ideas? Much appreciated!
NSString * const GET_FOLLOWERS = #"https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=";
NSString * const GET_FRIENDS = #"https://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=";
NSString * const GET_USER_INFO = #"https://api.twitter.com/1/users/show.json?user_id=";
#implementation TwitterAPI
+ (void)userNameForUserID:(NSNumber *)userID delegate:(id<UserNameDelegate>)delegate
{
NSURL *url = [NSURL URLWithString:[GET_USER_INFO stringByAppendingString:[userID stringValue]]];
// NSString *JSON = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:req queue:queue completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) {
[delegate addUserNameToArray:data];
}];
}
+ (NSArray *)fetchFollowersWithUserName:(NSString *)userName
{
NSURL *url = [NSURL URLWithString:[GET_FOLLOWERS stringByAppendingString:userName]];
NSArray *followerIDs;
NSString *JSON = [[NSString alloc] initWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
if ([JSON rangeOfString:#"error"].location == NSNotFound)
followerIDs = [[JSON JSONValue] valueForKey:#"ids"];
return followerIDs;
}

Convert NSDictionary array values into NSString format for MFMailComposeViewController

I have a UIPickerViewDelegate that allows the user to select a term from the picker and see the term's definition by using NSDictionary to access data stored in a plist file. I also have a MFMailComposeViewController to allow the user to email the term and it's definition elsewhere.
But I can't seem to get the term and definition formatted properly for entry into the email. I looked at various "convert NSDictionary to NSString" solutions, but none seem to offer what I need, including variations of Term = [dict objectForKey:#"Term"].
Under viewDidLoad, I have the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Glossary" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.termArray = [dict objectForKey:#"Term"];
self.definitionArray = [dict objectForKey:#"Definition"];
My pickerView code includes this:
NSString *resultString = [[NSString alloc] initWithFormat:
#"%#",
[definitionArray objectAtIndex:row]];
definitionLabel.text = resultString;
NSString *termString = [[NSString alloc] initWithFormat:
#"%#",
[termArray objectAtIndex:row]];
The code for showEmail contains:
int definitionIndex = [self.termPicker selectedRowInComponent:0];
NSString *emailTitle = [NSString stringWithFormat:#"Definition of %#", termLabel];
NSString *emailDefinition = [definitionArray objectAtIndex:definitionIndex];
NSString *messageBody = [NSString stringWithFormat:#"The definition of <B>%#</B> is:<P> <B>%#</B>", termLabel, emailDefinition];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
I would like the email title and message body to pull from the Term selected in the picker and the Definition associated with it. I'm sure it must be a simple formatting issue, but I can't figure it out.

Resources