“口袋郴州”项目总结-UIwebView用法总结(附带下拉刷新)

//
//  ViewController.h
//  KouDaiChenhZhou
//
//  Created by jishubumac on 14/11/7.
//  Copyright (c) 2014年 jishubumac. All rights reserved.
//
//UIWebView下拉刷新框架原创下载地址:http://code4app.com/ios/UIWebView-%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0/51aee3c16803fa770f000003
#import <UIKit/UIKit.h>
#import "EGORefreshTableHeaderView.h"//导入下拉刷新框架头文件

//引用协议
@interface ViewController : UIViewController<UIWebViewDelegate, UIScrollViewDelegate, EGORefreshTableHeaderDelegate,UITabBarDelegate> {
    //下拉视图
    EGORefreshTableHeaderView * _refreshHeaderView;
    //刷新标识,是否正在刷新过程中
    BOOL _reloading;
}

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSURL *homeurl;//主页url

//
//  ViewController.m
//  KouDaiChenhZhou
//
//  Created by jishubumac on 14/11/7.
//  Copyright (c) 2014年 jishubumac. All rights reserved.
//

#import "ViewController.h"
#import "PublicHeader.h"
#import <ShareSDK/ShareSDK.h>
#import <AdSupport/AdSupport.h>//为了获取User Agent而导入

@interface ViewController ()
{
    UIActivityIndicatorView *loadView;
    NSMutableURLRequest *_request;
    UIView *bgview;
    UILabel *titlelabel;
}

@end

@implementation ViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
     
    }
    return self;
}
//添加背景
- (void)addbackgroundView
{
    bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
    bgview.backgroundColor = [UIColor colorWithRed:0.0 green:0.85 blue:0.0 alpha:1.0];
    [self.navigationController.navigationBar addSubview:bgview];
}

//页标题
- (void)addLabel
{
    titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 100, 20)];
    titlelabel.font = [UIFont boldSystemFontOfSize:20.0f];
    titlelabel.textColor = [UIColor whiteColor];
    //    titlelabel.text = @"口袋郴州";
    [bgview addSubview:titlelabel];
}

- (void)addButton
{
    //分享按钮
    UIButton *sharebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sharebtn.frame = CGRectMake(self.view.frame.size.width - 22-15, 11, 22, 22);
    [sharebtn setImage:[UIImage imageNamed:@"分享_当前.png"] forState:UIControlStateNormal];
    [sharebtn setImage:[UIImage imageNamed:@"分享_按下.png"] forState:UIControlStateHighlighted];
    [sharebtn addTarget:self action:@selector(didshare) forControlEvents:UIControlEventTouchUpInside];
    [bgview addSubview:sharebtn];
}

- (void)addActivityIndicatorView
{
    loadView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    loadView.center = self.view.center;
    loadView.color=[UIColor colorWithWhite:0.4 alpha:1];
    [self.view addSubview:loadView];
    [loadView startAnimating];
}

//网页加载请求数据
- (void)webViewLoadRequest:(NSURL *)url
{
    _request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [_webView loadRequest:_request];
    NSLog(@"%@",_request);
}

- (void)viewDidAppear:(BOOL)animated
{
    [self webViewLoadRequest:_homeurl];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //识别客户端
    NSDictionary *dictionary = @{@"UserAgent": @"Mozilla/5.0 (iPhone; CPU iPhone OS 10_10 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12A365  client-cz365;{mac:57DBCBDC-597B-458E-91C4-D85C10597384;{token:57DBCBDC-597B-458E-91C4-D85C10597384;"};
    NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSLog(@"%@",adId);
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    [self addbackgroundView];
    [self addLabel];
    [self addButton];
    [self addActivityIndicatorView];
    //初始化webView
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - self.tabBarController.tabBar.frame.size.height)];
    _webView.scalesPageToFit = YES;//按比例适配屏幕
      _webView.detectsPhoneNumbers = YES;//允许获取网页上的号码并拨打
    [_webView  setUserInteractionEnabled: YES ];  //是否支持交互
    
    _webView.keyboardDisplayRequiresUserAction = YES;//交互时显示键盘
    _webView.delegate = self;
    _webView.scrollView.delegate = self;//设置代理,这一环对添加下拉刷新至关重要
    
    
    
    //初始化refreshView,添加到webview 的 scrollView子视图中
    if (_refreshHeaderView == nil) {
        _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0-self.webView.scrollView.bounds.size.height, self.webView.scrollView.frame.size.width, self.webView.scrollView.bounds.size.height)];
        _refreshHeaderView.delegate = self;
        [self.webView.scrollView addSubview:_refreshHeaderView];
    }
    [_refreshHeaderView refreshLastUpdatedDate];
    
    _homeurl = [NSURL URLWithString:@"http://m.cz365.com/"];
    [self webViewLoadRequest:_homeurl];

}

