Skip to content

Commit bb9c14a

Browse files
committed
给UIWebView添加与api相同的useragent
1 parent 0694ede commit bb9c14a

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

Coding_iOS/AppDelegate.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "IntroductionViewController.h"
2727

2828
#import "Tweet.h"
29+
#import "sys/utsname.h"
2930

3031
@implementation AppDelegate
3132

@@ -49,6 +50,16 @@ - (void)registerPush{
4950
}
5051
}
5152

53+
#pragma mark UserAgent
54+
- (void)registerUserAgent{
55+
struct utsname systemInfo;
56+
uname(&systemInfo);
57+
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
58+
NSString *userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey) ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], deviceString, [[UIDevice currentDevice] systemVersion], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0f)];
59+
NSDictionary *dictionary = @{@"UserAgent" : userAgent};//User-Agent
60+
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
61+
}
62+
5263

5364
#pragma lifeCycle
5465
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
@@ -67,6 +78,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
6778
//设置导航条样式
6879
[self customizeInterface];
6980
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
81+
82+
//UIWebView 的 User-Agent
83+
[self registerUserAgent];
7084

7185
if ([Login isLogin]) {
7286
[self setupTabViewController];
@@ -158,7 +172,7 @@ - (void)applicationWillTerminate:(UIApplication *)application
158172
// Saves changes in the application's managed object context before the application terminates.
159173
[self saveContext];
160174
}
161-
#pragma mark - Umeng Message
175+
#pragma mark - XGPush Message
162176
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
163177
{
164178
NSString * deviceTokenStr = [XGPush registerDevice:deviceToken];

0 commit comments

Comments
 (0)