Skip to content

Commit 1386cb7

Browse files
Add a better way to set / override the User Agent #4
1 parent 51cda32 commit 1386cb7

7 files changed

Lines changed: 94 additions & 57 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,22 @@ export function webViewLoaded(args: observable.EventData) {
8080
}
8181
```
8282

83+
### `setUserAgent`
84+
You can set this as a header, but it seems to work better setting it in a different way,
85+
so use this function if you want to override the user agent in your webview.
86+
87+
Note that on iOS this will override the user agent header in all of your webviews.
88+
89+
```typescript
90+
import { WebViewUtils } from 'nativescript-webview-utils';
91+
import { WebView } from 'tns-core-modules/ui/web-view';
92+
import * as observable from 'tns-core-modules/data/observable';
93+
94+
export function webViewForUserAgentLoaded(args: observable.EventData) {
95+
const wv: WebView = <WebView>args.object;
96+
WebViewUtils.setUserAgent(wv, "My Super Duper User-Agent!");
97+
}
98+
```
99+
83100
## Credits
84101
Quite some code was borrowed from [this repo](https://github.com/okmttdhr/nativescript-webview-custom-header).

demo/app/main-page.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ export function webViewLoaded(args: observable.EventData) {
2323
headers.set("Foo", "Bar :P");
2424
headers.set("X-Custom-Header", "Set at " + new Date().toTimeString());
2525
WebViewUtils.addHeaders(wv, headers);
26+
}
27+
28+
export function webViewForUserAgentLoaded(args: observable.EventData) {
29+
const wv: WebView = <WebView>args.object;
30+
WebViewUtils.setUserAgent(wv, "My Super Duper User-Agent!");
2631
}

demo/app/main-page.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<Label text="And now with custom headers:" class="t-20 text-center c-black" textWrap="true"/>
99
<WebView id="webviewWithCustomHeaders" loaded="webViewLoaded" height="400" src="https://httpbin.org/headers"/>
1010

11+
<Label text="Set custom User Agent (on iOS this affects ALL webviews of your app)" class="t-20 text-center c-black" textWrap="true"/>
12+
<WebView id="webViewForUserAgent" loaded="webViewForUserAgentLoaded" height="400" src="https://httpbin.org/headers"/>
13+
1114
</StackLayout>
1215
</ScrollView>
1316
</Page>

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { WebView } from "tns-core-modules/ui/web-view";
22

33
export const WebViewUtils: {
44
addHeaders: (wv: WebView, headers: Map<string, string>) => void;
5+
setUserAgent: (wv: WebView, userAgent: string) => void;
56
};

src/package.json

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
{
2-
"name": "nativescript-webview-utils",
3-
"version": "1.0.0",
4-
"description": "Add custom headers to a NativeScript WebView. Perhaps more utils later.",
5-
"main": "webview-utils",
6-
"typings": "index.d.ts",
7-
"nativescript": {
8-
"platforms": {
9-
"android": "3.0.0",
10-
"ios": "3.0.0"
11-
}
12-
},
13-
"scripts": {
14-
"tsc": "tsc -skipLibCheck",
15-
"build": "npm i && tsc",
16-
"postclone": "npm i && node scripts/postclone.js && cd ../demo && npm i && cd ../src && npm run plugin.link",
17-
"test.android": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build android && tns test android --justlaunch",
18-
"test.ios": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build ios && tns test ios --justlaunch",
19-
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\"",
20-
"plugin.link": "npm link && cd ../demo && npm link nativescript-webview-utils && cd ../src",
21-
"plugin.tscwatch": "npm run tsc -- -w",
22-
"demo.ios": "npm i && npm run tsc && cd ../demo && tns run ios --syncAllFiles",
23-
"demo.android": "npm i && npm run tsc && cd ../demo && tns run android --syncAllFiles",
24-
"demo.reset": "cd ../demo && rimraf platforms",
25-
"plugin.prepare": "npm run tsc && cd ../demo && tns plugin remove nativescript-webview-utils && tns plugin add ../src",
26-
"clean": "cd ../demo && rimraf hooks node_modules platforms && cd ../src && rimraf node_modules && npm run plugin.link",
27-
"ci.tslint": "npm i && tslint '**/*.ts' --config '../tslint.json' --exclude '**/node_modules/**'"
28-
},
29-
"keywords": [
30-
"ecosystem:NativeScript",
31-
"NativeScript",
32-
"JavaScript",
33-
"Android",
34-
"iOS",
35-
"WebView",
36-
"Headers",
37-
"WebView Headers"
38-
],
39-
"author": {
40-
"name": "Eddy Verbruggen",
41-
"email": "eddyverbruggen@yourdomain.com"
42-
},
43-
"bugs": {
44-
"url": "https://github.com/EddyVerbruggen/nativescript-webview-utils/issues"
45-
},
46-
"license": "Apache-2.0",
47-
"homepage": "https://github.com/EddyVerbruggen/nativescript-webview-utils",
48-
"readmeFilename": "README.md",
49-
"devDependencies": {
50-
"tns-core-modules": "^3.3.0",
51-
"tns-platform-declarations": "^3.3.0",
52-
"typescript": "~2.4.0",
53-
"prompt": "^1.0.0",
54-
"rimraf": "^2.5.0",
55-
"tslint": "^5.0.0"
56-
},
57-
"dependencies": {},
58-
"bootstrapper": "nativescript-plugin-seed"
2+
"name": "nativescript-webview-utils",
3+
"version": "1.1.0",
4+
"description": "Add custom headers to a NativeScript WebView. Perhaps more utils later.",
5+
"main": "webview-utils",
6+
"typings": "index.d.ts",
7+
"nativescript": {
8+
"platforms": {
9+
"android": "3.0.0",
10+
"ios": "3.0.0"
11+
}
12+
},
13+
"scripts": {
14+
"tsc": "tsc -skipLibCheck",
15+
"build": "npm i && tsc",
16+
"postclone": "npm i && node scripts/postclone.js && cd ../demo && npm i && cd ../src && npm run plugin.link",
17+
"test.android": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build android && tns test android --justlaunch",
18+
"test.ios": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build ios && tns test ios --justlaunch",
19+
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\"",
20+
"plugin.link": "npm link && cd ../demo && npm link nativescript-webview-utils && cd ../src",
21+
"plugin.tscwatch": "npm run tsc -- -w",
22+
"demo.ios": "npm i && npm run tsc && cd ../demo && tns run ios",
23+
"demo.android": "npm i && npm run tsc && cd ../demo && tns run android --syncAllFiles",
24+
"demo.reset": "cd ../demo && rimraf platforms",
25+
"plugin.prepare": "npm run tsc && cd ../demo && tns plugin remove nativescript-webview-utils && tns plugin add ../src",
26+
"clean": "cd ../demo && rimraf hooks node_modules platforms && cd ../src && rimraf node_modules && npm run plugin.link",
27+
"ci.tslint": "npm i && tslint '**/*.ts' --config '../tslint.json' --exclude '**/node_modules/**'"
28+
},
29+
"keywords": [
30+
"ecosystem:NativeScript",
31+
"NativeScript",
32+
"JavaScript",
33+
"Android",
34+
"iOS",
35+
"WebView",
36+
"Headers",
37+
"WebView Headers"
38+
],
39+
"author": {
40+
"name": "Eddy Verbruggen",
41+
"email": "eddyverbruggen@yourdomain.com"
42+
},
43+
"bugs": {
44+
"url": "https://github.com/EddyVerbruggen/nativescript-webview-utils/issues"
45+
},
46+
"license": "Apache-2.0",
47+
"homepage": "https://github.com/EddyVerbruggen/nativescript-webview-utils",
48+
"readmeFilename": "README.md",
49+
"devDependencies": {
50+
"tns-core-modules": "^3.3.0",
51+
"tns-platform-declarations": "^3.3.0",
52+
"typescript": "~2.4.0",
53+
"prompt": "^1.0.0",
54+
"rimraf": "^2.5.0",
55+
"tslint": "^5.0.0"
56+
},
57+
"dependencies": {},
58+
"bootstrapper": "nativescript-plugin-seed"
5959
}

src/webview-utils.android.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export class WebViewUtils extends android.webkit.WebViewClient {
99
private _view: any;
1010
private _origClient: any; // WebViewClient
1111

12+
public static setUserAgent(wv: WebView, userAgent: string) {
13+
wv.android.getSettings().setUserAgentString(userAgent);
14+
}
15+
1216
public static addHeaders(wv: WebView, headers: Map<string, string>) {
1317
WebViewUtils.headers = headers;
1418
// Wrap this in a timeout for Android, otherwise webview.android might not be initialized

src/webview-utils.ios.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export class WebViewUtils extends NSObject implements UIWebViewDelegate {
1010
private _owner: WeakRef<WebView>;
1111
private _originalDelegate: any; // UIWebViewDelegateImpl
1212

13+
public static setUserAgent(wv: WebView, userAgent: string) {
14+
// note that overrides the useragent for ALL webviews for the app, but that's prolly not a problem
15+
NSUserDefaults.standardUserDefaults.registerDefaults(
16+
NSDictionary.dictionaryWithObjectForKey(userAgent, "UserAgent"));
17+
}
18+
1319
public static addHeaders(wv: WebView, headers: Map<string, string>) {
1420
(<any>wv)._delegate = WebViewUtils.initWithOwner(new WeakRef(wv));
1521
WebViewUtils.headers = headers;
@@ -22,6 +28,7 @@ export class WebViewUtils extends NSObject implements UIWebViewDelegate {
2228
return delegate;
2329
}
2430

31+
// TODO this is prolly different for WKWebView
2532
// You can customize your http headers here.
2633
public webViewShouldStartLoadWithRequestNavigationType(webView: UIWebView, request: NSURLRequest, navigationType: number) {
2734
const urlString: string = request.URL.absoluteString;

0 commit comments

Comments
 (0)