iOS WebView獲取title, ip,對webview頁面截圖抓取,修改UA電腦網(wǎng)頁與手機(jī)網(wǎng)頁切換

UIWebView十分適合冬日里讓手機(jī)給取暖

創(chuàng)建UIWebView?

獲取頁面的名稱

webtitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];

獲取當(dāng)前頁面的ip地址

NSString *hostname = _domainStr;

調(diào)用此方法應(yīng)是傳入域名,當(dāng)前頁面url獲取不到

CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostname);

if (hostRef)

{

Boolean result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);

if (result == TRUE)

{

NSArray *addresses = (__bridge NSArray*)CFHostGetAddressing(hostRef, &result);

tempDNS = [[NSMutableArray alloc] init];

for(int i = 0; i < addresses.count; i++)

{

struct sockaddr_in* remoteAddr;

CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex((__bridge CFArrayRef)addresses, i);

remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);

if(remoteAddr != NULL)

{

const char *strIP41 = inet_ntoa(remoteAddr->sin_addr);

NSString *strDNS =[NSString stringWithCString:strIP41 encoding:NSASCIIStringEncoding];

//? NSLog(@"RESOLVED %d:<%@>", i, strDNS);

[tempDNS addObject:strDNS];

}}}}

修改uesr-agent ?電腦網(wǎng)頁與手機(jī)網(wǎng)頁的切換

NSDictionary*dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)", @"UserAgent", nil];

[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

修改UA瀏覽電腦網(wǎng)頁。此方法需在loadRequest前調(diào)用 否則無效

NSDictionary*dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69", @"UserAgent", nil];[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

修改UA瀏覽手機(jī)網(wǎng)頁。此方法需在loadRequest前調(diào)用 否則無效

對webview全頁面進(jìn)行抓取,主要是對webview中scrollview的抓取

首先在抓取頁面時,先將webView.scrollView置為初始。setScalesPageToFit = yes之前設(shè)置了自適應(yīng)

[webView.scrollView setZoomScale:self.webView.scrollView.minimumZoomScale animated:YES];

[webView.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

對webview頁面進(jìn)行繪制圖片

- (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets {

CGFloat scale = [UIScreen mainScreen].scale;

CGSize boundsSize = self.webView.bounds.size;

CGFloat boundsHeight = boundsSize.height;

CGSize contentSize = self.webView.scrollView.contentSize;

CGFloat contentHeight = contentSize.height;

CGFloat contentWidth = contentSize.width;

CGPoint offset = self.webView.scrollView.contentOffset;

[self.webView.scrollView setContentOffset:CGPointMake(0, 0)];

NSMutableArray *images = [NSMutableArray array];

while (contentHeight > 0) {

preMemory = [self usedMemory];//監(jiān)控內(nèi)存

UIGraphicsBeginImageContextWithOptions(boundsSize, NO, [UIScreen mainScreen].scale);

[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[images addObject:image];

CGFloat offsetY = self.webView.scrollView.contentOffset.y;

[self.webView.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];

contentHeight -= boundsHeight;

}

[self.webView.scrollView setContentOffset:offset];

CGSize imageSize = CGSizeMake(contentSize.width * scale,contentSize.height * scale);

UIGraphicsBeginImageContext(imageSize);

[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {

[image drawInRect:CGRectMake(0,scale * boundsHeight * idx,scale * boundsWidth,scale * boundsHeight)];}];

UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView * snapshotView = [[UIImageView alloc]initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];

snapshotView.image = [fullImage resizableImageWithCapInsets:capInsets];

[self handleActivityStop];

return snapshotView.image;

}網(wǎng)頁過大會引起內(nèi)存爆炸,生成的圖片在40M以內(nèi)沒問題

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容