UIWebView自定义user agent

Posted YuFly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIWebView自定义user agent相关的知识,希望对你有一定的参考价值。

#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的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义 UIWebView 的 MoviePlayer

ios UIWebView自定义Alert风格的弹框

UIWebView 内容宽度到自定义大小

如何在 FlipBoard 或 MobileRss 中识别自定义 UIWebView

如何向自定义 UIWebview 添加“打开方式”功能?

uiwebview 中的 uimenucontroller 带有自定义菜单项,没有更多菜单