XCode MacOS (not iOS) using WKWebView to open a URL in a window with:
#property (assign) IBOutlet NSView *webNSView;
#property (assign) IBOutlet WKWebView *webView;
- (void)windowDidLoad {
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.webNSView.frame];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:#"https://google.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.webNSView addSubview:webView]; }
This is working fine.
I want to send a blank url using [self blankUrl];
I have this:
-(void) blankUrl {
NSURL *nsurl=[NSURL URLWithString:#"about:blank"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
}
But I get error: "Unknown receiver 'webView'; did you mean 'WebView'?"
Changing to WebView gives error: "unrecognized selector sent to class."
Changing to self.webView will compile but doesn't load the url.
Basically, How can I update WKWebView URL?
In windowDidLoad you have a local variable called webView that you initialize, rather than the property. Then you try to use the property (uninitialized) one later.
Change the first line of windowDidLoad to...
self.webView = [[WKWebView alloc] initWithFrame:self.webNSView.frame];
...and also use self.webView in blankUrl.
Related
I made a custom segue and I want it to play sound when changing views. Unfortunately, it doesn't. Method playSound works fine when I use it in View Controller. But i don't want to export it in every View Controller i need. How can i improve my code and why segue doesn't want to play sound?
#import "CustomSegue.h"
#import AVFoundation;
#interface CustomSegue()
#property (nonatomic, strong) AVAudioPlayer *player;
#end
#implementation CustomSegue
#synthesize player;
- (void) perform
{
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[src.navigationController pushViewController:dst animated:NO];
}
completion:NULL];
[self playSound];
}
- (void) playSound
{
int r = arc4random_uniform(7) + 1;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%i", r] ofType:#"caf"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[self.player prepareToPlay];
[self.player play];
}
I bet your custom segue is released (by ARC) before the sound has a chance to play, and your audio player is released with it. The Life Cycle of a Segue is such that it is deallocated immediately after -performis executed.
What you need to do is keep the audio player around for a longer time. One way to do this is to add a (strong) reference to your AVAudioPlayer from an object that is not going to be deallocated for at least as long as the sound will play. For example, place the player in your app delegate, which sticks around as long as the app is running.
Try moving the player property and the -playsound method into your app delegate. The property should look something like:
#property (strong, nonatomic) AVAudioPlayer *player;
Then, in this segue, in place of [self playsound]; make a call to the app delegate with something like:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate playsound];
I am trying to load a UIWebview and am trying to implement the SVProgress HUD when the webview is loading and then dismissing it when its loaded.I have almost completed it however when it loads the progress hud shows for a second and then dosent show and then shows for a second and so fourth, like its blinking I'm kinda stuck, check out my code.
In my .h:
#import <UIKit/UIKit.h>
#import "SVProgressHUD.h"
#interface ViewController2 : UIViewController <UIWebViewDelegate> {
//Instantiated the timer varibale inside the interface.
NSTimer *timer1;
IBOutlet UIWebView *webView;
}
//Instantiate the webview ans NSString link.
#property(nonatomic,retain) UIWebView *webView;
#property (strong, nonatomic) NSString *link;
and in my .m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
#synthesize webView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Getting the link from the NSString called "link" and putting in the request.
NSString *link = [NSString stringWithString:self.link];
NSURL *URL = [NSURL URLWithString:link];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[webView loadRequest:request];
//Set the time for the loagind modal view.
timer1 = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
//Webview Properties
//Webview's name is web, which was instantianted in the propeties section.
webView.ScalesPageToFit = true;
webView.backgroundColor = [UIColor pomegranateColor];
}
//When the UIWebview is loading present "Fetching Homework" alert, thanks to SVProgressHUD
-(void)loading {
if(!webView.loading)
[SVProgressHUD dismiss];
else
[SVProgressHUD showWithStatus:#"Loading Homework"];
}
I think that it could just be the timer, but I'm not sure, anyhelp would be greatly appreciated.
Thanks in advance.
You should use the web view delegate to know when the page ends loading instead of using a timer to check it each 0.5 seconds. Try it this way:
Begin the SVProgressHUD when inits the page loading
[SVProgressHUD showWithStatus:#"Loading Homework"];
Then remove it when the page finish loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[SVProgressHUD dismiss];
}
Don't forget that you also have to set your webview delegate as this:
webView.delegate = self;
I'm trying to launch a URL with the UIWebView upon launch of my Mac application.
I started out with this,
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
- (void)viewDidLoad {
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
With that, I get two errors that I can't figure out.
(ViewController.h) #interface ViewController : UIViewController {
Error - Cannot find interface decleration 'UIViewController', superclass of 'ViewController'
(ViewController.m) - (void)viewDidLoad {
Error - Missing context for method declaration
I'm not really familiar with is and can't find what I'm doing wrong.
For OSX app, you don't have UIViewController, instead, you have NSWindowController and NSViewController, check Mac Developer Library.
You are using IOS functions instead of OSX.
Try this
In youy .h file
#import <WebKit/WebKit.h>
WebView *myWebView;
In your .m file
- (void)windowDidLoad
{
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[myWebView mainFrame] loadRequest:requestObj];
}
Don't forget to include "webkit.framework"
What I want is that my app should stop playing sound when it enters the background (when home button is pressed). I did this in appDelegate.m but it says use of undeclared identifier 'soundID'
- (void)applicationDidEnterBackground:(UIApplication *)application
{
AudioServicesDisposeSystemSoundID(soundID)
}
I have imported my VC into the delegate and have also exposed the soundID as a property. Where am I going wrong? Please help.
NSString *path = [[NSBundle mainBundle] pathForResource:#"something" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID);
AudioServicesPlaySystemSound(soundID);
If you want that functionality you should go to your app main plist file (probably named [your-app-name]-Info.plist) and there find a key:
Required background modes
and remove value
App plays audio
That should do the trick.
----------------EDIT:
Try this:
NSString* path = [[NSBundle mainBundle] pathForResource:#"myWavFile" ofType:#"wav" inDirectory:#"DirectoryName"];
self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
[self.player play];
Where self player is:
#property (strong, nonatomic) AVPlayer *player;
You should also:
#import <AVFoundation/AVFoundation.h>
And add this framework to your project.
This is my first iOS app, so I am probably missing something very simple. Please be kind. I have been tearing my hair out and I could really use some help.
Overview Of App
Basically, this is a single page application that just loads a UIWebView. I have an external accessory (bluetooth barcode scanner) that I connect and basically what I want to do is when the the app receives a scan, I want to call a method in my ViewController and update the UIWebView accordingly.
What Is Working
I am able to connect the scanner, load the first view, which loads the initial webpage, scan a barcode and call the method in my controller.
My Problem
I can't seem to figure out how to update the UIWebView from the method in my controller. It logs the url string to my debugger area, but never actually updates the webview. I am pretty sure I have some delegation wrong or something with my webview instance. There must be some glue code here that I am missing.
My Code HelloWorldViewController.h
#import <UIKit/UIKit.h>
#import "KScan.h"
#interface HelloWorldViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *page;
IBOutlet UILabel *myLabel;
Boolean IsFirstTime;
KScan *kscan;
}
- (void)setFirstTime;
- (void)DisplayConnectionStatus;
- (void)DisplayMessage:(char *)Message;
- (void)newBarcodeScanned:(NSString *)barcode;
- (void)loadBarcodePage:(NSString *)barcode;
#property (nonatomic, retain) KScan *kscan;
#property (nonatomic, retain) UIWebView *page;
#property (nonatomic, retain) UILabel *myLabel;
#end
My Code HelloWorldViewController.m
#import "HelloWorldViewController.h"
#import "common.h"
#implementation HelloWorldViewController
#synthesize myLabel;
#synthesize page;
#synthesize kscan;
- (void)setFirstTime
{
IsFirstTime = true;
}
- (void)viewDidLoad
{
self.kscan = [[KScan alloc] init];
[super viewDidLoad];
page.scrollView.bounces = NO;
//page.delegate = self;
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://192.168.0.187:3000"]]];
}
- (void) newBarcodeScanned:(NSString *)barcode
{
NSLog(#"%s[%#]",__FUNCTION__, barcode);
[self loadBarcodePage:barcode];
}
- (void)loadBarcodePage:(NSString *)barcode
{
NSLog(#"%s",__FUNCTION__);
NSString *url = [[NSString alloc] initWithFormat:#"http://www.google.com/%#", barcode];
NSLog(#"%#", url);
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
- (void)viewDidUnload
{
[myLabel release];
myLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)dealloc {
[page release];
[kscan release];
[myLabel release];
[super dealloc];
}
#end
Basically, I am just trying to load google.com into my page webview when scanning a barcode. My log statements are being logged with the correct URL, but this line of code doesn't work.
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
I am not getting any errors and my xCode debugging skills are not the greatest.
Any help would be greatly appreciated!
It looks like your webview is never allocated, or added to your main view. You are probably talking to a nil instance.
Unless your web view comes from a XIB file (which I doubt since it is not declared as an IBOutlet in your heder file) try adding something like this to your viewDidLoad:
self.page = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.page];