1

I have a mobile website built using jQuery Mobile. I am currently designing an iOS native app with a single webView to display this mobile website.

PROBLEM:

If the jQuery mobile webpage is viewed in a normal mobile browser, I want all to function normally. BUT if the webpage is viewed using the webView in the iPhone app (not Safari.app) I am designing, I want to include an additional line of code on the webpage to pull in some custom JS.

Here's what I want to be added to the page ONLY IF viewed inside of my iPhone app's webView:

<script type="text/javascript" src="http://www.example.com/mobile/js/custom-jqm-defaults.js"></script>

This code needs to be pulled in (according to the jQuery mobile docs) before jQuery Mobile is loaded. Is this possible? And if so, could you point me in the right direction and give me some code?

EDIT

I found this link whereby Facebook detects if they are being viewed WITHIN an app. Is there another way to do this obviously NOT using the FB API? Here is their code:

if (FB.UA.nativeApp()) {
  // Hide your credits stuff
}
Community
  • 1
  • 1
adamdehaven
  • 5,890
  • 10
  • 61
  • 84
  • Just like I said, the native facebook app also has a custom user agent, which is most likely how this function detects whether it is the native app or not. Theirs looks something like [this](http://mpulp.mobi/2012/01/funky-user-agent-on-facebook-iphone-app/). – Dima Jul 24 '12 at 19:02

1 Answers1

1

You can change the user agent in your NSURLRequest, and then check for that agent on your server.

Changing it app-side is easy.

Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
  • I'm not sure what you mean? Everyone running the app will be on an iPhone, but an iPhone may also potentially pull up the mobile site in Safari.app – adamdehaven Jul 24 '12 at 18:45
  • When a client requests a webpage, the client's user-agent is in the request header (OS, browser, etc). Description [here](http://en.wikipedia.org/wiki/User_agent). Example of your user agent [here](http://whatsmyuseragent.com/). You just need to change the user agent in the app request to something custom and check for that user agent it with javascript in your webapp. There's tons of example code out there for that part: [example](http://www.quirksmode.org/js/detect.html). – Dima Jul 24 '12 at 18:48
  • I understand what a user agent is. Unless there is a SPECIFIC user agent that is shown ONLY when an iPhone views a page WITHIN a webView that is NOT Safari.app, this is no help. Does that exist? – adamdehaven Jul 24 '12 at 18:52
  • You can change it to whatever string you want, and no browser (including safari) will have that user agent (unless somebody happened to select the same random string as you and hit your page with it for some reason), so you can check for it. – Dima Jul 24 '12 at 18:58
  • Would I just basically copy and paste the code from that page into my `viewDidLoad` method? And what do I put in place of `autorelease` as this is not supported with ARC? – adamdehaven Jul 24 '12 at 19:01
  • I found this code which is supported in iOS 5.1: http://stackoverflow.com/questions/8487581/uiwebview-ios5-changing-user-agent So I would just detect the user agent, and if it matches, then write that script to the page? – adamdehaven Jul 24 '12 at 19:04
  • Just get rid of the autorelease (and the 2 brackets associated with it), completely, nothing need in place of it. And yep, just change the userAgent header field in your request to some string that you make up (something no other type of client is likely to have). – Dima Jul 24 '12 at 19:05
  • The difference between the example I linked to and that one, is that one sets the custom agent globally within the app, so that every request will have that agent without having to set it. If you're okay with that, then it's the way to go. – Dima Jul 24 '12 at 19:08
  • Is there a downside to having every request set with the specified agent? – adamdehaven Jul 24 '12 at 19:16
  • There is if you are planning on accessing other websites that might have their own checks/optimizations for different browsers/systems. Some might not know how to display the content to you, some might flat out reject the request (if the developer wants to require certain browser versions). If it's just your site, it doesn't matter. – Dima Jul 24 '12 at 19:18
  • To add: it looks like the facebook app user agent actually takes the default user agent (easy to do), with the additional app info (distinguishing it to be from the facebook app) appended to it. You can probably do that too, it's just more work. – Dima Jul 24 '12 at 19:21
  • One more question: Is there a way to set the User Agent back to normal when you're done with it besides "writing it in" again? In other words, is there some sort of function like "restoreUserAgentToDefault" or something that would do that? – adamdehaven Jul 24 '12 at 19:24
  • I think you can just clear the user defaults key. by doing `[myUserDefaultsObject removeObjectForKey@"UserAgent"];` – Dima Jul 24 '12 at 19:24