Discussion in 'Mac Programming' started by drummingsoccer, Jan 16, 2013.

  1. drummingsoccer macrumors newbie

    Jan 9, 2013
    I'm looking to set a custom user agent for a webview in my application. I read the following documentation and tried to implement the code, but the user agent remains as the default.


    Site to check user agent

    I've been trying different ways of implementing this for hours now and I feel a bit silly. It seems so simple! I'm hoping that someone can provide an explanation of exactly how to effectively change my user agent on OS X (there seems to be much more info on doing this for iOS).

    My last question regarding WebKit didn't get a single reply (although it turns out I was missing a connection with File's Owner, just a silly mistake), so I'm hoping that this one brings in at least one answer...
  2. drummingsoccer, Jan 16, 2013
    Last edited: Jan 16, 2013

    drummingsoccer thread starter macrumors newbie

    Jan 9, 2013
    The whole project? Or just trying to change the user agent? Because for changing the user agent, I REALLY have no idea what I'm doing. To me, it appears that the following code (as seen in the documentation)
    - (void)setCustomUserAgent:(NSString *)userAgentString
    simply defines the CustomUserAgent string, yet I'm not sure to do with that string once I've changed it.

    To my little knowledge of obj-C, it appears that you must modify "userAgentString" in the code above to whatever the desired user agent is, and then somehow tell the program to use this agent.

    I have no idea how to go about doing this.

    I've tried some stuff, but none of it worked at all and I would probably be ridiculed if I posted here. Plus, I have since deleted it because it was just causing problems.
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Post your code that calls setCustomUserAgent:.

    If you have code that overrides setCustomUserAgent:, then that's not what the docs are telling you.

    I don't know why you mention C#, since this is all Objective-C.

    If you don't understand the difference between overriding a method and calling a method, then you need to show us what you know and don't know. That means showing your code. If you're not willing to do that, for whatever reason, then I don't see how anyone can help you. You'll have to compare what you did with what you can find on the internetz, as examples, tutorials, or whatever. But if you can't do that effectively, then you've dug yourself a hole you can't get out of.

    We have no way of knowing what code you wrote, in which method, or in which class, unless you post that code, and tell us what class it's in.

    Without seeing your actual code, you're asking us to guess what you wrote, guess which method and class it's in, then tell you why this guessed-at code isn't working correctly. I honestly can't think of any situation where guessing at what's wrong with guessed-at code could possibly be of any use.

    As a programmer, get used to being wrong. I'm wrong dozens or even hundreds of times a day. Every time I do a build and the compiler spits out an error or a warning, it's telling me I'm wrong. Every time I find a bug in compiled code, I'm discovering how wrong I was.

    No one can possibly hope to become any good at programming except by being wrong, and then correcting what's wrong. Even the best programmers didn't start out being right. Instead, they learned from their mistakes, because the mistakes were treated as mistakes and explained.

    Getting Answers:
  4. drummingsoccer thread starter macrumors newbie

    Jan 9, 2013
    What I'm saying is that I don't really have much code. In Document.m, I created a static string:
    static NSString *newUserAgent =@"test";
    and implemented
    - (void)setCustomUserAgent:(NSString *)userAgentString {
        userAgentString = newUserAgent;
    in the same file. I know that there's something else I need to do for this to work, but I don't know what it is.

    Also, my bad for the C# typo... I made that post quickly and have been looking into C# lately too. I may be a complete newbie, but I'm not that bad... :rolleyes:
  5. gnasher729 macrumors P6


    Nov 25, 2005
    To be honest, I think you have to learn how Objective-C works first.

    Turn on all reasonable warnings in Xcode (if you don't know how to do that, learn how to use Xcode. If you don't, you're stuffed). Compile the code, and Xcode will tell you what you at least one thing that you did wrong. It's along the lines of "I have no idea what you want to do, but this code won't do it".
  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    When the documentation (which you linked to in your original post) refers you to an existing method of an existing class, it usually means you should invoke that method on an instance of that class.

    To invoke a method, you send an object a message, like this:
    [yourWebViewInstance setCustomUserAgent:@"Your user agent string here"];
    This tells the object (a WebView instance) to set its custom user agent to the provided string. That WebView instance should continue to use that string until it's told otherwise. Any other WebView instances you might make later would have to be told the same thing, if you wanted them to do the same thing.

    If you don't know where to put that line of code (i.e. in which method), or what to use for yourWebViewInstance, then you should study the fundamentals.

    You should also describe where you're coming from. You've already said where you want to go, which is writing an app with a custom web view. But if we don't know your background, experience, and what you're learning from, it's difficult to offer advice.

    If you're learning from a book or online tutorial, which one? Be specific. Title, author, and edition of book. URL of tutorial.

    Exactly where are you in the book or tutorial? Chapter and page, or specific lesson in a tutorial.

    Are you working from sample code, or did you create the program entirely from scratch? URL of sample code, or instructions guiding you (if any).

    Is this your first Objective-C program, or have you written others before? Modified any sample programs before? Which ones?

    If you're not working from any book, tutorial, or sample code, you should be.
  7. drummingsoccer thread starter macrumors newbie

    Jan 9, 2013
    Thank you VERY much, chown. Answers like that is why I turn to forums for help.

    I come from web developing. HTML and CSS obviously, as well as a bit of php and javascript. I started my adventures into programming applications by learning C. I spent a few weeks reading up online and building little programs with what I had learned (i.e. a quadratic equation solver, a simple text based game, etc.). It was just a few weeks ago that I started with Xcode and objective-C. I've been trying to get my head around the whole "object-oriented" structure, but it's just a lot to grasp and is way more confusing to me than C. I am currently working on rewriting my quadratic equation solver in obj-C so it can have a GUI and all that.

    I also followed a tutorial on how to make a very simple browser (it only had a url field and webview) and have been adding more features ever since. Adding built-in webView features such as back and forward buttons was simple, as that is just linking up the webView's outlets to buttons. I added some things such as changing the url bar and window title to the current url and title by following the documentation. By pure logic and applying what I've learned so far about obj-C, I modified the url field to add http:// to the url string if it was not already there, and I created a google search field by taking the string value of the field, changing spaces to %20, and adding the google search prefix to the query.

    I don't a have a hard-copy book that I'm working from, but I recently (yesterday) found a free guide to obj-C/xcode that might be an okay start. I may post the link when I get around to it.

    So, to summarize, I'm a well-learned well developer that is just branching into programming applications. I've made lots of command-line tools with C, but this is my first object-oriented language and I'm having a bit of trouble getting my head around all of it.
  8. drummingsoccer, Jan 17, 2013
    Last edited: Jan 18, 2013

    drummingsoccer thread starter macrumors newbie

    Jan 9, 2013

Share This Page