iOSで通信する際のUser-Agentを組み立てる
こういうフォーマットのUAがつくりたい!
<アプリ名>/
例)
MyApp/1.0 (iPhone5,2; 6.0; iOS; ja)
//format: MyApp/1.0 (iPhone5,2; 6.0; iOS; ja) - (NSString*)buildUserAgent { // モデル名取得 NSString *modelname = [self platformName]; // osのバージョン取得 float iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; // 言語設定取得 NSArray *langs = [NSLocale preferredLanguages]; //取得した配列から先頭の文字列を取得(先頭が現在の設定言語) NSString *currentLanguage = [langs objectAtIndex:0]; NSString* userAgent = [NSString stringWithFormat:@"MyApp/1.0 (%@; %0.2f; iOS; %@)", modelname, iOSVersion, currentLanguage]; return userAgent; } // デバイスの情報を返す - (NSString *)platformName { size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); NSString *platformName = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; free(machine); return platformName; }
ちょっと決め打ち多いね^^;
あとはこの文字列をNSURLConnectionなりAFHTTPClientなりにセットしよう!