In my App I want to disable the Copy/Paste/Cut of contents that are showed by a UIWebView object. To achieve this, I created a UIWebView subclass and overrode the - (BOOL)canPerformAction:(SEL)action withSender:(id)sender method:
#pragma mark - UIResponderStandardEditActions
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(copy:) ||
action == #selector(paste:)||
action == #selector(cut:)) {
return _copyCutAndPasteEnabled;
}
return [super canPerformAction:action withSender:sender];
}
Now the user no longer can make such operations, however the UIWebView still shows the "selection rectangle", as you can see in the following screenshot:
NOTE: The content being showed in the UIWebView is not HTML pages. I'm showing document files (PDF, DOC, PPT), loaded from a file using:
NSURL *fileURL = [NSURL fileURLWithPath:<document file path..>];
NSURLRequest *fileRequest = [NSURLRequest requestWithURL:fileURL];
[<uiwebView> loadRequest:fileRequest];
Is there any way to disable/hide this selection rectangle feature too?
[]s,
You can try injecting javascript into the webView. This code works on the iPhone too but only when the page is fully loaded.
http://www.javascriptsource.com/page-details/disable-text-selection.html or
http://solidlystated.com/scripting/proper-way-to-disable-text-selection-and-highlighting/
To get it to work properly when the page is only half loaded or still loading you'll proably have to use a setup similar to this one where you inject the disabling javascript just as it would start selecting.
http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
One last thing I can think of that might work, have you tested adding the selector #selector(select:) to the list of disabled selectors?
EDIT:
Ok as you what to display a PDF rather then a html page you can try this.
Setup
Put the webView in a scrollView;
Set the ScrollView's: delegate to self; min zoom to 1.0 and max zoom to whatever you want;
After the weView has finished loading.
Scan for the first UIScrollView in the webView, (we are scanning for it so this shouldn't break on later iOS versions).
Set the UIWebViews frame to it's scrollViews size.
And set the scrollView's contentSize to the webViews contentSize (or by now, it's frame).
-(void)setup{
//Get the webView's first scrollView (the one all the content is in).
UIScrollView *webViewContentView;
for (UIView *checkView in [webView subviews] ) {
if ([checkView isKindOfClass:[UIScrollView class]]) {
webViewContentView = (UIScrollView*)checkView;
break;
}
}
webView.frame = CGRectMake(0, 0, webViewContentView.contentSize.width, webViewContentView.contentSize.height);
scrollView.contentSize = webViewContentView.contentSize;
scrollView.delegate = self;
}
To enable zooming you'll need to add an extra method from the scrollViews delegate.
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return webView;
}
And you can download a sample project http://www.multiupload.com/P8SOZ4NW6C (It's an xcode 4 project).
NOTES: The zooming pixelates the webView's content if you zoom in to much because the webView doesn't know to re-render things, and of cause you can't click links (but in a PDF that shouldn't be a problem).
EDIT 2: Better method
Since writing this I have realised a MUCH simpler and easier method, which should also solve the pixelation problem.
-(void)removeInput{
UIScrollView *webViewContentView;
for (UIView *checkView in [webView subviews] ) {
if ([checkView isKindOfClass:[UIScrollView class]]) {
webViewContentView = (UIScrollView*)checkView;
break;
}
}
for (UIView *checkView in [webViewContentView subviews] ) {
checkView.userInteractionEnabled = NO;
}
}
Now you should only be able to interact with the UIScrollView itself, meaning zooming, scrolling, ect.. should still work (and the page should re-render rather the pixelating when you zoom) but you can't select any text, or type anything in.
Also something about this method is that based on this list of UIWebView subViews it might be possible to set this before the page starts loading rather then after it has finished loading the page.
I found an answer on here by Johnny Rockex and it worked like a champ. It works for documents (including .pdf files) displayed in a UIWebView. UIWebView without Copy/Paste when displaying PDF files
Related
I have a UIWebView with a contentEditable div in order to implement some kind of rich text editor. I need to trimm the copy & cut options in the UIMenuController that appears in the web view once the user selects any piece of text.
There seems to be a lot of solutions around the web, but for some reason, non of them applies for my scenario.
I've subclassed the UIWebView and implemented the canPerformAction:(SEL)action withSender: to remove the copy and cut, but once the user chooses "Select" or "Select All", a new menu appears, and apparently, the web view does not intercept this action and the canPerform method is not being called.
Is there a way to trimm actions for this cases?
I will adapt another answer of mine for your case.
The canPerformAction: is actually called on the internal UIWebDocumentView instead of the UIWebView, which you cannot normally subclass. With some runtime magic, it's possible.
We create a class which has one method:
#interface _SwizzleHelper : UIView #end
#implementation _SwizzleHelper
-(BOOL)canPerformAction:(SEL)action
{
//Your logic here
return NO;
}
#end
Once you have a web view which you want to control the actions of, you iterate its scroll view's subviews and take the UIWebDocumentView class. We then dynamically make the superclass of the class we created above to be the subview's class (UIWebDocumentView - but we cannot say that upfront because this is private API), and replace the subview's class to our class.
#import "objc/runtime.h"
-(void)__subclassDocumentView
{
UIView* subview;
for (UIView* view in self.scrollView.subviews) {
if([[view.class description] hasPrefix:#"UIWeb"])
subview = view;
}
if(subview == nil) return; //Should not stop here
NSString* name = [NSString stringWithFormat:#"%#_SwizzleHelper", subview.class.superclass];
Class newClass = NSClassFromString(name);
if(newClass == nil)
{
newClass = objc_allocateClassPair(subview.class, [name cStringUsingEncoding:NSASCIIStringEncoding], 0);
if(!newClass) return;
Method method = class_getInstanceMethod([_SwizzleHelper class], #selector(canPerformAction:));
class_addMethod(newClass, #selector(canPerformAction:), method_getImplementation(method), method_getTypeEncoding(method));
objc_registerClassPair(newClass);
}
object_setClass(subview, newClass);
}
I have made a view in the application that shows a UITableView. It will be full of results (obviously) almost all the time, however when it does not have any results I want to show another view that inform the user about how he/she could populate the table.
I want to design that view in the interfacebuilder. I will have to check in the code whether the datasource is empty or not to toggle between the two different nibs. How do I instantiate and configure a view made in Interfacebuilder?
The easies way to do this is by adding the view in xib normally and make it visible
Design your both views, the table view and the other view, give the tableView a tag of 111 for example and give the otherview another tag 222 for example
Now in viewDidLoad
Get both the views
UIView *noDataView = [self.view viewWithTag:222];
UITableView *tableView = [self.view viewWithTag:111];
//Hide both of them or only the noDataView until you know if you have data from the dataSource or not
Check for your data source
//hasElements do you have any element to show?
if(hasElements)
{
noDatView.hidden = YES;
tableView.hidden = NO;
}
else
{
noDatView.hidden = NO;
tableView.hidden = YES;
}
You can load nib file based on condition.You can write category as follows:
self.view = (UIView *)[self loadNib:#"SecondView" inPlaceholder:self.view];
- (UIView *)viewFromNib:(NSString *)nibName
{
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
for (id view in xib) { // have to iterate; index varies
if ([view isKindOfClass:[UIView class]]) return view;
}
return nil;
}
- (UIView *)loadNib:(NSString *)nibName inPlaceholder:(UIView *)placeholder
{
UIView *nibView = [self viewFromNib:nibName];
[nibView setFrame:placeholder.frame];
self.view = nibView;
//[self.view insertSubview:nibView aboveSubview:placeholder];
//[placeholder removeFromSuperview];
return nibView;
}
The other answers give you possible technical solutions but I would propose that if you are using the standard Apple design guidelines, you probably don't even need to worry about it. For instance, somewhere on your screen you should have a bar button item with the identifier "Add" (which shows the plus icon). Then rather than giving a long (often poorly localised) description of how to add items, just have a header for an empty section which says "No items" replacing items with whatever pluralised noun is appropriate for your table's items. For example, for an Archery related app I am working on:
Notice how the Edit button is currently disabled too, thus no explanation is needed as the only thing they can do at this point is tap the Add button (screenshots on the Appstore will have shown them what they can expect to see after this point).
Sorry for the long question, but I have been stuck on this for days and have exhausted all other help.
Currently, I have a tab bar application with four tabs. In the second tab (SecondViewController), I have a segmented controller at the top that should switch between "videos" and "images". The videos page should have around 5 youtube videos loaded in UIWebView using the code here. The images view should contain around 5 thumbnails that, when clicked on, open into a larger picture. My problem is that I have tried out many different ways of accomplishing this, and none seem to work to any extent. Really the main thing I am looking for here is the recommended way of going about switching between two views using a segmented controller and if it is possible to load the views from different files (videosView.h/m and imagesView.h/m).
In SecondViewController.m, I have the app respond to the UISegmentedController using the following, though I have absolutely no idea if this is even close to correct.
- (IBAction)segmentedControlChanged
{
switch (segmentedControl.selectedSegmentIndex)
{case 0:
[self.view addSubview:videosView.view];
[imagesView.view removeFromSuperview];
NSLog(#"1");
break;
case 1:
[self.view addSubview:imagesView.view];
[videosView.view removeFromSuperview];
NSLog(#"2");
break;
default:
break;
}
}
In videosView.h, I only have the following:
#import <UIKit/UIKit.h>
#interface videosView : UIWebView
{
}
- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
#end
In videosView.m, I have the following, though I am getting a warning on the initWithFrame line.
- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];
// HTML to embed YouTube video
NSString *youTubeVideoHTML = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
#end
imagesView is made, but has no added code it in currently, as I am just trying to get the videos sorted out first.
My recommendation:
Use one view controller and have the view controller contain both views. You are already doing that.
You can still use separate files (subclasses of UIView).
Do not use addSubview: and removeFromSuperView:, but rather set these "container" view as hidden as appropriate.
Also, in the segmentedControlChanged method, do all the other necessary switching tasks, such as canceling open URL connections etc.
Do the initialization of the web content of the container views in viewDidLoad rather than in the initializer. Make sure you do not freeze the UI but use asynchronous loading.
EDIT: Adding subclassing code.
in SecondViewController.h:
#include VideosView.h
...
#property (nonatomic, retain) VideosView *videoView;
in SecondViewController.m
-(void)viewDidLoad {
self.videosView = [[VideosView alloc] init];
// add to superview etc.
}
Whenever you want to execute view specific code, just call any method you define in VideosView.h and implement in .m.
[self.videosView playVideo];
I have an app which I add a subview to (and remove the same subview based on user interactions). I am looking for a way to check whether the subview is present or not at any given time.
For example:
In the current view (UIView *viewA) I add a subview (UIView *viewB). I then want a way of checking whether viewB is being displayed at any given time.
Sorry if this isn't very clear, it's quite hard to describe.
an UIView stores its superview and is accessible with the superview-property just try
if([viewB superview]!=nil)
NSLog(#"visible");
else
NSLog(#"not visible");
But the better approach is to use the hidden-property of UIView
I went through the same issue and consulted Apple Documentation and came up with this elegant solution:
if([self.childView isDescendantOfView:self.parentView])
{
// The childView is contained in the parentView.
}
I updated to Swift4, Thanks a lot to #Shinnyx and #thomas.
if viewB.superview != nil{
print("visible")
}
else{
print("not visible")
}
if selfView.isDescendant(of: self.parentView) {
print("visible")
}
else{
print("not visible")
}
func isDescendant(of: UIView)
Returns a Boolean value indicating whether the receiver is a subview of a given view or identical to that view.
Here's a method I put in the appDelegate so that I can display the entire subview hierarchy from any point.
// useful debugging method - send it a view and it will log all subviews
// can be called from the debugger
- (void) viewAllSubviews:(UIView *) topView Indent:(NSString *) indent {
for (UIView * theView in [topView subviews]){
NSLog(#"%#%#", indent, theView);
if ([theView subviews] != nil)
[self viewAllSubviews:theView Indent: [NSString stringWithFormat:#"%# ",indent]];
}
}
call it with a string with one character and it will indent for you. (i.e. [appDelegate viewAllSubviews:self.view Indent:#" "];)
I find it useful to clear the debug pane first, then call this from the debugger, and copy it into a text editor like BBEdit that will show the indents.
You can call it using the mainWindow's view and see everything on your screen.
I'm using TTLauncherView as a sort of home screen for my app and I only have one page's worth of icons. How can I make it so the TTLauncherView won't let you drag icons to "the next page"? I want to set a maximum number of pages (in this case one.)
(EDIT: long story short, I subclassed beginEditing, see the answer below.)
I see where why it adds an extra page when beginEditing is called, but I don't want to edit the framework code. (That makes it hard to update to newer versions.) I'd also prefer not to subclass and override that one method, if I have to rely on how it's implemented. (I'm not against subclassing or adding a category if it's clean.)
I tried setting scrollView.scrollEnabled to NO in the callback method launcherViewDidBeginEditing in my TTLauncherViewDelegate, but that doesn't work while it's in editing mode and I don't know why.
I tried adding a blocker UIView to the scrollview to intercept the touch events by setting userInteractionEnabled=NO, which works OK. I still have to disable the dragging of TTLauncherItems to the next page somehow.
I also tried setting the contentSize of the scrollview to it's bounds in launcherViewDidBeginEditing, but that didn't seem to work either.
Is there a better way?
Tried to block gestures:
- (void)setLauncherViewScrollEnabled:(BOOL)scrollEnabled {
if (scrollEnabled) {
[self.scrollViewTouchInterceptor removeFromSuperview];
self.scrollViewTouchInterceptor = nil;
} else {
// iter through the kids to get the scrollview, put a gesturerecognizer view in front of it
UIScrollView *scrollView = [launcherView scrollViewSubview];
self.scrollViewTouchInterceptor = [UIView viewWithFrame:scrollView.bounds]; // property retains it
UIView *blocker = self.scrollViewTouchInterceptor;
[scrollView addSubview:scrollViewTouchInterceptor];
[scrollView sendSubviewToBack:scrollViewTouchInterceptor];
scrollViewTouchInterceptor.userInteractionEnabled = NO;
}
}
For reference: TTLauncherView.m:
- (void)beginEditing {
_editing = YES;
_scrollView.delaysContentTouches = YES;
UIView* prompt = [self viewWithTag:kPromptTag];
[prompt removeFromSuperview];
for (NSArray* buttonPage in _buttons) {
for (TTLauncherButton* button in buttonPage) {
button.editing = YES;
[button.closeButton addTarget:self action:#selector(closeButtonTouchedUpInside:)
forControlEvents:UIControlEventTouchUpInside];
}
}
// Add a page at the end
[_pages addObject:[NSMutableArray array]];
[_buttons addObject:[NSMutableArray array]];
[self updateContentSize:_pages.count];
[self wobble];
if ([_delegate respondsToSelector:#selector(launcherViewDidBeginEditing:)]) {
[_delegate launcherViewDidBeginEditing:self];
}
}
I think overriding beginEditing in TTLauncherView is your best bet. Since you'd only really be touching one method (and only a few lines in that method), upgrading it when the time comes shouldn't be too bad. Since that method explicitly adds the extra page, I'm not sure how you'd get around it w/o editing that specific piece of code
As Andrew Flynn suggested in his answer, I was able to make it work by subclassing and overriding the beginEditing method to remove the extra page TTLauncherView adds when it goes into editing mode.
One problem I'm having is I can't figure out how to remove the warning I get for calling the (private) method updateContentSize on my subclass. I tried casting it to id, but that didn't remove the warning. Is it possible?
edit: I was able to remove the warning by using performSelector to send the message to the private class. (I had previously create a category method performSelector:withInt that wraps NSInvocation so that I can pass primitives via performSelector methods, which makes this very convenient.)
// MyLauncherView.h
#interface MyLauncherView : TTLauncherView {
NSInteger _maxPages;
}
#property (nonatomic) NSInteger maxPages;
#end
// MyLauncherView.m
#implementation MyLauncherView
#synthesize maxPages = _maxPages;
- (void)beginEditing {
[super beginEditing];
// ignore unset or negative number of pages
if (self.maxPages <= 0) {
return;
}
// if we've got the max number of pages, remove the extra "new page" that is added in [super beginEditing]
if ([_pages count] >= self.maxPages ) {
[_pages removeLastObject];
[self updateContentSize:_pages.count]; // this has a compiler warning
// I added this method to NSObject via a category so I can pass primitives with performSelector
// [self performSelector:#selector(updateContentSize:) withInt:_pages.count waitUntilDone:NO];
}
}