多くの人が示唆したように、私の計画は、Web からコンテンツをダウンロードし、ライブラリ フォルダーに保存して、そこからコンテンツを読み込むことだと考えました。アプリ内には、ユーザーが選択するたびにサーバーに接続してコンテンツを更新する更新ボタンがあります。apple.com でサンプル コードを見つけました ( https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html )
少し微調整すれば問題なく動作するはずです。これは、複数のファイルをダウンロードしようとする場合にも役立つ可能性があります。iPhone アプリで複数のファイルをダウンロードする (目的 c)
- (IBAction)down
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://abc.com/missing.html"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSString *msg;
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
//You can also use the connection:didReceiveData: method to provide an indication of the connection’s progress to the user.
// receivedData is an instance variable declared elsewhere.
NSString *msg = [NSString stringWithFormat:@"data %@",data,nil ];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"-download-"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
//NSLog ([[NSString alloc] initWithFormat:@"The value: %d object: %@\n", alphArray.count, [alphArray objectAtIndex:1.1],nil]);
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"the data %@",string);
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [libraryPath stringByAppendingPathComponent:@"Missing.html"];
NSLog(@"Full path: %@", filePath);
//NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//write data to files
[data writeToFile:filePath atomically:YES];
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
// release the connection, and the data object
[connection release];
[receivedData release];
}
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSCachedURLResponse *newCachedResponse = cachedResponse;
if ([[[[cachedResponse response] URL] scheme] isEqual:@"https"]) {
newCachedResponse = nil;
} else {
NSDictionary *newUserInfo;
newUserInfo = [NSDictionary dictionaryWithObject:[NSDate date]
forKey:@"Cached Date"];
newCachedResponse = [[[NSCachedURLResponse alloc]
initWithResponse:[cachedResponse response]
data:[cachedResponse data]
userInfo:newUserInfo
storagePolicy:[cachedResponse storagePolicy]]
autorelease];
NSLog(@"Succeeded! Receive use:%@",newUserInfo);
}
NSLog(@"Succeeded! Received %@ - use:%@",newCachedResponse);
return newCachedResponse;
}