更新报亭图标

时间:2012-04-02 16:37:50

标签: iphone objective-c ios xcode

我按照教程书[报亭章节]关注iOS 5,但我在更新图标方面遇到了问题。

据我所知,报亭框架有一个从URL下载内容并将其保存到应用程序目录的功能,例如,天气应用程序终止或不能使用此方法,我是对的吗?

1-应用程序应该从我的网站下载一个图标,而应用程序在后台 2-下载图标文件后,应用程序应使用当前图标替换我的新图标,该图标与推送通知一起

这是我的代码,但没有任何反应!!我应该把这行代码放在哪里?在我的appDelegate或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...");

}

示例代码太混乱了!有很多代码和自定义类或代表,我将不胜感激帮助我解决这个问题

谢谢

已编辑:

#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:]:无法识别的选择器发送到实例0x85acfd0       (LLDB)

1 个答案:

答案 0 :(得分:1)

在创建连接时调用connectionDidfinishDownloading。例如:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(conn){
        webData = [NSMutableData data];
        [Loading startAnimating];

        NSLog(@"%@",webData);
    }

将这4个放在视图控制器中

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response 
    {
        [webData setLength: 0];
    }


// This method is responsible for storing the newly received data. The new data is appended to the NSMutableData object created in the button method, or the method that calls NSRUL methods.
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{
    [webData appendData:data];
}




//If an error is encountered during the download, the delegate receives a connection:didFailWithError: message. The NSError object passed as the parameter specifies the details of the error. It also provides the URL of the request that failed in the user info dictionary using the key NSURLErrorFailingURLStringErrorKey.
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{
    // inform the user if connection fails//
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);



}




   //Finally, if the connection succeeds in downloading the request, the delegate receives the connectionDidFinishLoading: message
    - (void)connectionDidFinishDownloading:(NSURLConnection*)connection destinationURL:(NSURL*)destinationURL {

//[webView loadRequest:request];//if you wanted load the website into the webview

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...");

}