How to open web browser with UIWebView - objective-c

Hey guys,
On my Xcode project, I am trying to open a web browser at the click of one of my buttons, but here's what I'm really trying to do. First of all, I am opening a web browser using this example code:
NSURL *fanPageURL = [NSURL URLWithString:#"fb://profile/210227459693"];
if (![[UIApplication sharedApplication] openURL: fanPageURL]) {
NSURL *webURL = [NSURL URLWithString:#"http://www.facebook.com/pages/Blackout-Labs/210227459693"];
[[UIApplication sharedApplication] openURL: webURL];
}
[super viewDidLoad];
Now this code works properly, but the problem is that it's closing my application and opens the link in the Safari application, which is not what I want. I went the other way around by creating another view (after click the button), then inserted a UIWebView using Interface Builder, but it still didn't work. So I was hoping someone can help me out how to open this link within my app instead of closing my app and opening the link in the Safari app, thanks

Look at SVWebViewController. It's a ready made UIViewController subclass with a UIWebView in it and all you need to do to show it is
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:#"http://google.com"];
[self.navigationController pushViewController:webViewController animated:YES];
There is also a modal version, see the Usage Section.

You'll need to create a UIViewController subclass that has UIWebView as a subview, and show that when the button in tapped.

Related

Opening a "Welcome" window from main application window

I am working on a visual editor application for OSx
Currently, the application opens my MainMenu.xib.
I would like to present user when opening the application with a Welcome window, listing the previous opened projects, and a button to create a new project (actually linking to my App Delegate menuNewProject method)
What is the best approach to open this "Welcome" xib instead of MainMenu xib ?
Make my current app delegate open the Welcome xib like this ?
Welcome *welcome = [[Welcome alloc] initWithWindowNibName:#"Welcome"];
and then hide my MainMenu window ?
Or make the starting point of my application Welcome xib and not MainMenu, and open my visual editor from there ?
Thank you for your help.
As per my understanding you want to open "Welcome" Screen instead of "MainMenu"...
If you set launch screen in info.plist file, Welcome screen will be opened instead of MainMenu
Set this field in info.plist file :-
Launch screen interface file base name = Welcome
Hopefully it works for you.. Thanks
OK, I think I got it:
At the end of
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
I added:
[self.window orderOut:nil]; // hide application window
Welcome *welcome = [[Welcome alloc] initWithWindowNibName:#"Welcome"];
[welcome.window makeKeyAndOrderFront:self];
[[NSApplication sharedApplication] runModalForWindow:welcome.window]; // display splash screen
In my Welcome.m file, I added a close button on the splash window :
-(void)windowDidLoad
{
[super windowDidLoad];
NSButton* closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:NSTitledWindowMask];
[closeButton setFrameOrigin:NSMakePoint(0, 410)];
NSView* contentView = self.window.contentView;
[contentView addSubview:closeButton];
[closeButton setTarget:[AppDelegate appDelegate]];
[closeButton setAction:#selector(closeModal:)];
}
And in AppDelegate.m, I added a closeModal method which is in charge of closing my welcome screen and display the application window:
-(void) closeModal:(id)sender
{
[[NSApplication sharedApplication] stopModal];
[self.window orderFront:nil];
}
Seems to work, maybe I should add something to discard my Welcome screen from memory too.

Objective-c Opening video in webview performs viewdidload in rootViewcontroller

I have a strange problem and I have no idea how to solve it. I have a root viewcontroller. Via button you can switch to the child viewcontroller. In the child view controller I open a link like this as a request to the webview:
myURL = [NSURL URLWithString:#"http://m.imdb.com/video/imdb/vi2284695065/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
the video on the page is autoplaying and switching to fullscreen mode. In exactly this moment I get a log message which indicates that the viewdidload method on my rootviewcontroller is called. Which eventually makes my code not working anymore. But why? Its not the viewdidload of the current viewcontroller its the one of the rootviewcontroller?! I donĀ“t know what kind of code I should provide actually...
Edit
I switch to viewcontroller like this:
UIViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:#"child"];
[self presentViewController:NVC animated:NO completion:nil];
The rootviewcontroller does not get a received memory warning.

UIDocumentInteractionController: "Open In" visible on iPhone but not on iPad - why?

For testing purposes I wrote two apps:
First one plays an MP3 file using UIDocumentInteractionController
Second one does nothing but registers for the file type "public.mp3"
If I deploy the apps to the iPhone Simulator, my MP3 player app shows a button on top "Open in 'MP3Test'". If I deploy to the iPad Simulator however, there is no button and no "Open In" menu either.
This has been tested with iOS5.
Can somebody explain if this is a bug or a feature and what the reason is behind it?
Depends upon where you are presenting it from.
If you are presenting it from somewhere around the middle of the screen or below, just present from the frame of the object that you are presenting from.
if that is on the navigation bar, try this:
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:#"License" ofType:#"pdf"];
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]];
controller.delegate = self;
CGRect navRect = self.navigationController.navigationBar.frame;
navRect.size = CGSizeMake(1500.0f, 40.0f);
[controller presentOptionsMenuFromRect:navRect inView:self.view animated:YES];
The iPad has an affinity for popovers (see UIPopover), why it presents UIActionSheets in them. Facing a similar issue that you had, I had my UIDocumentInteractionController present itself from an UIBarButtonItem (resulting in a UIPopover presentation), rather than from the view itself (something that worked just fine on the iPhone):
Save a reference to the action button (I have mine in my navigation bar).
Use PresentOpenInMenu using the action button reference, rather than the View reference, resulting in a UIPopover-presentation.
Please note that the change does not effect the iPhone app - it behaves likes before, i.e. opens the OpenInMenu from the bottom of the screen just as it would, if you'd used the View reference to present it.
On iPad UIDocumentInteractionController appearing like Pop Up Try something like this
-(void)shareClick:(UIButton*)sender {
/*some code*/
CGRect rectFor appearing = [sender.superview convertRect:sender.frame toView:self.view];
[interactionController presentOptionsMenuFromRect:rect inView:self.view animated:YES];
}

Calling UIWebView from IBAction ButtonPressed

This is probably a simple question...
I have a UIView with a button that corresponds to a URL address http:// etc. I want to be able to click this web address button and have it load a UIWebView on a separate UIViewController and XIB that shows the website, allowing me to then hit a back button and go back to my first view that has the original button. Basically I want to be able to load the webpage but have it operate within the App rather than start up Safari. Is this possible?
So far I have build a dedicated XIB and ViewController for my webpage for which the viewDidLoad method looks like this:
- (void)viewDidLoad{
[super viewDidLoad];
NSString *urlAddress = pushURL; //pushURL is passed as parameter
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[addressBar setText:urlAddress];
}
Question is... how do I call the UIWebView from my button (which is in the first class/parent viewController)? All my other viewControllers in my app are using UITableView so it's a simple DidSelectRowAtIndex.
Thanks.
It sounds like you want to be using a UINavigationController. Then when the button is tapped just push the UIViewController with the web view to the UINavigationController.

How do I add a UIWebView to my iOS app?

I have an app at the moment that when a button is pushed on the first screen does some work and makes a URL, and then does
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:currentURL]];
which launches Safari with my URL. I want instead to have a webview launch from here so the user can do some custom things with it. I am still having a hell of a time understanding MVC in iOS and need help. My project is minimal and consists of an AppDelegate.h/m and a ViewController.h/m, the.m of the view controller is where the function that does this Safari launch lives.
Can anyone help me understand how to do what I'm trying to d?
Thanks...
The simplest way is just to add a UIWebView when the button gets pressed. Add this method to your ViewController.m and have this be performed when the button gets pressed.
Programmatically:
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
[self.view addSubview:webView];
[webView release];
}
Using Interface Builder:
1) Add a UIWebView object to your interface.
2) Set the "hidden" property to checked (in the "Attributes Inspector" window in Interface Builder). You'll keep it hidden until you want to show it.
3) Add the following code to your ViewController.h, below the other #property lines:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
4) Add the following line below the #synthesize in your ViewController.m
#synthesize webView;
And add [webView release]; in the dealloc method.
5) Go back into IB and click on File's Owner, and connect the webView outlet to the webView you created.
6) Add the following method instead of the method I showed above (for the programmatic example):
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
self.webView.hidden = NO;
}
You could set up an IBAction method to respond to the button press, but it sounds like you already have the button press working, so I wouldn't bother with that now.
As far as adding a button above the web view, you can either subclass web view and add the button there, or just have a separate button you define in your nib or programmatically and have that hide the webView to "get rid of it".
For adding UIWebView using Swift to your app just drag the WebView to your story board and then using an assistant editor connect WebView to ViewController.swift
And then Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.
let url = NSURL (string: "https://www.simplifiedios.net");
let request = NSURLRequest(URL: url!);
webView.loadRequest(request);
As simple as that only 3 lines of codes :)
Ref: UIWebView Example
Since this is the top result on Google, if you can use WKWebView instead of UIWebView, you should.
import WebKit
let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)