I follow iOS 5 by tutorials book [Newsstand Chapter] , but I have a problem with updating icon .
As I know newsstand framework has a feature to download contents from an URL and save it to the application directory for example, weather application is terminated or not this method should work , am I right ?
1- Application should download just an icon from my site, while the app is in the background
2- After download the icon file , application should replace my new icon with current icon , which is along with push notification
here is my code but nothing happens !! where should I put this line of code ? in my appDelegate or AppViewController ?
- (void)connectionDidFinishDownloading:(NSURLConnection*)connection destinationURL:(NSURL*)destinationURL {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[self saveFile:@"NSIcon" ofType:@"png" fromURL:@"http://website.com/NSIcon.png" inDirectory:documentsDirectory];
UIImage * newsstandImage = [self loadImage:@"NSIcon" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
UIApplication *app = [UIApplication sharedApplication];
[app setNewsstandIconImage:newsstandImage];
NSLog(@"downloading...");
}
the sample code is too confusing ! with lots of codes and custom classes or delegates , I would be grateful help me to solve this issue
Thank you
EDITED :
#pragma mark ViewDidLoad
- (void)viewDidLoad
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://website.com/NSIcon.png"]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn){
webData = [NSMutableData data];
UIApplication *indicator = [UIApplication sharedApplication];
indicator.networkActivityIndicatorVisible = YES;
NSLog(@"%@",webData);
}
}
#pragma mark NewsStand Methods
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response
{
[webData setLength:0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
NSLog(@"Download Finish");
UIApplication *indicator = [UIApplication sharedApplication];
indicator.networkActivityIndicatorVisible = NO;
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
// inform the user if connection fails//
NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishDownloading:(NSURLConnection*)connection destinationURL:(NSURL*)destinationURL {
UIApplication *indicator = [UIApplication sharedApplication];
indicator.networkActivityIndicatorVisible = NO;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[self saveFile:@"NSIcon" ofType:@"png" fromURL:@"http://website.com/NSIcon.png" inDirectory:documentsDirectory];
UIImage * newsstandImage = [self loadImage:@"NSIcon" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
UIApplication *app = [UIApplication sharedApplication];
[app setNewsstandIconImage:newsstandImage];
NSLog(@"downloading...");
}
2012-04-03 23:35:11.297 iMag[6757:15803] -[__NSCFDictionary setLength:]: unrecognized selector sent to instance 0x85acfd0
(lldb)
connectionDidfinishDownloading is called when you when you create a connection. Example:
Put these 4 in view controller