Xcode (creating app for mac os). WebView does't download page - objective-c

library file (AppDelegate.h) :
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
#private
__unsafe_unretained NSWindow *window;
__unsafe_unretained WebView *wview;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet WebView *wview;
#end
Xcode did not compile my code, before i add __unsafe_unretained in private (Existing instance variable 'window' for property 'window' with assign attribute must be __unsafe_unretained).
main file (AppDelegate.m) :
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window;
#synthesize wview;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[wview mainFrame]loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: #"http://vk.com"]]];
}
- (IBAction)wview:(id)sender {
[[wview mainFrame]loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:#"http://vk.com"]]];
}
#end

Related

add property to NSApplication/NSResponder causes error on macOS app

I am trying to migrate one iOS app to macOS, the code below work!
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
}
#property (nonatomic, assign) NSInteger myProperty;
#end
RootController.h
#import <UIKit/UIKit.h>
#interface RootController : UIViewController{}
#property (strong,nonatomic) AppDelegate *appDelegate;
#end
RootController.m
appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.myProperty=5;
so I set the similar code on macOS app
AppDelegate.h
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>
#import <Cocoa/Cocoa.h>
#interface AppDelegate :NSResponder <NSApplicationDelegate>{
}
#property (nonatomic, assign) NSInteger myProperty;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize myProperty;
#end
AppController.h
#import "AppDelegate.h"
#interface AppController : NSWindowController <>{
}
#property (strong,nonatomic) AppDelegate *appDelegate;
#end
AppController.m
//..
appDelegate =(AppDelegate *)[[NSApplication sharedApplication] delegate];
appDelegate.myProperty=5;//
cause error
[AppController setMyProperty:]: unrecognized selector sent to instance 0x7f8992b4a280
your comment welcome

"missing setter or instance variable" log message occurs initializing NSWindowController with Objective-C in Xcode:

Here is an abstract I took from an app that already works in which a parent window and sheet are processed. The abstract here compiles, launches, and displays the parent window without having received the message I sent to its text field. The run produces 'missing setter or instance variable' log messages for the text field, the button, and the window itself. For some reason I have not gotten the parent window to be properly initialized, and I suspect I will have the same problem with the sheet window but I can't get past this problem to debug the rest of the app.
I believe I have omitted something fundamental in the process of connecting the window to its File's Owner, even though when looking at the connections inspector for the .xib it shows the custom class to be the ParentClass and the various connections are made. I can find no instructions in Apple's labyrinthine documentation nor here in StackOverflow to guide me through the connection process that would allow me to discover the part(s) I'm missing.
Anything you can offer will be gratefully studied.
ParentDelegate.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#class ParentClass;
#interface ParentDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow * window;
#property (weak, nonatomic) ParentClass * parentController;
#end
ParentDelegate.m
#import "ParentDelegate.h"
#implementation ParentDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { }
- (void)applicationDidBecomeActive:(NSNotification *)aNotification { }
- (void)applicationDidResignActive:(NSNotification *)aNotification { }
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{ return YES; }
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{ return NSTerminateNow; }
#end
ParentClass.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#interface ParentClass : NSWindowController {
}
#property (assign) IBOutlet NSWindow * window;
#property IBOutlet NSTextField * messageTextField;
#property IBOutlet NSButton * proceedButton;
#property (strong) NSMutableString * parentPropInfo;
- (IBAction) awakeFromNib;
- (IBAction) doProceed:(id)sender;
#end
ParentClass.m
#import "ParentClass.h"
#import "ParentDelegate.h"
#import "SheetClass.h"
#implementation ParentClass
ParentDelegate * parentDelegate;
SheetClass * sheetController;
- (IBAction)awakeFromNib {
parentDelegate = [NSApplication sharedApplication].delegate;
parentDelegate.parentController = self;
sheetController = [[SheetClass alloc] initWithWindowNibName: #"SheetClass"];
_messageTextField.stringValue = #"Click Proceed button";
}
- (IBAction)doProceed:(id)sender {
_parentPropInfo = #"Hello!".mutableCopy;
[NSApp runModalForWindow:sheetController.window];
// Sheet active now until it issues endModal, then:
_messageTextField.stringValue = sheetController.sheetPropInfo;
[NSApp endSheet: sheetController.window];
[sheetController.window orderOut:self];
}
#end
SheetClass.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#interface SheetClass : NSWindowController {
}
#property (assign) IBOutlet NSWindow * window;
#property (weak) IBOutlet NSTextField * propTextField;
#property (weak) IBOutlet NSButton * returnButton;
#property (strong) NSMutableString * sheetPropInfo;
- (IBAction)awakeFromNib;
- (IBAction)doReturn:(id)sender;
#end
SheetClass.m
#import "SheetClass.h"
#import "ParentClass.h"
#implementation SheetClass
ParentClass * parent;
- (IBAction)awakeFromNib {
parent.window = self.window.sheetParent;
_propTextField.stringValue = parent.parentPropInfo;
}
- (IBAction)doReturn:(id)sender {
_sheetPropInfo = #"Done!".mutableCopy;
[NSApp stopModal];
}
#end
Ultimately I would like to use this mini-app as a starting template for several other apps.

How change this from UIWebView to WKWebView?

