gpt4 book ai didi

iphone - 更新报亭图标

转载 作者:行者123 更新时间:2023-11-29 04:43:40 25 4
gpt4 key购买 nike

我通过教程书籍 [报摊章节] 关注 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...");

}

示例代码太困惑了!有很多代码和自定义类或委托(delegate),我将不胜感激帮助我解决这个问题

谢谢

编辑:

#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 在您创建连接时被调用。示例:

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

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

将这4个放入 View Controller

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

}

关于iphone - 更新报亭图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9980268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com