Skip to content

Instantly share code, notes, and snippets.

@yaakaito
Created April 3, 2012 23:53
Show Gist options
  • Save yaakaito/2296450 to your computer and use it in GitHub Desktop.
Save yaakaito/2296450 to your computer and use it in GitHub Desktop.
iOSアプリでUAが知りたかったけど、見つからなかったので力技で出すコード 4.3~ (対応してないのはそれっぽいのが出るようになってる)
#import "UserAgent.h"
@implementation UserAgent
// iOS4.3 later
+ (NSString *)defaultUserAgent {
// shared
static dispatch_once_t pred = 0;
__strong static NSMutableDictionary *userAgentTable;
dispatch_once(&pred, ^{
userAgentTable = [[NSMutableDictionary alloc] init];
// iPhone
// 4.3
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5" forKey:@"iPhone_4.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5" forKey:@"iPhone_4.3.1"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5" forKey:@"iPhone_4.3.2"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" forKey:@"iPhone_4.3.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_4 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8K2 Safari/6533.18.5" forKey:@"iPhone_4.3.4"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_5 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5" forKey:@"iPhone_4.3.4"];
// 5.0
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334 Safari/7534.48.3" forKey:@"iPhone_5.0"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A405 Safari/7534.48.3" forKey:@"iPhone_5.0.1"];
// 5.1
[userAgentTable setObject:@"Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3" forKey:@"iPhone_5.1"];
// iPod Touch
// 4.3
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5" forKey:@"iPod Touch_4.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5" forKey:@"iPod Touch_4.3.1"];
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5" forKey:@"iPod Touch_4.3.2"];
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" forKey:@"iPod Touch_4.3.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_4 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8K2 Safari/6533.18.5" forKey:@"iPod Touch_4.3.4"];
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_5 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5" forKey:@"iPod Touch_4.3.4"];
// 5.0
[userAgentTable setObject:@"Mozilla/5.0 (iPod; U; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334 Safari/7534.48.3" forKey:@"iPod Touch_5.0"];
[userAgentTable setObject:@"Mozilla/5.0 (iPod; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A405 Safari/7534.48.3" forKey:@"iPod Touch_5.0.1"];
// 5.1
[userAgentTable setObject:@"Mozilla/5.0 (iPod; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3" forKey:@"iPod Touch_5.1"];
// iPad
// 4.3
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5" forKey:@"iPad_4.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5" forKey:@"iPad_4.3.1"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5" forKey:@"iPad_4.3.2"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" forKey:@"iPad_4.3.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 4_3_4 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8K2 Safari/6533.18.5" forKey:@"iPad_4.3.4"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 4_3_5 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5" forKey:@"iPad_4.3.4"];
// 5.0
[userAgentTable setObject:@"Mozilla/5.0 (iPad; U; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334 Safari/7534.48.3" forKey:@"iPad_5.0"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad; CPU OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A405 Safari/7534.48.3" forKey:@"iPad_5.0.1"];
// 5.1
[userAgentTable setObject:@"Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3" forKey:@"iPad_5.1"];
// iPhone Simulator
// 4.3
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5" forKey:@"iPhone Simulator_4.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5" forKey:@"iPhone Simulator_4.3.1"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5" forKey:@"iPhone Simulator_4.3.2"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" forKey:@"iPhone Simulator_4.3.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_4 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8K2 Safari/6533.18.5" forKey:@"iPhone Simulator_4.3.4"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_5 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5" forKey:@"iPhone Simulator_4.3.4"];
// 5.0
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334 Safari/7534.48.3" forKey:@"iPhone Simulator_5.0"];
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A405 Safari/7534.48.3" forKey:@"iPhone Simulator_5.0.1"];
// 5.1
[userAgentTable setObject:@"Mozilla/5.0 (iPhone Simulator; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3" forKey:@"iPhone Simulator_5.1"];
// iPad Simulator
// 4.3
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 4_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5" forKey:@"iPad Simulator_4.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 4_3_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5" forKey:@"iPad Simulator_4.3.1"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5" forKey:@"iPad Simulator_4.3.2"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" forKey:@"iPad Simulator_4.3.3"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 4_3_4 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8K2 Safari/6533.18.5" forKey:@"iPad Simulator_4.3.4"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 4_3_5 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5" forKey:@"iPad Simulator_4.3.4"];
// 5.0
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; U; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334 Safari/7534.48.3" forKey:@"iPad Simulator_5.0"];
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; CPU OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A405 Safari/7534.48.3" forKey:@"iPad Simulator_5.0.1"];
// 5.1
[userAgentTable setObject:@"Mozilla/5.0 (iPad Simulator; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3" forKey:@"iPad Simulator_5.1"];
});
NSString *key = [NSString stringWithFormat:@"%@_%@",[[UIDevice currentDevice] model],[[UIDevice currentDevice] systemVersion]];
NSString *userAgent = [userAgentTable objectForKey:key];
if(userAgent){
return userAgent;
}
NSString *modelName = [[UIDevice currentDevice] model];
if([modelName isEqualToString:@"iPod Touch"]){
modelName = @"iPod";
}
NSString *version = [[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] componentsJoinedByString:@"_"];
NSString *cpu = @"iPhone OS";
if([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]){
cpu = @"OS";
}
return [NSString stringWithFormat:@"Mozilla/5.0 (%@; CPU %@ %@ like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3", modelName, cpu, version];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment