Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7406] iOS: Set a user-agent for a webview

GitHub Issuen/a
TypeNew Feature
PriorityLow
StatusClosed
ResolutionFixed
Resolution Date2017-02-08T06:10:10.000+0000
Affected Version/sRelease 1.8.0.1, Release 2.1.3, Release 3.0.0
Fix Version/sRelease 6.1.0
ComponentsiOS
Labelsn/a
ReporterMauro Parra-Miranda
AssigneeVijay Singh
Created2012-01-25T15:35:31.000+0000
Updated2017-02-08T21:50:31.000+0000

Description

PROBLEM DESCRIPTION

Some people are interested in being able to set the user-agent for the webview, just like the way we do with the HTTPClient. The usage will be similar to the HTTPClient's modification of the user-agent, but will allow more developers to connect webviews to special content, like twitter that recently doesn't allow you to create a webview of a timeline from our implementation (looks like they identify the agent as mobile, but since doesn't know which one is, it just breaks).

MORE INFO

I have seen this request often in the community.

Comments

  1. Sindre Sorhus 2012-08-11

    Would be very useful.
  2. Michael Gangolf 2014-02-07

    Need to use this on iOS too. Perhaps this could help to fix this issue: http://stackoverflow.com/questions/8487581/uiwebview-ios5-changing-user-agent I'm testing the module from the marketplace at the moment but a native version would be nice and since its available on android it would close this gap.
  3. Ingo Muschenetz 2014-03-25

    It seems we can't find a way to do this that is exposed in iOS (changing NSUserDefaults isn't recommended). Has anyone been able to locate native functionality for doing so?
  4. Hans Knöchel 2016-09-14

    So here is what I would suggest: We could set the webview user-agent as part of the Ti.setUserAgent() method. The method is global, so it's also used for HTTP-requests and geolocation-requests. Setting it per webview would create the major problem that different webviews could have different user-agents, which is not possible, since the only way to set the user-agent in an iOS-webview is by changing the defaults for that. Let me know your thoughts on that.
  5. Ingo Muschenetz 2016-09-14

    The only question I have is that perhaps you would need to set it differently for a webview than an HTTP request, since you might need to emulate a browser. I would be fine if it was the same for all webviews (so not per-webview, but webview is different than HTTP request).
  6. Hans Knöchel 2016-09-14

    Sure, it would just the same method in which the configuration per API is made (basically around [here](https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TopTiModule.m#L49). Will propose something in the next sprint.
  7. Vijay Singh 2017-01-12

    PR: https://github.com/appcelerator/titanium_mobile/pull/8746 I have created the property in TiUIWebView, to make parity with android. We can change the user agent with creation of new webview as in example code. In doc we can mention that this property is not per-webview (different from android). [~hansknoechel] Let me know your thought.
       var win = Ti.UI.createWindow({
           backgroundColor: '#fff'
       });
        
       var btn1 = Ti.UI.createButton({
       	left: 20,
       	top: 30,
           title: 'Load webview 1'
       });
        
       var btn2 = Ti.UI.createButton({
       	top: 30,
       	right: 20,
           title: 'Load webview 2'
       });
        
       btn1.addEventListener('click', function() {
       var webview1 = Ti.UI.createWebView({
       	top: 100,
           left: 0,
           width: Ti.Platform.displayCaps.platformWidth / 2,
           url : 'https://developer.apple.com/library/',
           userAgent : 'testUserAgent'
       });
         
       webview1.addEventListener('load', function(e) {
           Ti.API.warn('webview 1: load - ' + e.url);
           Ti.API.info('webview 1: User-Agent - ' + webview1.evalJS('navigator.userAgent'));
       });
       
       win.add(webview1);
        
       }); 
        
       btn2.addEventListener('click', function() {
           var webview2 = Titanium.UI.createWebView({
           	top: 100,
           	left: Ti.Platform.displayCaps.platformWidth / 2,
           	width: Ti.Platform.displayCaps.platformWidth / 2,
               url : 'https://developer.apple.com/app-store/best-of-2016/',
           });
            
           webview2.setUserAgent('testvvs');
            
           webview2.addEventListener('load', function(e) {
               Ti.API.warn('webview 2: load - ' + e.url);
               Ti.API.info('webview 2: User-Agent - ' + webview2.evalJS('navigator.userAgent'));
           });
           win.add(webview2);
       });
        
       win.add(btn2);
       win.add(btn1);
       win.open();
       
  8. Eric Wieber 2017-02-08

    FR approved by [~htbryant], verified in SDK build 6.1.0.v20170206114448. Closing as fixed.

JSON Source