zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    快速了解MongoDB
    如何在 Ubuntu 上安装 MongoDB
    Python操作MongoDB文档数据库
    C# Convert.ToDouble 在不同的区域下的问题
    Visual Studio 调试C#程序时,提示“无法加载程序集****.XmlSerializers.dll",文件找不到 的问题
    C# 调用打印机打印文件
    C# 获取所有可用的打印机
    记录一次排查使用HttpWebRequest发送请求的发生“基础连接已关闭:接收时发生错误”异常问题的过程
    SQL 分组后只获取每组的一条数据
    记录一次使用NPOI遇到的问题
  • 原文地址:https://www.cnblogs.com/YuFly-lyx/p/6060985.html
Copyright © 2011-2022 走看看