How does iBooks do this? - objective-c

I am currently displaying text in a uiwebview. However, I would like to allow a user to select text and perform some action with the selected text (google it). Apple has done this sort of thing with iBooks. When you click on a word you can choose to look up the word in a dictionary. How can I do the same thing with Webview?
UIMenuController seems to be what I need to look at. But I couldn't find any sample code on how to do this. I'm new to iPhone development please help.

Starting in iOS 3.2 you can use UIMenuController to add additional UIMenuItems to the system menu that appears when selecting text in a UIWebView. Here's a simple example:
- (void)viewDidLoad {
[super viewDidLoad];
UIMenuItem *defineItem = [[[UIMenuItem alloc] initWithTitle:#"Define" action:#selector(defineSelection:)] autorelease];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:defineItem]];
}
- (void)defineSelection:(id)sender {
NSString *selection = [webView stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
// Do something with the selection
}
Apple describes how this works in the Adding Custom Edit Menu Items section of the Device Features Programming Guide.

This recent blog post from a developer would seem to suggest that at least in the iPad iOS branch, some iBooks functionality is based on private APIs.

Related

UIImagePickerController on iOS 7: Square mode?

I'm working with iOS 7 now, trying to get the UIImagePickerController to let the user select a square mode since it's a feature in iOS 7 now. Is there something I need to enable to allow this swipe? Relevant code posted below, though it's pretty standard.
UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraView.delegate = (id)self;
[self presentViewController:cameraView animated:YES completion:nil];
I've been scouring the messy iOS 7 documentation that's available, but I can't even tell if they changed the developer library at all - I don't see ANYTHING new in it.
set allowEditing to YES.
from the result dict use the key UIImagePickerControllerEditedImage
then you will have the squared image.
i find no way to let the user select, which format he want :(
I looked at the iOS 7 header for UIImagePickerController in Xcode, and I do not see new API for 7.0 or a square photo mode. The square photo mode appears to be a feature of the app, not of UIImagePickerController.

iOS GridView with Sections

I am trying to implement my own gridview similar to KKGridView.
My gridview has extra complexity because it needs to support multiple sections with a varying amount of items / cells in each section. (See below)
It also needs to be able to handle moving one cell from section 1 to section 0 and section 0 to section 1.
I have written the UIView to display the grid but it does not support moving cells from one section to another. The question is does anybody have a gridview that supports this functionality or can advise me of the best way forward as this gridview needs to be supported in iOS 5 & iOS 6
I would try using a combination of UICollectionView, with a UICollectionViewFlowLayout along with PSTCollectionView for iOS5 compatibility.
Take a look at the UICollectionViewDelegateFlowLayout docs, I think you'll find everything you need to build a per-section custom layout as you need.
PSTCollectionView is a "open Source, 100% API compatible replacement of UICollectionView for iOS4.3+". Further, you can use UICollectionView on iOS6 and only fall back to PSTCollectionView on iOS5 devices. Take a look at the GitHub project page.
I'm using this approach in one of the apps I'm working on, and it is really simple to implement.
EDIT: The steps you'd take to make what you want would be roughly as following:
You need to download at include the PSTCollectionView in your project. For that follow the steps on the GitHub page.
In your .h file, import PSTCollectionView.h and add the following two protocols: <PSUICollectionViewDataSource, PSUICollectionViewDelegate>. Note that with this you'll use Apple's UICollectionView in iOS6 and PSTCollectionView in iOS5. Here you should also add a property for your collection view: #property (nonatomic, strong) PSUICollectionView *collectionView;
In the viewDidLoad: method, you need to have something like this:
Code:
PSUICollectionViewFlowLayout *flowLayout = [[PSUICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setItemSize:CGSizeMake(91, 119)];
[flowLayout setMinimumLineSpacing:0];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
And you'd need to implement the following methods to your liking:
Methods:
numberOfSectionsInCollectionView:
collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:
collectionView:layout:insetForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
Important: Don't forget to prefix every mention of UICollectionView classes with PS. For example PSUICollectionView or PSUICollectionViewLayout.
EDIT 2: For a general understanding of UICollectionView refer to this excellent tutorial.
You can use you custom cells to achieve this functionality. One Cell with 10 subViews and 2nd with 6 subView. It will work for you.
you can get the view in the cell by tags i.e 1-6 and 1-10
Try this

How to set initial zoom on UIWebView in xcode

I'm wondering how to set an initial zoom on a PDF document loaded in a UIWebView in XCode. It is a table, and I want the web view to load zoomed in on a specific column (or part of a column which I can then scroll up and down). I've been reading and some people have pointed to this method - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script but I don't know exactly how this works.
And while we're on the subject of PDFs, is there any way to scrape the PDF and turn it into usable strings?
Edit: For reading a pdf, PDFBox is one possible solution. You have to import JAR files into your Xcode project. Here is a tutorial: http://www.oreillynet.com/pub/a/mac/2005/01/07/ipod_reader.html
UIWebView *webV = [[UIWebView alloc] init];
webV.scrollView.zoomScale = 4.0;

How to add the iOS "Open In..." feature to an app

I would like to know how to present the "Open In..." Action Sheet (iPhone) / Popover (iPad) from my app, preferably an IBAction
I would hope that it'd be similar to declaring a file type then creating the view and opening the app selected by the user, but I know it is more complicated then that.
I realize that a similar question has been asked on StackOverflow, but I cannot make sense of the answer that was accepted: How to use "open in..." feature to iOS app?, and I have found some Apple Documentation on Document Interaction Programming. But, I can't really make sense of these.
Create a UIDocumentInteractionController by using the interactionControllerWithURL: class method (pass the URL of the file you want to open in another app).
Then call either presentOpenInMenuFromRect:inView:animated: or presentOpenInMenuFromBarButtonItem:animated:. The controller takes care of presenting the popover with available apps for that file type and opening the selected app.
If you want to know when the menu was dismissed and which app was selected, you need to implement the UIDocumentInteractionControllerDelegate protocol.
omz makes some good points on how to do that in his answer, however this procedure is much easier with the introduction of new APIs in iOS 6. Here's a simple and efficient way to show the UIActionSheet Open-In-Menu in iOS 6 and up:
NSArray *dataToShare = #[contentData]; //Or whatever data you want to share - does not need to be an NSArray
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
Also, if your app is compatible with versions of iOS lower than 6.0 you may want to check if the Share Service exists:
if ([UIActivityViewController class])
Once you present the sheet, iOS will automatically handle the rest for you. It will display a beautiful uiactionsheet with icons showing each app or service the user can open / share your data with:
Note that depending on the contents of the data, iOS will show different services in the Share Sheet
EDIT: The method above shares the file content, but not the file itself. Refer to omz's answer for more on that.
I've personally never had to do this, but your answer can most certainly be found in this Apple Documentation: http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html#//apple_ref/doc/uid/TP40010410-SW1.

Can I disable UIPickerView scroll sound?

I want to disable the annoying clicks that the UIPickerView generates upon scrolling up and down. Is there a way to do this? I want to play short sounds for each item that the picker view lands upon. It gets ruined by the built in sound.
I understand that the picker sounds can be turned off globally by switching off the keyboard sounds in iPhone/iPod settings. But is there a way to programatically do this?
Any help will be much appreciated!
Thanks
I've been struggling with a UIPickerView sound issue, and even though it's only partially relevant to the original question, I'm posting the problem/solution here because this topic keeps coming up in my search results so I think anyone else in the same boat may end up here too…
I needed to initialize a UIPickerView to restore the currently selected row from saved data. Simple, right? In viewDidLoad, just call the selectRow:inComponent:animated method of UIPickerView:
[myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
This works as expected, but has a side effect that it generates a single "click" sound as if the user had scrolled the control. The click sound only occurs when running on a device (not the simulator), and only if the device has iOS 3.x installed (I tested with 3.1.3 and 3.2). This was apparently a bug in iOS that was fixed starting with iOS 4.0. But if you need to target Gen1 iPhone, you're stuck with iOS 3.1.3 where this problem is present.
I discussed the issue with Apple DTS, but they were unable to suggest any workaround other than upgrading to 4.0. I asked if they would make an exception and permit the use of the undocumented setSoundsEnabled mentioned above (which does actually solve the problem). The answer was, "There are no exceptions."
After some additional detective work, I discovered that you can prevent the sound from occurring by temporarily removing the UIPickerView from the superview, call selectRow, then re-add it to the superview. For example, in viewDidLoad:
UIView *superview = [myPicker superview];
[myPicker removeFromSuperview];
[myPicker reloadAllComponents];
[myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
[superview addSubview:myPicker];
This gets rid of the extraneous click sound without using undocumented/private APIs so should pass Apple's approval process.
After using this specific undocumented api for over a year on the App Store Apple finally asked me to remove it from my App. It is very frustrating for audio apps to have that damn click sound. The best advice is to share with users that the picker sound can be disabled globally in the settings application under "Sounds" and setting "Keyboard Clicks" to "Off". I also strongly recommend visiting https://bugreport.apple.com/ and filing a bug for UIPickerView, as it can cause distortion in audio applications when the picker click is played.
they have just rejected an app of mine because the use of undocumented api's...thats one of them.
Someone I know says he got this past the App Store review just last week:
// Hide private API call from Apple static analyzer
SEL sse = NSSelectorFromString([NSString stringWithFormat:#"%#%#%#", #"set",#"Sounds",#"Enabled:"]);
if ([UIPickerView instancesRespondToSelector:sse]) {
IMP sseimp = [UIPickerView instanceMethodForSelector:sse];
sseimp(self.thePicker, sse, NO);
}
There is an undocumented way (I'm actually not sure if it is still available in iphone 3.0) but here it is any way
#import <UIKit/UIKit.h>
#interface SilintUIPickerView: UIPickerView
{ }
- (void) setSoundsEnabled: (BOOL) enabled;
#end
use this subclass instead and call [view setSoundsEnabled: NO]
I'm interested in knowing how it goes in the latest SDK, give it a shot and let us know.
Could this trick work? Someone was able to suppress the camera shutter sound effect by playing an inverted copy of the sound at the same moment: https://stackoverflow.com/a/23758876/214070
Maybe this not the answer for this particular question, but I had a similar problem - set minimumDate for datePicker, and I wanted set it without annoying "click" sound. After some time found very simple solution:
datePickerCustomTime.minimumDate = [[NSDate date] dateByAddingTimeInterval:300]// min time to set = now + 5 min
[datePickerCustomTime setDate:[[NSDate date] dateByAddingTimeInterval:300] animated:NO];
I found small quickie solution for this try below
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, yPickerView, VIEW_WIDTH, PICKERVIEW_HEIGHT)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
pickerView.alpha = 0.8f;
pickerView.tag = fieldTag;
[pickerView selectRow:pickerViewSelectedIndex inComponent:0 animated:NO];
set the animated:NO for selectRow: method