我試圖建立一個的NSURLRequest下載一個簡單的index.html其外耳炎的style.css片,但我不太清楚如何做到這一點..我永遠只能剛剛格式化的URL對我想要的文件的請求..但是這必須稍有不同,我無法找到我想要做的事情的一個好例子。的NSURLRequest與多個參數
這是我到目前爲止的代碼:
#pragma mark - NSURLConnection methods
- (void)htmlRequest
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/index.html"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData data];
} else {
// Inform the user that the connection failed.
NSLog(@"Connection Fail");
}
}
- (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.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// inform the developer of error type
}
// This method uses methodName to determin which Initalizer method to send the response data to in EngineResponses.m
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// EngineResponses *engineResponses = [EngineResponses sharedManager];
// [engineResponses GetManufacturers:receivedData];
NSString *myString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", myString);
}
,你可以看到我打電話的index.html直接..我想知道如何格式化我的要求,所以我得到index.html作爲還有的style.css
任何幫助,將不勝感激。
原諒我的無知,但什麼回調方法? – HurkNburkS
那些名字以連接開頭的方法: – CarmeloS
好涼..我需要更多地瞭解它們,然後像你說的只是在回調中區分它們。有沒有一種方法可以說是在請求的一半進行請求回去並根據你解析的數據自動獲得一個新文件? – HurkNburkS