Skip to content

Commit

Permalink
#35 Unicode characters not working
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Feb 16, 2016
1 parent ff4fcd3 commit 72efce6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 62 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Android code: mostly [Brill Papping](https://github.com/bpappin)


## 6. Change history
2.2.1 Encoding of diacritical characters fixed on iOS, so you can now use `Español` as a title or button label.
1.1.6 You can now set the iOS actionSheet origin position (uses the iOS `actionSheet.showFromRect` method)
1.1.2 You can now select a theme for your Android popup, see the first example above

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-actionsheet",
"version": "2.2.0",
"version": "2.2.1",
"description": "Show a sheet of options the user can choose from.",
"cordova": {
"id": "cordova-plugin-actionsheet",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin
xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-actionsheet"
version="2.2.0">
version="2.2.1">

<name>ActionSheet</name>

Expand Down
131 changes: 71 additions & 60 deletions src/ios/ActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,88 @@ @implementation ActionSheet
NSString* theCallbackId;
UIActionSheet *actionSheet;

- (CGRect)getPopupRectFromIPadPopupCoordinates:(NSArray*)comps {
CGRect rect = CGRectZero;
if ([comps count] == 2) {
rect = CGRectMake([[comps objectAtIndex:0] integerValue], [[comps objectAtIndex:1] integerValue], 0, 0);
} else if ([comps count] == 4) {
rect = CGRectMake([[comps objectAtIndex:0] integerValue], [[comps objectAtIndex:1] integerValue], [[comps objectAtIndex:2] integerValue], [[comps objectAtIndex:3] integerValue]);
}
return rect;
}

#pragma mark - Cordova interface methods
- (void) show:(CDVInvokedUrlCommand*)command {
theCallbackId = command.callbackId;
NSDictionary* options = [command.arguments objectAtIndex:0];

NSString *title = [options objectForKey:@"title"] ?: nil;
NSArray *buttons = [options objectForKey:@"buttonLabels"];
NSArray *position = [options objectForKey:@"position"] ?: nil;
NSString *addCancelButtonWithLabel = [options objectForKey:@"addCancelButtonWithLabel"] ?: nil;
NSString *addDestructiveButtonWithLabel = [options objectForKey:@"addDestructiveButtonWithLabel"] ?: nil;

[self.commandDelegate runInBackground:^{

actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:addDestructiveButtonWithLabel
otherButtonTitles:nil];
theCallbackId = command.callbackId;
NSDictionary* options = command.arguments[0];

for(int i = 0; i < [buttons count]; i++) {
[actionSheet addButtonWithTitle:[buttons objectAtIndex:i]];
}
NSString *title = options[@"title"] ?: nil;
NSArray *buttons = options[@"buttonLabels"];
NSArray *position = options[@"position"] ?: nil;
NSString *addCancelButtonWithLabel = options[@"addCancelButtonWithLabel"] ?: nil;
NSString *addDestructiveButtonWithLabel = options[@"addDestructiveButtonWithLabel"] ?: nil;

if (addCancelButtonWithLabel != nil) {
[actionSheet addButtonWithTitle:addCancelButtonWithLabel];
actionSheet.cancelButtonIndex = [buttons count]+(addDestructiveButtonWithLabel == nil ? 0 : 1);
[self.commandDelegate runInBackground:^{

actionSheet = [[UIActionSheet alloc] initWithTitle:[self encodeString:title]
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:[self encodeString:addDestructiveButtonWithLabel]
otherButtonTitles:nil];

for(int i = 0; i < [buttons count]; i++) {
[actionSheet addButtonWithTitle:[self encodeString:buttons[i]]];
}

if (addCancelButtonWithLabel != nil) {
[actionSheet addButtonWithTitle:[self encodeString:addCancelButtonWithLabel]];
actionSheet.cancelButtonIndex = [buttons count]+(addDestructiveButtonWithLabel == nil ? 0 : 1);
}

dispatch_async(dispatch_get_main_queue(), ^{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (position!= nil) {
CGRect rect = [self getPopupRectFromIPadPopupCoordinates:position];
[actionSheet showFromRect:rect inView:self.webView.superview animated:YES];
} else {
[actionSheet showInView:self.webView.superview];
}

dispatch_async(dispatch_get_main_queue(), ^{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (position!= nil) {
CGRect rect = [self getPopupRectFromIPadPopupCoordinates:position];
[actionSheet showFromRect:rect inView:self.webView.superview animated:YES];
} else {
[actionSheet showInView:self.webView.superview];
}
}
else{
// In this case the device is an iPhone/iPod Touch.
[actionSheet showInView:self.webView.superview];
}
});
}];
}
else{
// In this case the device is an iPhone/iPod Touch.
[actionSheet showInView:self.webView.superview];
}
});
}];
}

- (void) hide:(CDVInvokedUrlCommand*)command {
dispatch_async(dispatch_get_main_queue(), ^{
// dismissing with -2 because it's +1'd by didDismissWithButtonIndex below and we want it to report -1
[actionSheet dismissWithClickedButtonIndex:-2 animated:true];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:theCallbackId];
});
dispatch_async(dispatch_get_main_queue(), ^{
// dismissing with -2 because it's +1'd by didDismissWithButtonIndex below and we want it to report -1
[actionSheet dismissWithClickedButtonIndex:-2 animated:true];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:theCallbackId];
});
}

// delegate function of UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
#pragma mark - helper methods
- (NSString*) encodeString:(NSString*)input {
if (input == nil) {
return nil;
} else {
return [NSString stringWithCString:[input cStringUsingEncoding:NSISOLatin1StringEncoding]
encoding:NSUTF8StringEncoding];
}
}

// ActionSheet button index is 0-based, but other Cordova plugins are 1-based (prompt, confirm)
buttonIndex++;
- (CGRect)getPopupRectFromIPadPopupCoordinates:(NSArray*)comps {
CGRect rect = CGRectZero;
if ([comps count] == 2) {
rect = CGRectMake([[comps objectAtIndex:0] integerValue], [[comps objectAtIndex:1] integerValue], 0, 0);
} else if ([comps count] == 4) {
rect = CGRectMake([[comps objectAtIndex:0] integerValue], [[comps objectAtIndex:1] integerValue], [[comps objectAtIndex:2] integerValue], [[comps objectAtIndex:3] integerValue]);
}
return rect;
}

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)buttonIndex];
[self.commandDelegate sendPluginResult:pluginResult callbackId:theCallbackId];
#pragma mark - UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

// ActionSheet button index is 0-based, but other Cordova plugins are 1-based (prompt, confirm)
buttonIndex++;

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)buttonIndex];
[self.commandDelegate sendPluginResult:pluginResult callbackId:theCallbackId];
}

@end

0 comments on commit 72efce6

Please sign in to comment.