#pragma mark - webViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

//回调时获取的四种不同的url
    //    http://m.cz365.com/user/error_login.aspx
    //    cz365callback://openurl?url=/quan/
    //    cz365callback://gotopage?page=0&url=/forum/forum-5-1.html
    //  cz365callback://openurl?url=http://m.cz365.com/lottery/detail.aspx
    //判断导航类型
    if (navigationType == UIWebViewNavigationTypeOther) {

//回调跳转时,捕获设置标题的url,通过url解码,判别和字符串处理,得到标题
        NSString *str2 = [[request.URL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",str2);
        if ([str2 hasPrefix:@"cz365callback://settitle?type=left&title="]) {
            NSMutableString *mstr2 = [NSMutableString stringWithString:str2];
            [mstr2 deleteCharactersInRange:NSMakeRange(0, 41)];
            titlelabel.text = mstr2;        }
        if ([str2 hasPrefix:@"cz365callback://settitle?type=center&title="]){
            NSMutableString *mstr3 = [NSMutableString stringWithString:str2];
            [mstr3 deleteCharactersInRange:NSMakeRange(0, 43)];
            titlelabel.text = mstr3;
        }
        
    }
    //判断是否单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        DetailViewController *DVCtrl = [[DetailViewController alloc] init];

//获取url
        NSURL *url = [request URL];
        NSString *curUrl= [url absoluteString];
        //解码
        NSString *str = [curUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        //判别4种url,并进行字符串处理,提取url
        if([str hasPrefix:@"http://"]){
            //在当前页显示
        }
        else{
            NSMutableString *mstr = [NSMutableString stringWithString:str];
            [mstr deleteCharactersInRange:NSMakeRange(0,16)];
            if ([mstr hasPrefix:@"openurl?url="]) {
                [mstr deleteCharactersInRange:NSMakeRange(0,12)];
                if ([mstr hasPrefix:@"http://"]) {

// 个别特殊的,需要编码才能打开的url
                    if ([mstr isEqualToString:@"http://m.news.so.com/ns?q=郴州&pq=&src=srp&_ms=0&log_id=5370304"] ) {
                        NSString * encodingString = [mstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                        DVCtrl.currenturl = [NSURL URLWithString:encodingString];// 若用未编码(含中文)的字符串初始化url,则初始化的url为空
                        NSLog(@"%@",encodingString);
                    }
                    else{
                        
                        DVCtrl.currenturl = [NSURL URLWithString:mstr];//属性传值
                        NSLog(@"%@",mstr);
                    }
                }
                else{
                    NSString *urlstr = [NSString stringWithFormat:@"http://m.cz365.com%@",mstr];
                    DVCtrl.currenturl = [NSURL URLWithString:urlstr];
                }
                [self.navigationController pushViewController:DVCtrl animated:YES];//push到窗口二显示
                self.tabBarController.tabBar.hidden = YES;//隐藏tabBar
                [self.navigationController setNavigationBarHidden:YES animated:YES];//隐藏导航条
                
            }
            
            else{
                NSString *numstr = [mstr substringWithRange:NSMakeRange(14, 1)];
                NSLog(@"%@",numstr);
                NSInteger index = [numstr intValue];
                NSLog(@"%d",index);
                //传送tabBar.selectedIndex当前值
                [[NSNotificationCenter defaultCenter] postNotificationName:@"selectedIndex" object:numstr];
                [mstr deleteCharactersInRange:NSMakeRange(0,20)];
                NSString *urlstr2 = [NSString stringWithFormat:@"http://m.cz365.com%@",mstr];
                //给不同页面传其当前url,并在选中的页面打开该url
                switch (index) {
                    case 0:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Firstnotification" object:urlstr2];self.tabBarController.selectedIndex = index;}
                        break;
                    case 1:{
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"Secondnotification" object:urlstr2];self.tabBarController.selectedIndex = index;
                    }
                        break;
                    case 2:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Thirdnotification" object:urlstr2];self.tabBarController.selectedIndex = index;}
                        break;
                    case 3:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Fourthnotification" object:urlstr2];self.tabBarController.selectedIndex = 3;}
                        break;
                    case 4:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Fifthnotification" object:urlstr2];self.tabBarController.selectedIndex = 4;}
                        break;
                    default:
                        break;
                }
                return NO;
            }
        }
        
    }
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"Start loading...");
    _reloading = YES;
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

//webView重载完成后,禁止刷新
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.webView.scrollView];

//获取原有User Agent
    NSString *secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];//OC和js交互
    NSLog(@"%@",secretAgent);
    NSLog(@"finished");
    [self.view addSubview:_webView];
    [loadView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"load page error:%@", [error description]);

_reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.webView.scrollView];

}

