WebView doesn't respond to my keyboard input? - xcode

I'm writing some app for mac.
Here's the problem:
If I pull out an put NSTextField and WebView on the window, and connect NSTextField with WebView's method takeStringURLFrom:, they acts correctly. I can type on everything in the WebView! But, if I load the site with loadRequest: like this:
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"blah"]]];
WebView doesn't respond to my keyboard input.
How to solve this problem?

Related

Handoff not working from native app to website

My devices:
iPad Mini (latest), iOS 8 dp5.
Macbook Air, Yosemite dp5.
I have Handoff working between the two above devices. Safari, Mail, Messages, Calendar, etc. all handoff with no problems.
I can even handoff between my website on the Air and my native app on the iPad.
What I can't do yet is go from my native app on the iPad to my website in Safari on my Air.
For the first view controller that loads in my native app, I have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserActivity *webHandoff = [[NSUserActivity alloc] initWithActivityType:#"com.myApp.iphone.staging.webbrowsing"];
webHandoff.webpageURL = [NSURL URLWithString:#"http://staging.myApp.com"];
[webHandoff becomeCurrent];
}
In my app's Info.plist file, I have this:
<key>NSUserActivityTypes</key>
<array>
<string>com.myApp.iphone.staging.webbrowsing</string>
</array>
Am I missing something or do I have something configured incorrectly?
Thanks for any help!
I made two significant changes to my code:
1) configure/destroy and set the NSUserActivity object in viewDidAppear/disappear as opposed to viewDidLoad:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSUserActivity *webHandoff = [[NSUserActivity alloc] initWithActivityType:#"com.myApp.iphone.staging.web-browsing"];
webHandoff.webpageURL = self.handoffWebpageURL;
[self setUserActivity:webHandoff];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.userActivity invalidate];
}
2) Since UIViewController is a subclass of UIResponder and UIResponders have a userActivity property, instead of calling [webHandoff becomeCurrent] I simply called [self setUserActivity:webHandoff];
Not sure why moving it out of viewDidLoad had any impact, and not sure why I need to set it to the viewController's instance of NSUserActivity, but the changes above give me a solid and reliable handoff experience throughout my entire app.

how to make NSURL point to local dir?

