2010-06-24 103 views
1

我想顯示一個圖像從一個URL到我的UIWebview,但圖像的大小太小,無法通過拉伸它使模糊顯示..因爲我不能自動調整大小的圖像(因爲它變得模糊)我是想爲iPhone獲取和設置用戶代理值...但即時通訊無法找到有關它的相關信息,我也不確定它是否會工作?任何人有任何想法如何可以完成?在iphone中在uiwebview中獲取和設置用戶代理?

回答

1

如果實現UIWebViewDelegate類,可以取得與webView:shouldStartLoadWithRequest:navigationType:請求 - 在這一點上,你應該能夠熟練操作的要求,像

[request setValue:@"your custom useragent" forHTTPHeaderField:@"User-Agent"]; 
+0

orHTTPHeaderField:@「User-agent」working – hemant 2010-06-24 11:49:42

0

獲得用戶代理,你可以嘗試使用JavaScript like this或從@cem答案,就可以實現webView:shouldStartLoadWithRequest:navigationType:,然後從傳遞給您的UIWebViewDelegate請求對象讀取頭字段:

[request valueForHTTPHeaderField:@"User-Agent"]; 

對於設置,但是,UIWebViewDelegate方法不適用於我的iOS 5。也許它在過去。

什麼工作對我來說實際上是定義爲UserAgent關鍵的應用範圍的用戶默認值(這不是一個錯字...有話用戶代理之間沒有連字符)。

在我的應用程序的委託,我用了一個初始化,得到這個值之前的任何UIWebViewsNSURLRequests設置創建:

+ (void) initialize { 
    // TODO: put in whatever custom user agent you want here: 
    NSString* userAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19"; 

    NSDictionary* initDefaults = [[NSDictionary alloc] initWithObjectsAndKeys: 
            userAgent, @"UserAgent", 
            nil]; 

    [[NSUserDefaults standardUserDefaults] registerDefaults:initDefaults]; 
} 

注意:如果你不使用ARC,記得在釋放initDefaults上面的代碼片段。