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)
Related
I am new to IOS, I am trying to create an app where I need to display the webview of any site along with slide out menus . To achieve this I have tried using swrevealviewcontroller to get the slide out menu working and use the uiwebview on the controller page but now I need to pass url to webview from the slide out menu working with single uiwebview.
How can I achieve this , I have tried using this link
Pass a UIWebView request using prepareForSegue
but I am stuck at one point , I am getting error as
Urlstr does not belong to the class , I have checked it
it will be great if anyone could guide me with the complete step
Add an NSString property to the ViewController you are presenting like this:
#property (nonatomic, strong) NSString *webviewURLString;
Then when you present assign the ViewController's webviewURLString property with something like this:
MyViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
// THIS IS WHERE YOU ASSIGN IT
myVC.webviewURLString = #"<your url goes here>";
[self.navigationController pushViewController:myVC animated:YES];
Then when in MyViewController's viewDidLoad method you can check if webviewURLString has a value and if it does, start loading the web view with the url from webviewURLString.
I have a cocoa WebView inside of an NSSplitPane as a subview of one of the split pane's Custom Views. This serves as a preview of some HTML content. To smooth the transition when updating the preview
I make an NSImageView from the WebView
Replace the WebView with the NSImageView
Load the new html into the WebView
Replace the NSImageView with the updated WebView when the html has finished loading
This is the gist of the code is:
From the header
NSImageView *previewImageView;
NSString *content;
#property (strong) IBOutlet NSView *previewContainer;
#property (strong) IBOutlet WebView *previewWebView;
From the class
- (void)updatePreview
{
previewImageView = [self imageViewFromWebView:previewWebView];
[[previewContainer animator] replaceSubview:previewWebView
with:previewImageView];
[[previewWebView mainFrame] loadHTMLString:content baseURL:nil];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[[previewContainer animator] replaceSubview:previewImageView
with:previewWebView];
}
This code does not work correctly if the WebView is defined in the xib file with the referencing outlet set to the previewWebView and the frame load delegate set. The web view is initially shown correctly, gets swapped for the image view ok, but when swapped back does not get displayed.
If I instead define the WebView in code
// inside of viewDidAppear
NSRect frame = [previewContainer frame];
NSRect webViewFrame = NSMakeRect(0, 0, frame.size.width, frame.size.height);
previewWebView = [[WebView alloc] initWithFrame:webViewFrame];
[previewWebView setUIDelegate:self];
[previewWebView setFrameLoadDelegate:self];
[previewWebView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[previewContainer addSubview:previewWebView];
and not the interface builder, the swapping code works as expected. Any ideas as to what may be different about how I'm defining the WebView in code that makes it work but not when define in the interface builder?
It's very possible that Interface Builder archives the WebView object with some settings that are different to the defaults when creating a WebView programmatically.
You should probably try a few things:
[previewWebview setHostWindow:yourWindow];
This associates the WebView with your window. This is required if you remove the WebView from the window, otherwise the WebView will stop operating. The WebView will retain your window, so you should make sure you set the host window to nil before closing your window.
[previewWebView setShouldUpdateWhileOffscreen:YES];
This will ensure the web view actually loads content when it's offscreen.
[previewWebView setShouldCloseWithWindow:NO];
This will prevent the WebView from "closing" when its host window closes. If you don't do this, the WebView will call its close method, which essentially shuts it down, clearing all content and caches and preventing it from being used again. I'm pretty sure this is the default when you instantiate the WebView in Interface Builder, so you want to make sure it doesn't happen.
You may find that you don't need to do this if you've set the host window specifically.
Note that you will need to call [previewWebView close] when you do actually close your window if you do this.
I need a gallery in my application and I found this tutorial on how to implement the Three20 into my application: http://www.raywenderlich.com/1430/how-to-use-the-three20-photo-viewer.
I've a storyboard where the root is a Tab Bar Controller. This root controller has a relation with a View Controller with the class PhotoViewController from the above tutorial.
The last step in the tutorial is to add code to the AppDelegate, but in my case I added this code to the PhotoViewController:
#import "PhotoViewController.h"
#import "PhotoSet.h"
#implementation PhotoViewController
#synthesize photoSet = _photoSet;
- (void) viewDidLoad {
[[TTURLRequestQueue mainQueue] setMaxContentLength:0];
TTNavigator *navigator = [TTNavigator navigator];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
navigator.window = window;
TTURLMap *map = navigator.URLMap;
[map from:#"tt://appPhotos" toSharedViewController:[PhotoViewController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"tt://appPhotos"]];
self.photoSource = [PhotoSet samplePhotoSet];
}
- (void) dealloc {
self.photoSet = nil;
}
#end
Here is a image of the result after tabbing the "Gallery" tab in the root tab controller:
Here is a image when I scroll in the view:
This is almost fine, but I have some issues:
When I tab the Gallery and the above view appear, there is no way back to the root tab controller. How can I add a back button?
When I swipe in the gallery, the text "Error" is displayed for a while until the image is fully loaded. Why?
When I tab "See All" it shows an list of all images. How can I change the background-color for the navigation bar in the top?
I think that MWPhotoBrowser has a nicer interface than EGOPhotoViewer (feels closer to the native Photos app).
MWPhotoBrowser is an implementation of a photo browser similar to the native Photos app in iOS. It can display one or more images by providing either UIImage objects, file paths to images on the device, or URLs to images online. The photo browser handles the downloading and caching of photos from the web seamlessly. Photos can be zoomed and panned, and optional (customisable) captions can be displayed. Works on iOS 3.2+. All strings are localisable so they can be used in apps that support multiple languages.
Better try EGOPhotoViewer.
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.
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.