首页 > 代码库 > UIWebView自定义user agent

UIWebView自定义user agent

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } span.s1 { color: #78492a } span.s2 { } span.s3 { color: #bb2ca2 } span.s4 { color: #000000 } span.s5 { color: #703daa } span.s6 { color: #3d1d81 } span.s7 { color: #d12f1b } span.s8 { color: #272ad8 } span.s9 { color: #31595d } span.s10 { color: #4f8187 } span.Apple-tab-span { white-space: pre }</style>

#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