- (void)didshare
{
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK"  ofType:@"jpg"];
    
    //构造分享内容
    id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
                                       defaultContent:@"默认分享内容,没内容时显示"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"ShareSDK"
                                                  url:@"http://www.sharesdk.cn"
                                          description:@"这是一条测试信息"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    [ShareSDK showShareActionSheet:nil
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions: nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                if (state == SSResponseStateSuccess)
                                {
                                    NSLog(@"分享成功");
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                                }
                            }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    
    [_webView reload];//下拉刷新,重新加载网页
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return _reloading; // should return if data source model is reloading
    
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
}

@end

时间: 2024-10-08 04:44:54

“口袋郴州”项目总结-UIwebView用法总结(附带下拉刷新)的相关文章

Android 下拉刷新上拉加载效果功能,使用开源项目android-pulltorefresh实现

应用场景: 在App开发中,对于信息的获取与演示,不可能全部将其获取与演示,为了在用户使用中,给予用户以友好.方便的用户体验,以滑动.下拉的效果动态加载数据的要求就会出现.为此,该效果功能就需要应用到所需要的展示页面中. 知识点介绍: 本文主要根据开源项目android-pulltorefresh展开介绍. android-pulltorefresh [一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView.ViewPager.WevView.ExpandableListView.G

MJRefresh–用法最简单的下拉刷新框架

简介 用于为应用添加常用的上拉加载更多与下拉刷新效果,适用 UIScrollView . UITableView . UICollectionView . UIWebView. 项目主页: MJRefresh 最新示例: 点击下载 快速入门 使用环境 ARC iOS 6.0 + 安装 通过CocoaPods安装 pod 'MJRefresh' 手动安装 将 MJRefresh 文件夹中的所有文件拽入项目中,在需要的地方导入主头文件: 类结构图 MJRefreshComponent.h /** 刷

PullToRefreshListView上拉加载、下拉刷新 eclipse项目

说明:此项目实在fragment中的,需要依赖library完成,还用到了Xuitls.jar包.使用了Pull解析XML package com.bwie.test.fragment; import java.io.ByteArrayInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List; import me.XListView; import org.xmlpull.v1.

你必须了解的RecyclerView的五大开源项目-解决上拉加载、下拉刷新和添加Header、Footer等问题

前段时间做项目由于采用的MD设计,所以必须要使用RecyclerView全面代替ListView.但是开发中遇到了需要实现RecyclerView上拉加载.下拉刷新和添加Header以及Footer等需求问题,现将问题解决中用到的五大开源项目总结下来,方便他人. 首先介绍下RecyclerView,RecyclerView相比ListView增加了很多新特性: ? Adapter中的ViewHolder模式 - 对于ListView来说,通过创建ViewHolder来提升性能并不是必须的.因为L

IOS UIWebView 下拉刷新功能的简单实现

1.运行效果图 2.swift 代码的实现 import UIKit class RefreshWebViewController: UIViewController,UIScrollViewDelegate,UIWebViewDelegate{ @IBOutlet weak var webView: UIWebView! //刷新控件view private var header:UIView! //下拉到50时松开刷新 private let pullHeight:CGFloat=50 //

【转】google推出的SwipeRefreshLayout下拉刷新用法

SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便. 使用如下: 1.先下载android-support-v4.jar最新版本,之前的版本是没有SwipeRefreshLayout下拉刷新控件的,如果已经更新,此步骤可省略. 2.在xml文件中引用android.support.v4.widget.SwipeRefreshLayout控件,在里面可以放置任何一个控件,例如ListView,gridvi

google推出的SwipeRefreshLayout下拉刷新用法

使用如下: 1.先下载android-support-v4.jar最新版本,之前的版本是没有SwipeRefreshLayout下拉刷新控件的,如果已经更新,此步骤可省略. 2.在xml文件中引用android.support.v4.widget.SwipeRefreshLayout控件,在里面可以放置任何一个控件,例如ListView,gridview等. [html] view plaincopy <android.support.v4.widget.SwipeRefreshLayout a

Android-PullToRefresh下拉刷新库基本用法

PullToRefresh是一套实现非常好的下拉刷新库,它支持: ListView ExpandableListView GridView WebView ScrollView HorizontalScrollView ViewPager 等多种常用的需要刷新的View类型,而且使用起来也十分方便. (下载地址:https://github.com/chrisbanes/Android-PullToRefresh) PullToRefresh基本用法: 1.在布局文件中添加PullToRefre

iOS开发项目篇—30下拉刷新

iOS开发项目篇—30下拉刷新 一.网络监控 当应用所处的网络环境不好的时候,获取不到相应的网络数据,考虑到用户对应用的使用体验,有必要对网络的状况进行监听. 在程序启动完的时候,监控网络 YYAppDelegate.m文件代码: 1 // 2 // YYAppDelegate.m 3 // 4 5 #import "YYAppDelegate.h" 6 #import "YYOAuthViewController.h" 7 #import "YYCont