I have tried with the 3 or 4 posts about this here but none use file path and I cannot get this working with WKWebView, it works with UIWebView, please can someone help me.
Yes, I am very new to this, I tried all day before posting here tonight, so easy to understand instructions would be great. Thanks.
.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *contentWebView;
#end
.m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize contentWebView;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"LockBackground" ofType:#"html"];
NSURL * fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSURLRequest * myNSURLRequest = [[NSURLRequest alloc]initWithURL:fileURL];
[contentWebView loadRequest:myNSURLRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Simply changing
#property (weak, nonatomic) IBOutlet UIWebView *contentWebView;
to
#property (weak, nonatomic) IBOutlet WKWebView *contentWebView;
should work.
If you intend to provide support for both iOS 7 and iOS 8, you'll have to declare two variables and add the following verification:
if ([WKWebView class]) {
// do new webview stuff
}
else {
// do old webview stuff
}

EXEC_BAD_ACCESS when clicking on a button in objective C

I am just learning and trying to put forth some of the knowledge with objective c and cocoa. I have a little program that has a login screen which then displays a main menu. On the main menu view, there is a button that should load a view to be able to add some data to the app. When I click on the button, it doesn't fire the IBAction but throws an error
EXC_BAD_ACCESS [StartMenuViewController performSelector:withObject:]: message sent to deallocated instance 0x6080195e9f90
I know that it is because there is an object that is not instantiated when the message is being sent, but I cannot find out why. Sorry for the code and the novice level here is the main menu view controller StartMenuViewController.m file:
#import "StartMenuViewController.h"
#import "AppDelegate.h"
#import "MasterViewController.h"
#interface StartMenuViewController ()
#end
#implementation StartMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
-(IBAction)showStrainView:(id)sender{
AppDelegate *delegate = nil;
delegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
MasterViewController *addStrainView = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
[delegate swapView:self.view toView:addStrainView.view];
}
#end
here is the StartMenuViewController.h
#import <Cocoa/Cocoa.h>
#interface StartMenuViewController : NSViewController
#property (weak) IBOutlet NSButton *showStrainViewButton;
#end
Here is the AppDelegate.h
#import <Cocoa/Cocoa.h>
#class loginView, MasterViewController,StartMenuViewController;
#interface AppDelegate : NSObject <NSApplicationDelegate>{
loginView *loginView;
MasterViewController *masterViewController;
}
#property (assign) IBOutlet NSWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (strong) loginView *loginView;
#property (strong) MasterViewController *masterViewController;
#property (strong) StartMenuViewController *starMenuViewController;
-(void)swapView:(NSView *)view1 toView:(NSView *)view2;
#end
Here is the AppDelegate.m
#import "AppDelegate.h"
#include "MasterViewController.h"
#import "ScaryBugDoc.h"
#import "Strains.h"
#import "loginView.h"
#interface AppDelegate()
#end
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#synthesize loginView;
#synthesize masterViewController;
#synthesize starMenuViewController;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// 1. Create the master View Controller
self.loginView = [[loginView alloc] initWithNibName:#"loginView" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.window.contentView addSubview:self.loginView.view];
self.loginView.view.frame = ((NSView*)self.window.contentView).bounds;
}
-(void)swapView:(NSView *)view1 toView:(NSView *)view2{
//[view1 removeFromSuperview];
[self.window.contentView addSubview:view2];
view2.frame = ((NSView*)self.window.contentView).bounds;
}
#end
If you need the loginView.h and loginView.m files I can add them as well. I would appreciate any and all help. Thanks in advance
The problem is here:
MasterViewController *addStrainView = [[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil];
[delegate swapView:self.view toView:addStrainView.view];
You don't keep a reference to the MasterViewController so it gets deallocated even though its view is kept around. Now anytime the view tries to access methods from the view controller, things fail.
You should make addStrainView a child view controller of self in the above code so the view controller sticks around as long as its view.

Call Function When WebView Changes

So, I've been looking at the documentation and Googling, but I can't figure out how to call a method when my WebView (OSX, not iOS), changes... All the examples seem to be for iOS. My code looks like this:
SNAFAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface SNAFAppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet WebView *browser;
- (IBAction)forwards:(id)sender;
#end
SNAFAppDelegate.m
#import "SNAFAppDelegate.h"
#implementation SNAFAppDelegate
#synthesize window = _window;
#synthesize browser = _b;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[_b mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://google.com"]]];
}
#end
SNAFBrowser.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#import <WebKit/WebFrame.h>
#import <WebKit/WebEditingDelegate.h>
#import <WebKit/WebKit.h>
#interface SNAFBrowser : WebView
#property (assign) IBOutlet WebView *browser;
#end
SNAFBrowser.m
#import "SNAFBrowser.h"
#implementation SNAFBrowser
#synthesize browser = _b;
- (void)webViewDidChange:(NSNotification *)notification
{
NSLog(#"Hello World");
}
#end
Then, in my MainMenu.xib file, I linked the Outlet "browser" from the AppDelegate to the WebView and set the WebView's custom class to SNAFBrowser.
Essentially, I just want to know when the webview loads another page.
I'm probably doing something ridiculously stupid, so any help is appreciated.
Implement the WebFrameLoadDelegate protocol, and see which methods are suitable to implement, for example webView:didFinishLoadForFrame:.
Example:
#interface SNAFBrowser : WebView <WebFrameLoadDelegate>
...
#end
#implementation SNAFBrowser
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
NSLog(#"Something got loaded!");
}
...
#end