Change PhoneGap / Cordova User-Agent for AJAX

前端 未结 2 1187
鱼传尺愫
鱼传尺愫 2020-12-09 06:21

I\'m using phonegap / cordova for developing an Android app.

The app has to read files on a remote server. Therefore I\'m using AJAX (jQuery). The problem is: The re

相关标签:
2条回答
  • 2020-12-09 06:42

    put this in your onCreate method: (after super.init();)

    super.appView.getSettings().setUserAgentString("Your own agent string");
    

    this worked for me.

    See for more settings: http://developer.android.com/reference/android/webkit/WebSettings.html

    0 讨论(0)
  • 2020-12-09 07:00

    If you're looking for a more powerful http client for cordova that allows changing the user agent, or any header in that matter, try out https://github.com/aporat/cordova-plugin-fetch. it wraps around well tested native networking libraries (AFNetworking 2 on ios, and OKHttp on android).

    it also follows the window.fetch, so you could use cordovaFetch on the simulators and devices, while testing on the browser with fetch.js.

    Just install the plugin with

    cordova plugin add https://github.com/aporat/cordova-plugin-fetch.git

    and include the user agent header in any request you made.

    cordovaFetch('/users.json', {
      method : 'GET',
      headers: {
       'User-Agent': 'your user agent'
     },
    })
    
    0 讨论(0)
提交回复
热议问题