iOS WebView获取title, ip,对webview页面截图抓取,修改UA电脑网页与手机网页切换

UIWebView十分适合冬日里让手机给取暖

创建UIWebView 

获取页面的名称

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

获取当前页面的ip地址

NSString *hostname = _domainStr;

调用此方法应是传入域名,当前页面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  电脑网页与手机网页的切换

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浏览电脑网页。此方法需在loadRequest前调用 否则无效

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浏览手机网页。此方法需在loadRequest前调用 否则无效

对webview全页面进行抓取,主要是对webview中scrollview的抓取

首先在抓取页面时,先将webView.scrollView置为初始。setScalesPageToFit = yes之前设置了自适应

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

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

对webview页面进行绘制图片

- (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];//监控内存

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;

}网页过大会引起内存爆炸,生成的图片在40M以内没问题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,430评论 0 3
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,741评论 0 3
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    MMOTE阅读 1,700评论 1 1
  • 在残酷的生活中,为了心中那美好的梦想,我们勇往直前像射出的弓箭一样。如飞娥扑火般的无所畏惧,或许这就是青春。 在这...
    sobule阅读 117评论 0 0
  • 每个人都有回忆。 回忆里总是有若干林林总总的人候在某一处,等你回头,等你想起。 ---题记 ...
    106ebf7e78a0阅读 295评论 1 2