Reputation: 874
Objective is to identify website being opened in
Upvotes: 0
Views: 2074
Reputation: 2465
iPhone
Change the UserAgent in the App
// Modify the user-agent
NSString* suffixUA = @"AppName";
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* defaultUA = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString* finalUA = [defaultUA stringByAppendingString:suffixUA];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:finalUA, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
Now you could check it via Javascript:
function isIOS() {
return /iPhone|iPad|iPod/i.test(navigator.userAgent);
}
function isIOSInApp() {
return isIOS() && /AppName/i.test(navigator.userAgent);
}
For Android:
Activity onCreate
this.webView.getSettings().setUserAgentString(
this.webView.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
);
Values.XML
<string name="user_agent_suffix">AppName/1.0</string>
Now you could check it via Javascript:
function isAndroid() {
return /Android/i.test(navigator.userAgent);
}
function() isNativeApp {
return isAndroid() && /AppName\/[0-9\.]+$/.test(navigator.userAgent);
}
Upvotes: 2
Reputation: 54
You can use mdetect.js file provided by mobileesp. Check below link : http://www.hand-interactive.com/detect/mobileesp_demo_javascript.htm
Upvotes: 1