Help setting up webview

Discussion in 'Mac Programming' started by Danneman101, Jan 10, 2011.

  1. macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    Do you know of any tutorial on how to set up a webview in a mac os x - app? Or any code-samples, perhaps?
    Anything to get me from getting the damned "SIGABRT"-error I get all the time I try setting a webview-project up as described below:


    myAppDelegate.h
    Code:
    #import <Cocoa/Cocoa.h>
    #import <WebKit/WebKit.h>
    
    @interface myAppDelegate : NSObject <NSApplicationDelegate> {
    	// Declare:		WebView
    	IBOutlet WebView *webView;
    }
    @property (assign) IBOutlet NSWindow *window;
    // Set property:	WebView
    @property (nonatomic, retain) IBOutlet WebView *webView;
    @end
    

    myAppDelegate.m
    Code:
    #import "myAppDelegate.h"
    @implementation myAppDelegate
    @synthesize window;
    // Synt:	WebView
    @synthesize webView;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
    {	
    	// LOAD WEBSITE
    	NSString *urlAddress = @"http://www.iphonedevsdk.com";
    	NSURL *url = [NSURL URLWithString:urlAddress];
    	NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    	[webView loadRequest:requestObj];
    }
    @end
    

    MainMenu.xib
    - A webview is added to the window.
    - The webview is then linked in the "Referencing Outlets" to the "my App Delegate" > "webView" that was declared in the h-file above.


    Still I keep getting a "SIGABRT"-error (with no other info), so I guess Im doing something wrong...
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Run > Console. This will bring up the debugger console. Does your output contain the following?

    Code:
    *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)
    
    If it does, then you haven't linked your program to the WebKit framework. You can do that by:

    1. Right-clicking/control+clicking on the frameworks folder under your project in Xcode.
    2. Add > Existing Frameworks
    3. Select WebKit.framework
    4. Add

    2 other things....

    loadRequest: needs to be sent to webView's mainFrame, not it webView itself.

    Code:
    [[webView mainFrame] loadRequest:requestObj];
    
    And according to the Memory Management of Nib Objects chapter of the Memory Management Programming Guide (1), outlets under Mac OS X should declared, differently to iOS outlets, like this:

    Code:
    @property (assign) IBOutlet UserInterfaceElementClass *anOutlet;
    
    Note it's assign instead of nonatomic, retain.

    (1) http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/Articles/mmNibObjects.html
     
  3. macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #3
    If you forget to add the WebKit framework in Xcode, you'll get this:
    Code:
    2011-01-10 20:42:38.421 WebViewDemo[1031:a0b] An uncaught exception was raised
    2011-01-10 20:42:38.423 WebViewDemo[1031:a0b] *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)
    2011-01-10 20:42:38.426 WebViewDemo[1031:a0b] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)'
    *** Call stack at first throw:
    (
    	0   CoreFoundation                      0x00007fff860cc7b4 __exceptionPreprocess + 180
    	1   libobjc.A.dylib                     0x00007fff87f130f3 objc_exception_throw + 45
    	2   CoreFoundation                      0x00007fff860cc5d7 +[NSException raise:format:arguments:] + 103
    	3   CoreFoundation                      0x00007fff860cc564 +[NSException raise:format:] + 148
    	4   Foundation                          0x00007fff858e2c9e _decodeObjectBinary + 2427
    	5   Foundation                          0x00007fff858e3a1d -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1229
    	6   Foundation                          0x00007fff858e3f5d -[NSArray(NSArray) initWithCoder:] + 462
    	7   Foundation                          0x00007fff858e2d17 _decodeObjectBinary + 2548
    	8   Foundation                          0x00007fff858e2191 _decodeObject + 208
    	9   AppKit                              0x00007fff83a8901e -[NSView initWithCoder:] + 362
    	10  Foundation                          0x00007fff858e2d17 _decodeObjectBinary + 2548
    	11  Foundation                          0x00007fff858e2191 _decodeObject + 208
    	12  AppKit                              0x00007fff83a973e3 -[NSWindowTemplate initWithCoder:] + 3824
    	13  Foundation                          0x00007fff858e2d17 _decodeObjectBinary + 2548
    	14  Foundation                          0x00007fff858e3a1d -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1229
    	15  Foundation                          0x00007fff858e3460 -[NSSet(NSSet) initWithCoder:] + 447
    	16  Foundation                          0x00007fff858e2d17 _decodeObjectBinary + 2548
    	17  Foundation                          0x00007fff858e2191 _decodeObject + 208
    	18  AppKit                              0x00007fff83a1910e -[NSIBObjectData initWithCoder:] + 1983
    	19  Foundation                          0x00007fff858e2d17 _decodeObjectBinary + 2548
    	20  Foundation                          0x00007fff858e2191 _decodeObject + 208
    	21  AppKit                              0x00007fff83a1883d loadNib + 146
    	22  AppKit                              0x00007fff83a17d9a +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 248
    	23  AppKit                              0x00007fff83a17bd2 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 326
    	24  AppKit                              0x00007fff83a15153 NSApplicationMain + 279
    	25  WebViewDemo                         0x00000001000016a9 main + 33
    	26  WebViewDemo                         0x0000000100001680 start + 52
    )
    terminate called after throwing an instance of 'NSException'
    Program received signal:  “SIGABRT”.
    sharedlibrary apply-load-rules all
    kill
    
    The key part being that it doesn't know the WebKit class:
    2011-01-10 20:42:38.426 WebViewDemo[1031:a0b] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)'

    You said you only got SIGABRT and no other info. Are you sure you didn't get those exceptions logs and the stack trace as above?

    Add the WebKit framework in Xcode and you'd get this:

    2011-01-10 20:45:07.823 WebViewDemo[1077:a0b] -[WebView loadRequest:]: unrecognized selector sent to instance 0x10041a560

    as you were sending loadRequest to webView instead of its main frame. This should have given you a compiler warning anyway.

    Code:
    //	change this
    //	[webView loadRequest:requestObj];
    
    //	to this
    	[[webView mainFrame] loadRequest:requestObj];
    
    
    Now it works and loads the page :)

    You've still got some strange things there, like a @property and @synthesize for window , but no instance variable to go with it.

    Playing with WebView has been on my list of things to do as I learn Cocoa, so that was fun.
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    This would have come from the Cocoa application template, as is.

    It does work. It works because, for the modern Mac OS X runtime (1), Objective-C will synthesise a instance variable if one isn't declared.

    (1) http://developer.apple.com/library/...timeGuide/Articles/ocrtVersionsPlatforms.html
     
  5. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #5
    Thank you both very much - seems you posted about the same time and gave overlapping advice :)

    Yes, you're right - I totally forgot to include the framework :p Its been so long since I worked with xcode I forgot this has to be done.

    Also, thanks for the tip about loading the webview, and how to properly assign properties :)
     

Share This Page