UIWebView - 1

UIWebview的使用和学习都不难,直接贴上来的一个例子,并且带有菊花的

#import "ViewController.h"
#import "OpenUDID.h"            //获取用户的OpenUDID
#import "macaddress.h"          //获取用户的macaddress
#import <AdSupport/AdSupport.h>

#define WAP_URL @"requested web address "

@interface ViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView* webView;
@property (nonatomic, strong) NSString* mainUrl;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加加载画面
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 48.0f, 48.0f)];
    [_activityIndicator setCenter:self.view.center];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [_activityIndicator setHidesWhenStopped:YES];
    [_activityIndicator startAnimating];
    
    // 添加WebView并加载WAP_URL
    NSString * platform = @"ios";
    NSString * bundleid = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSString * version  = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString * openudid = [OpenUDID value];
    NSString * idfv     = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSString * macaddr  = [MacAddress GetMacAddress];
    NSString * idfa     = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    
    self.mainUrl =[NSString stringWithFormat:WAP_URL,
                   platform,
                   bundleid,
                   macaddr,
                   idfa,
                   idfv,
                   openudid,
                   version];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.mainUrl]
                                          cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                      timeoutInterval:120.00]];
    [self.webView setDelegate:self];
    self.webView.hidden = YES;

    [self.view addSubview:_activityIndicator];
    [self.view addSubview:self.webView];
}

// 更新app应用
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *urlStr = [[request  URL] absoluteString];
    
    //结合 itms协议,如果点击了协议之后页面立即返回主页,不然默认空白
    if ([urlStr hasPrefix:@"itms-services"]) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        
        
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.mainUrl]
                                                   cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                               timeoutInterval:120.0]];
        [self.view addSubview:self.webView];
        
        return NO;
    }
    
    return YES;
}

# pragma mark - Web View Delegate
// 数据加载完
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_activityIndicator stopAnimating];
    webView.hidden = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end


其实UIWebView还有很多缓存啊等等的很多的接口方法,都有待我们去学习和使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值