iOS开发总结-UIWebView 集成 浏览器
//
// detailWebViewController.m
// BJ
//
// Created by shirenfeng on 16/11/6.
// Copyright © 2016年 com.wsb123. All rights reserved.
// 浏览器
#import "detailWebViewController.h"
#import "DBStrore.h"
#import "BaseSVRRequestOperator.h"
#import "Utilities.h"
#import "FTMacro.h"
#import "AppDelegate.h"
#import "HttpConnect.h"
#import<CommonCrypto/CommonDigest.h>
@interface detailWebViewController ()<UIWebViewDelegate> @property (nonatomic, strong) UIButton *capPicBtn;
@property (nonatomic, strong) UIButton *shopName;
@property (nonatomic, strong) UIButton *mReloadBtn;
@property (nonatomic, strong) UIWebView *mWebView;
@property (nonatomic, strong) BaseSVRRequestOperator *netWorking;
@property (nonatomic, strong) NSMutableArray *clientArray;
@property (nonatomic,strong) NSString *loadedURL;
#define MAXCOUNT 30
#define MAXTIMEOUT 10
@end @implementation detailWebViewController - (void)viewDidLoad {
[super viewDidLoad]; //_shopName.frame = CGRectMake((kScreenWidth-100)/2,11,100,30);
// _capPicBtn.frame = CGRectMake(kScreenWidth-80,11,80,30); // Do any additional setup after loading the view from its nib.
// self.navigationController.navigationBarHidden = NO; [self initview]; }
-(void)initview
{
for(UIView *view in [self.view subviews])
{
[view removeFromSuperview];
} NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *shopname = [defaults stringForKey:@"shopnamedata"]; UIButton *returnTextBtn = [[UIButton alloc] initWithFrame:CGRectMake(,,,)];
[returnTextBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[returnTextBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[returnTextBtn setTitle:[NSString stringWithFormat:@"返回“选123”"] forState: UIControlStateNormal];
returnTextBtn.backgroundColor = [UIColor clearColor];
[returnTextBtn setTitleColor:self.view.tintColor forState:UIControlStateNormal];
returnTextBtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[returnTextBtn addTarget:self action:@selector(returnApp) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:returnTextBtn]; _shopName = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth-)/,,,)];
[_shopName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_shopName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_shopName.backgroundColor = [UIColor clearColor];
[_shopName setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
_shopName.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[self.view addSubview:_shopName]; _capPicBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-,,,)];
[_capPicBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[_capPicBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_capPicBtn setTitle:[NSString stringWithFormat:@"加入对比"] forState: UIControlStateNormal];
_capPicBtn.backgroundColor = [UIColor clearColor];
[_capPicBtn setTitleColor:self.view.tintColor forState:UIControlStateNormal];
_capPicBtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[_capPicBtn addTarget:self action:@selector(capPicBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_capPicBtn]; _lineView = [[UIView alloc] initWithFrame:CGRectMake(, , kScreenWidth, 0.9)];
_lineView.backgroundColor = [UIColor colorWithHue: saturation: brightness:0.85 alpha:0.85];
[self.view addSubview:_lineView]; _mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(,,kScreenWidth,kScreenHeight-)];
[self.view addSubview:_mWebView]; UIButton *returnimgbtn = [[UIButton alloc] initWithFrame:CGRectMake(,,,)];
[returnimgbtn setImage:[UIImage imageNamed:@"back_b.png"]forState:UIControlStateNormal];
[returnimgbtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[returnimgbtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
returnimgbtn.backgroundColor = [UIColor clearColor];
[returnimgbtn setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
returnimgbtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[returnimgbtn addTarget:self action:@selector(returnApp) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:returnimgbtn]; if([shopname rangeOfString:@"贝贝"].location != NSNotFound || [shopname rangeOfString:@"亚马逊"].location != NSNotFound || [shopname rangeOfString:@"天猫"].location != NSNotFound){
UIButton *backimgbtn = [[UIButton alloc] initWithFrame:CGRectMake(,,,)];
[backimgbtn setImage:[UIImage imageNamed:@"hb_back.png"]forState:UIControlStateNormal];
[backimgbtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[backimgbtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
backimgbtn.backgroundColor = [UIColor clearColor];
[backimgbtn setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
backimgbtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[backimgbtn addTarget:self action:@selector(backPageBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backimgbtn];
} // back_b.png hb_back.png // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// NSString *urlStr = [defaults stringForKey:@"urlshopname"];
// if(urlStr){
// self.title =urlStr;
// } // self.mWebView.scalesPageToFit = YES;
self.mWebView.scrollView.showsVerticalScrollIndicator = YES;
self.mWebView.hidden = YES;
self.mWebView.delegate = self;
self.loadedURL = NULL; self.netWorking = [BaseSVRRequestOperator new];
[Utilities addMyGifImageView:self];
[self addWebViewURL]; [self writeClickRecord]; [self performSelector:@selector(timeOut:) withObject:self afterDelay:MAXTIMEOUT];
}
-(void)writeClickRecord
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *goodsid = [NSNumber numberWithInt:[[defaults stringForKey:@"goodsiddata"] intValue]];
NSNumber *itemid = [NSNumber numberWithInt:[[defaults stringForKey:@"itemiddata"] intValue]];
AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
NSNumber *uuid = [NSNumber numberWithInt:myDelegate.uuid]; HttpConnect * connect = [[HttpConnect alloc] init];
connect.delegate = self; NSString *UDID=[DBStrore sharedDBStore].sessionId;
NSLog(@"write click :%@-%d-%d-%d",UDID,[uuid intValue],[goodsid intValue],myDelegate.share);
NSMutableDictionary * dic = [NSMutableDictionary dictionary]; [dic setObject:UDID forKey:@"udid"];
[dic setObject:uuid forKey:@"uuid"]; if([[defaults stringForKey:@"itemiddata"] intValue]==-){
NSLog(@"itemid=%d",[itemid intValue]); }
else if([[defaults stringForKey:@"itemiddata"] intValue]!=){
NSLog(@"itemid=%d",[itemid intValue]);
[dic setObject:itemid forKey:@"itemid"];
[connect postRequestForPostValue:dic url:@"addclick.php" requestTag:];
}
else{
NSNumber *adid = [NSNumber numberWithInt:[[defaults stringForKey:@"adiddata"] intValue]];
NSLog(@"adid=%d",[adid intValue]);
[dic setObject:adid forKey:@"adid"];
[connect postRequestForPostValue:dic url:@"addadclick.php" requestTag:];
} } -(void)postRequestStarted:(AFHTTPRequestOperation*)request manage:(AFHTTPRequestOperationManager*)manage{ NSLog(@"REQ START");
}
-(void)postRequestFinished:(AFHTTPRequestOperation*)request manage:(AFHTTPRequestOperationManager*)manage{ NSLog(@"REQ FINISHED"); NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableLeaves error:nil ];
NSString * status = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"status"]]; if([status isEqualToString: @""]){ NSLog(@"REQ success");
}
else{
NSLog(@"REQ error status=@%@",status);
} }
-(void)postRequestFailed:(AFHTTPRequestOperation*)request manage:(AFHTTPRequestOperationManager*)manage{ NSLog(@"REQ FAILED"); } - (void)timeOut:(id)time
{
[Utilities removeMyGifImageView:self.view]; if(self.loadedURL == NULL){
UIButton *reloadBtn = [[UIButton alloc] initWithFrame:CGRectMake(/,/,kScreenWidth-,kScreenHeight-)];
[reloadBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[reloadBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[reloadBtn setTitle:[NSString stringWithFormat:@"重试"] forState: UIControlStateNormal];
reloadBtn.backgroundColor = [UIColor clearColor];
[reloadBtn setTitleColor:self.view.tintColor forState:UIControlStateNormal];
reloadBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
[reloadBtn addTarget:self action:@selector(initview) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:reloadBtn];
_mReloadBtn=reloadBtn; }
}
- (void)backPageBtn
{
NSLog(@"backPageBtn");
if([self.mWebView canGoBack])
{
[self.mWebView goBack];
}
else
{
[[DBStrore sharedDBStore] removeInformationView:];
}
}
- (void)addWebViewURL
{
// NSString *urlStr = @"http://www.jd.com";
// NSURL *URL = [NSURL URLWithString:str]; //string>url
// NSString *str1 = [URL absoluteString]; //url>string
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlStr = [defaults stringForKey:@"urldata"];
NSString *shopname = [defaults stringForKey:@"shopnamedata"]; [_shopName setTitle:shopname forState: UIControlStateNormal]; NSLog(@"webpage old url:%@",urlStr); //urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF32StringEncoding];
NSLog(@"webpage url:%@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:MAXTIMEOUT];
[self.mWebView loadRequest:request];
NSLog(@"webpage request ok ");
}
- (void)returnApp
{
NSLog(@"returnApp");
[[DBStrore sharedDBStore] removeInformationView:];
} - (void)capPicBtnClick
{
NSLog(@"capPic");
if(self.loadedURL != NULL){ UIGraphicsBeginImageContextWithOptions(CGSizeMake(kCapPicWidth, kCapPicHeight), YES, ); //设置截屏大小
[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = viewImage.CGImage;
CGRect rect = CGRectMake(kCapPicStartx, kCapPicStarty, kCapPicWidth, kCapPicHeight);//这里可以设置想要截图的区域
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
// UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库
NSData *imageViewData = UIImagePNGRepresentation(sendImage); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:];
NSString *pictureName= [NSString stringWithFormat:@"screenShow_%@.png",[self md5:self.loadedURL]];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; NSLog(@"url: %@", self.loadedURL);
NSString *titlename=[_shopName.titleLabel.text stringByReplacingOccurrencesOfString:@"搜" withString:@""];
titlename=[titlename stringByReplacingOccurrencesOfString:@"同品类" withString:@""];
NSString *key= [NSString stringWithFormat:@"%@,%@,%@",titlename,savedImagePath,self.loadedURL]; NSLog(@"capPicBtnClick count: %d",[DBStrore sharedDBStore].CapPicArray.count); NSFileManager *fileManager=[[NSFileManager alloc] init];
NSError *error; for(int i=;i<[DBStrore sharedDBStore].CapPicArray.count;i++){
if([[DBStrore sharedDBStore].CapPicArray[i] isEqualToString:key]){
NSLog(@"remove at %d",i); if([fileManager fileExistsAtPath:savedImagePath]){
NSLog(@"del imgpath:%@",savedImagePath);
[fileManager removeItemAtPath:savedImagePath error: &error];
if(error){
NSLog(@"del file error = %@",error);
}
}
[imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录
CGImageRelease(imageRefRect);
[self archivedCapPic]; NSLog(@"notify freshCapPicCount");
[[NSNotificationCenter defaultCenter] postNotificationName:@"freshCapPicCount" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"capPicChanged" object:nil userInfo:nil];
[Utilities showAlertView:@"加入成功"];
return ;
}
} [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录
CGImageRelease(imageRefRect); if([DBStrore sharedDBStore].CapPicArray.count == MAXCOUNT)
{
NSFileManager *fileManager=[[NSFileManager alloc] init];
NSError *error; NSString *keytmp = [DBStrore sharedDBStore].CapPicArray[];
NSArray *tmparr=[keytmp componentsSeparatedByString:@","];
NSString *imagePath=tmparr[];
if([fileManager fileExistsAtPath:imagePath]){
NSLog(@"del imgpath:%@",imagePath);
[fileManager removeItemAtPath:imagePath error: &error];
if(error){
NSLog(@"del file error = %@",error);
}
} [[DBStrore sharedDBStore].CapPicArray removeObjectAtIndex:]; }
NSLog(@"3 saveKey count: %d",[DBStrore sharedDBStore].CapPicArray.count);
[[DBStrore sharedDBStore].CapPicArray addObject:key];
NSLog(@"4 saveKey count: %d",[DBStrore sharedDBStore].CapPicArray.count);
[self archivedCapPic]; NSLog(@"notify freshCapPicCount");
[[NSNotificationCenter defaultCenter] postNotificationName:@"freshCapPicCount" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"capPicChanged" object:nil userInfo:nil]; [Utilities showAlertView:@"加入成功"];
}
else{
[Utilities showAlertView:@"加载中。。请稍后再试"];
}
}
- (NSString *) md5:(NSString *) input {
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * ]; for(int i = ; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]]; return output;
}
- (void)archivedCapPic
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *bindUserLocalSettingDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:[DBStrore sharedDBStore].CapPicArray];
[defaults setObject:bindUserLocalSettingDictionaryData forKey:FTcapPic];
[defaults synchronize]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark UIWebViewDelegate - (void )webViewDidFinishLoad:(UIWebView *)webView
{
// [self.mWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"common.checkIos('%@')", @"ios"]];
self.mWebView.hidden = NO;
[Utilities removeMyGifImageView:self.view];
self.loadedURL=[NSString stringWithFormat:@"%@",webView.request.URL.absoluteString];
NSLog(@"webViewDidFinishLoad url:%@",webView.request.URL.absoluteString);
if(_mReloadBtn!=nil){
[_mReloadBtn removeFromSuperview];
_mReloadBtn=nil;
} } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[Utilities removeMyGifImageView:self.view]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{ if(request != nil){
NSURL *url = request.URL; if(url !=nil){
// 要处理的代码
// ...... NSLog(@"url.scheme=%@",url.scheme);
//拦截app跳转
if ([url.scheme isEqualToString:@"weixin"])
{
NSLog(@"return weixin");
return YES;
}
if ([url.scheme isEqualToString:@"alipay"])
{
NSLog(@"return alipay");
return YES;
}
if (!([url.scheme isEqualToString:@"http"]) && !([url.scheme isEqualToString:@"https"]))
{
NSLog(@"return no");
return NO;
}
NSLog(@"return yes"); }
} return YES;
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end 加扣群385378208即可下载工程源码
加扣群385378208即可下载工程源码
iOS开发总结-UIWebView 集成 浏览器的更多相关文章
- iOS开发之支付宝集成
项目中要用到支付功能,需要支付宝,微信,银联三大支付,所以打算总结一下,写两篇文章,方便以后的查阅, 大家在做的时候也能稍微参考下,用到的地方避免再次被坑.这是第二篇支付宝集成,第一篇银联支付在这里. ...
- 【iOS开发】UIWebView与JavaScript(JS) 回调交互
------------------------------------------------- 很多关于objc 与 js 交互的文章都比较适用于 mac开发,iOS的webview 还是有所不一 ...
- ios开发之--UIWebView全属性
最近的项目当中需要用到html和ios的交互,所以就凑空整理一下,所有webView相关的方法和属性,如有不对的地方,请大家不吝指教! 代码如下: 1,创建webview并设置代理 UIWebView ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS开发--iOS及Mac开源项目和学习资料
文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...
- iOS开发常用第三方库
UI 动画 网络相关 Model 其他 数据库 缓存处理 PDF 图像浏览及处理 摄像照相视频音频处理 响应式框架 消息相关 版本新API的Demo 代码安全与密码 测试及调试 AppleWatch ...
- iOS开发之资料收集
github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自github:https://github ...
- iOS开发 非常全的三方库、插件、大牛博客等等
UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...
- iOS开发---集成百度地图完善版
一.成为百度的开发者.创建应用 http://developer.baidu.com/map/index.php?title=首页 (鼠标移向 然后选择你的项目需要的功能 你可以在里面了解到你想要使用 ...
随机推荐
- OSPF + LVS ,突破LVS瓶颈 (转)
突破LVS瓶颈,LVS Cluster部署(OSPF + LVS) 前言 架构简图 架构优势 部署方法 1.硬件资源准备 2.三层设备OSPF配置 3.LVS调度机的OSPF配置 a.安装软路由软件q ...
- CentOS 7.0 重置root密码
步骤一,开机时随便按下键盘,进入以下菜单 步骤二: 选择第一项,按e键进行修改 步骤三,定位到 ro( linux 16 or linuxefi ) 步骤四:把ro改成 “rw init=/sysro ...
- HTML5 程序设计笔记(二)
Canvas API 1.HTML5 Canvas 概述 1.1 历史 Canvas的概念最初是由苹果公司提出的,用于在Mac OS X WebKit中创建控制板部件(dashboard widget ...
- 【转】oracle数据库NUMBER数据类型
原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a) precision表示数字中的有效位;如果没有指定prec ...
- 多线程-GCD学习笔记
********************************* 基本概念 *********************************** 1. Grand Central Dispatch ...
- SKProductsRequest ios 7不调用delegate
在iOS7中,内购只能在真机上才会调用 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProdu ...
- UIKit的手风琴菜单,单条展开和多条同时展开
这个也要进来看看哈. 记得加多个属性时的用法就可以了. 因为官网提供太多的SAPMLE啦.. http://www.getuikit.net/docs/accordion.html <div c ...
- Principles of Motion Sensing
Principlesof Motion Sensing Various sensors capable of detecting motionin free space have been comme ...
- Android开源项目发现--- 效率开发工具篇(持续更新)
1.Json2Java 根据JSon数据自动生成对应的Java实体类,还支持Parcel.Gson Annotations对应代码自动生成.期待后续的提取父类以及多url构建整个工程的功能 项目地址: ...
- 纯css切换左侧菜单
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...