Lmsgsendnilself

Uninhibited Soul, Free Craziness

自定义UIWebview的user-agent

| Comments

注册 user-agent

1
2
3
4
5
6
7
8
- (void)registerUserAgent {
    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectZero] ;
    NSString *oldAgent = [webview stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *appendAgent = [NSString stringWithFormat:@" %@/%@ (%@)", APP_NAME,APP_BUILD_VERSION, PLATFORM_NAME];
    NSString *newAgent = [oldAgent stringByAppendingString:appendAgent];
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
}


移除user-agent

1
2
3
4
5
6
7
8
9
-(void)removeUserAgent{
    NSDictionary *registeredDefaults = [[NSUserDefaults standardUserDefaults] volatileDomainForName:NSRegistrationDomain];
    if ([registeredDefaults objectForKey:@"UserAgent"] != nil)
    {
        NSMutableDictionary *mutableCopy = [NSMutableDictionary dictionaryWithDictionary:registeredDefaults];
        [mutableCopy removeObjectForKey:@"UserAgent"];
        [[NSUserDefaults standardUserDefaults] setVolatileDomain:mutableCopy forName:NSRegistrationDomain];
    }
}

Comments