2012-06-25 82 views
0

我試圖建立一個的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

任何幫助,將不勝感激。

回答

1

我總是在可變陣列保持這種數據結構創建一個新的數據結構,其中有一個-connection屬性和屬性 - 請求,這樣

@interface connectionWrapper : NSObject 
@property(retain) NSURLRequest *request 
@property(retain) NSURLConnection *connection 

,你能辨別回調的連接通過遍歷數組,每個connectionWrapper實例的-connection屬性與connection參數比較方法的回調方法,如果它們匹配(指向同一個對象),那麼你可以檢索connectionWrapper實例的-request屬性,那麼-url NSURLRequest實例的屬性。

因爲我不是一個母語是英語,我覺得代碼是一個更好的導師。

-(NSURLRequest*)getRequestByConnection:(NSURLConnection*)connection 
{ 
    for(connectionWrapper *w in theArrayContainingAllConnectionWrappers) 
    { 
     if(w == connection) 
      return w.request; 
    } 
} 

在回調方法:

-(void)connection:(NSURLConnection*)connection didReceiveResponse(NSURLResponse*)response 
{ 
    NSURLRequest *request = [self getRequestByConnection:connection]; 
    NSURL *url = [request url]; 
    /*apply different approach to different url/* 
} 

PS:這是非常可悲的,NSURLConnection的沒有-request屬性,以便我們可以檢索與輕鬆連接相關的請求。

+0

原諒我的無知,但什麼回調方法? – HurkNburkS

+0

那些名字以連接開頭的方法: – CarmeloS

+0

好涼..我需要更多地瞭解它們,然後像​​你說的只是在回調中區分它們。有沒有一種方法可以說是在請求的一半進行請求回去並根據你解析的數據自動獲得一個新文件? – HurkNburkS

0

你知道.css文件的名稱?

如果是這樣我只想讓2個請求,否則你將不得不寫一個解析器來查找該鏈接的CSS和進行第二次請求,反正。

我也建議尋找到一個庫來處理的東西downlading - 可與先進的功能做繁重你大圖書館的很多的。

這裏的3我用:

http://blog.mugunthkumar.com/coding/ios-tutorial-advanced-networking-with-mknetworkkit/

https://github.com/tonymillion/TMHTTPRequest

https://github.com/pokeb/asi-http-request

+0

我之前使用asihttprequest,但它已經停止,所以不是我已經轉移到NSURLRequest ..我發現沒關係,只是意味着一些額外的工作,我已經做了很多我自己的解析器方法..但是我只是無法確定是否有一些時髦的快捷方式來獲取CSS文件等..通常我只是調用PHP腳本,從SQL抓取數據..這個HTML/CSS/JavaScript的東西,我還沒有嘗試過,所以如果我不得不採取不同的方式。 – HurkNburkS

+0

感謝您的回覆。 – HurkNburkS

1

這種或那種方式,你將不得不作出2個請求。即使您直接在網絡瀏覽器中打開網頁,瀏覽器也會爲其下載的HTML中引用的CSS文件提出單獨請求。如果您的應用程序需要HTML和CSS文件,那麼您希望它創建2個獨立的URL請求,首先獲取HTML,然後獲取CSS文件。

現在,僅僅因爲需要創建2個請求,並不意味着您總是需要編寫這些2個請求的代碼。可能像@Slee推薦的類庫會自動獲取第一個請求的結果,解析出來,並對任何引用的CSS文件發出請求。我沒有和他們合作,所以我不確定他們支持什麼,或者是否有任何圖書館會爲你做這件事。

您可能需要考慮的一件事是通過UIWebView加載HTML和CSS,而不是手動處理。 UIWebView將嘗試加載,解析和呈現HTML文件到UI組件中。在這個過程中,它將加載引用的CSS和JavaScript文件並將其應用於其渲染。如果你想做特別的事情,比如攔截加載CSS文件的調用,你可以實現UIWebViewDelegate協議並設置UIWebView的代理。在該代理中,您可以實現-webView:shouldStartLoadWithRequest:navigationType:方法,以便在Web視圖加載CSS文件時收到通知。您可以使用該方法的調用來查看爲CSS發出的請求,並執行其他有趣的請求。

+0

好的真棒!非常感謝,因爲它涉及到我正面臨的當前問題,所以它具有很大的意義:)現在,我只需要閱讀並弄清楚如何嘗試這個......但是,謝謝你這有時是戰鬥中的一半,需要做,而不是做,現在只需檢查ios庫中的一些函數,並查看一些示例代碼。歡呼花時間提供深度響應。 – HurkNburkS

+0

我的意思是我打算只下載數據放入一個字符串,然後將其添加到UIWebView ..但現在你可以直接通過UIWebView加載它..我將考慮這樣做! – HurkNburkS