Change User Agent in UIWebView

前端 未结 14 1833
孤城傲影
孤城傲影 2020-11-22 17:03

I have a business need to be able to customize the UserAgent for an embedded UIWebView. (For instance, I\'d like the server to respond differently if, say, a user is using

14条回答
  •  有刺的猬
    2020-11-22 17:58

    By pooling the answer by Louis St-Amour and the NSUserDefaults+UnRegisterDefaults category from this question/answer, you can use the following methods to start and stop user-agent spoofing at any time while your app is running:

    #define kUserAgentKey @"UserAgent"
    
    - (void)startSpoofingUserAgent:(NSString *)userAgent {
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{ kUserAgentKey : userAgent }];
    }
    
    - (void)stopSpoofingUserAgent {
        [[NSUserDefaults standardUserDefaults] unregisterDefaultForKey:kUserAgentKey];
    }
    

提交回复
热议问题