码迷,mamicode.com
首页 > Web开发 > 详细

UIWebView自定义user agent

时间:2016-11-14 12:34:15      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:1.0   add   reload   suse   dispose   bounds   color   ide   ati   

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

 

@end

 

@implementation WebViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

+(void)registUserAgent{

   

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        UIWebView *webView=[[UIWebView alloc]init];

        

        

        NSString *oldUA = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

        

        NSString *newUA = [NSString stringWithFormat:@"%@%@",oldUA,@"你的userAgent"];

        NSDictionary *dictionary = @{@"UserAgent":newUA};

        

        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

        

       

    });

}

 

 

-(void)loadView

{

    [super loadView];

 

    [WebViewController registUserAgent];

    

if(!webView)

    {

        webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

        webView.delegate=self;

        if([[[UIDevice currentDevice] systemVersion] floatValue]>6.9)

        webView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20);

 

        

        NSString* urlString = @"你的网址";

 

        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];

    }

    [self.view addSubview:webView];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

}

 

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (void)reload{

    [webView reload];

}

 

 

@end

UIWebView自定义user agent

标签:1.0   add   reload   suse   dispose   bounds   color   ide   ati   

原文地址:http://www.cnblogs.com/YuFly-lyx/p/6060985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!