reading Adium code today, found an interesting usage of NSURL:
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:#"adium://%#/adium", [messageStyle.bundle bundleIdentifier]]];
[[webView mainFrame] loadHTMLString:[messageStyle baseTemplateForChat:chat] baseURL:baseURL];
I tried to log the url and got this adium://im.adium.Smooth Operator.style/adium, Then I created a blank project to see how to create such an NSURL but failed. When I sending loadHTMLString message to a webview's frame in my project, if the baseURL is nil, everything is fine, if not, I got a blank page in the view.
here is my code, the project name is webkit
NSURL *baseURL = [NSURL URLWithString:#"webkit://resource"];
//if baseURL is nil or [[NSBundle mainBundle] bundleURL], everything is fine
[[webView mainFrame] loadHTMLString:#"<html><head></head><body><div>helloworld</div></body></html>"
baseURL: baseURL];
[frameView setDocumentView:webView];
[[frameView documentView] setFrame:[frameView visibleRect]];
the question is how to make a self defined protocol instead of http://?
adium://%#/adium , first section is called protocol you can also register your protocol webkit: Take a look at How to map a custom protocol to an application on the Mac? and Launch Scripts from Webpage Links
[NSURLProtocol registerClass:[AIAdiumURLProtocol class]];
[ESWebView registerURLSchemeAsLocal:#"adium"];
I tried to find where did adium define the adium schema in the info.plist, unfortunately, there's nothing there, only some irc/xmpp protocols.
so I finally launched the debugger, and found the code above in the AIWebKitDelegate init method, anyway this is another way to register a self defined protocol~

how to make web browser in Xcode load a web page automatically

I am building a web browser in Xcode cocoa application, using the interface builder. I have it all working when it comes to typing in web address and clicking go.
but I would like for it to automatically load a web page instead of seeing white when I open my project, I looked in inspector to see if you can set a specific web address but I couldn't find anything to do this.
How about if you load the URL that you would like in the viewdidload function:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *targetURL = [NSURL fileURLWithPath:WebsitePath];
NSURLRequest *WebRequest = [NSURLRequest requestWithURL:targetURL];
[self.WebViewer loadRequest:WebRequest];
self.WebViewer.scalesPageToFit = YES;
}
Maybe you can try to load the WebView when the application finish launching,just add the URL Request in the applicationDidFinishLauching parameter,else if your WebView is on the MainView you can add a loadingScreen that appear while the webView is loading!
Lucky!

Making a button change the webview

I'm trying to make a mac app using the apple developer tools, specifically xcode. I'm basically imbedding my website into in in a sort of iTunes-esque way- side bar at right, user clicks a button on side bar and webview web page changes. Problem is I'm having trouble trying to get the button to load a specific URL into the webview.
WebView* webView = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
NSString *urlAddress = #”http://www.example.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
So you first get the url as a string, turn it into an NSURL object, and then into an NSURLRequest object and finally give that to the webview object to load. You could also do this with a webview created in Interface Builder by ommiting the first line (where the webview is being initialised) and make sure you add the webView property to the viewcontroller as an IBOutlet and connecting it to the webview in Interface Builder.
Edit: It seems I was using iPhone syntax for the webview. Loading a url on a mac webview however is still similar, so all I've changed is the class name.

How to programmatically set the URL of a WebView in an OSX Xcode project?

I've created a new Mac OSX Application in Xcode, included a WebView via Interface Builder and now I'm trying to programmatically tell it what URL to load. Here's how far I got:
Create the project
Include Webkit.framework
Include the WebView in Interface Builder
Then from there I've included what I think is necessary to get access to the WebView, including:
#import <WebKit/WebKit.h>
#synthesize webView;
// etc, I think this is all good
Where I fall over is getting access to the WebView itself and telling it what URL to load. I'm trying:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *urlAddress = #"http://www.example.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[webView mainFrame] loadRequest:requestObj];
}
But I think I'm supposed to connect some stuff in Interface Builder first?
My end goal is to create a Mac OSX Application that's essentially just a View-Based Application (which is simple to do using the iPhone SDK). From there I want to include a local htdocs folder with my own local .html files but the first step is to tell the WebView what URL to load :)
Thanks!
The code you've written above is correct, I just tested it then and successfully loaded a URL.
I'm not sure what you've declared in your header file but you should have something like this:
//usual #import(s)
#import <WebKit/WebKit.h>
#interface myAppDelegate : NSObject <NSApplicationDelegate> {
WebView *myWebView;
//other instance variables
}
#property (retain, nonatomic) IBOutlet WebView *myWebView;
//other properties and methods
#end
As #pulazzo says, you also need to connect your code with the control defined in Interface Builder.
To do this, in Interface Builder, you need to find your application delegate class, right-click (or control-click) on it and find myWebView in the list of outlets. Drag from the circle on the same line as myWebView onto your web view. I don't think my explanation is much chop so you'll probably have a lot more luck reading the Interface Builder Quick Start Guide on the Apple developer site.
In your implementation file, you need to implement the getter and setter methods for your new property, myWebView. You (probably) don't need to do any customisation to this so you can just use #synthesize:
#implementation myAppDelegate
#synthesize window;
#synthesize myWebView;
//your function etc
#end
Did you declare your webView property as an IBOutlet and connect it to your WebView in Interface Builder?
If not, then webView isn't set and your code will just silently fail to do anything.
yes, you says right! he declared like this:
IBOutlet WebView webView;
but it doesn't need necessary coding with #sinthesize,
you can just use a awakeFromNib method in your appController implementation.
-(void)awakeFromNib{
//etc etc
}

Resources