ASIHTTPRequest project, tam olarak ne istediğinizi yapmak üzere tasarlanmış ASIWebPageRequest
adlı bir sınıf var. Projenize ek bir bağımlılık eklemenin bir sakıncası yoksa, bunun sizin için iyi bir çözüm olduğunu düşünüyorum: ASIWebPageRequest. Ben sevdim sayfada
yukarıdaki nasıl kullanılacağını bazı iyi örnekler vardır ama tamamlamak için burada bunlardan biri ekleriz:
- (IBAction)loadURL:(NSURL *)url
{
// Assume request is a property of our controller
// First, we'll cancel any in-progress page load
[[self request] setDelegate:nil];
[[self request] cancel];
[self setRequest:[ASIWebPageRequest requestWithURL:url]];
[[self request] setDelegate:self];
[[self request] setDidFailSelector:@selector(webPageFetchFailed:)];
[[self request] setDidFinishSelector:@selector(webPageFetchSucceeded:)];
// Tell the request to embed external resources directly in the page
[[self request] setUrlReplacementMode:ASIReplaceExternalResourcesWithData];
// It is strongly recommended you use a download cache with ASIWebPageRequest
// When using a cache, external resources are automatically stored in the cache
// and can be pulled from the cache on subsequent page loads
[[self request] setDownloadCache:[ASIDownloadCache sharedCache]];
// Ask the download cache for a place to store the cached data
// This is the most efficient way for an ASIWebPageRequest to store a web page
[[self request] setDownloadDestinationPath:
[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]];
[[self request] startAsynchronous];
}
- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest
{
// Obviously you should handle the error properly...
NSLog(@"%@",[theRequest error]);
}
- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest
{
NSString *response = [NSString stringWithContentsOfFile:
[theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
// Note we're setting the baseURL to the url of the page we downloaded. This is important!
[webView loadHTMLString:response baseURL:[request url]];
}
Çok teşekkürler bu örnek için, daha sonra çalışacağız. ASIReplaceExternalResourcesWithData'nın verileri sayfaya nasıl ekleyebileceğini söyleyebilir misiniz? Sayfa önbellekte bir html dosyası olarak kalır, değil mi? İstek orijinal html'yi değiştiriyor mu? –
Evet, haklısın. ASI sayfayı değiştirir ve bu varlıkları [Veri URI'leri] ile değiştirir (http://en.wikipedia.org/wiki/Data_URI_scheme). Alternatif olarak, kaynakları ASIMeplaceExternalResourcesWithLocalURLs' adlı farklı bir URL değiştirme modunu kullanabilir, bu da kaynakları dosya olarak indirecek, ancak HTML'yi bu yerel dosyalarda işaret edecek şekilde değiştirmeye devam edebilirsiniz. – xoebus
Teşekkürler xoebus. Ayrıca çevrimdışı taramayı etkinleştirmek için şu kod satırını eklemem gerektiğini de öğrendim: 'request.cachePolicy = ASIAskServerIfModifiedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy;' –