, UIWebVIew iOS 11 iPad, Phonegap/Cordova. UIWebVIew WKWebView, , Apple . Phonegap/Cordova WKWebView, . , div , .
index.html
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1 maximum-scale=1, user-scalable=no" />
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval'">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="PhonegapUtility.js"></script>
<script type="text/javascript">
function onBodyLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
addSelectButton('#selecta');
addSelectButton('#selectb');
}
function addSelectButton(selectID) {
var u = new PhonegapUtility();
u.isIpad(function(resp){
if (resp == 1) {
var buttonID = selectID+"Button";
if($(buttonID).length == 0) {
$("body").append("<div id='"+buttonID.replace("#","")+"' onclick='showPicker(\""+selectID+"\");'></div>");
}
$(buttonID).css("position","absolute");
$(buttonID).css("left",$(selectID).offset().left+"px");
$(buttonID).css("top",""+$(selectID).offset().top+"px");
$(buttonID).css("width",$(selectID).width()+"px");
$(buttonID).css("height",$(selectID).height()+"px");
}
});
}
function showPicker(selectID) {
var optionArray = [];
$(selectID).find('option').each(function(index,element){
optionArray.push(element.text);
});
var u = new PhonegapUtility();
u.showPicker(optionArray.join("|||"),$(selectID).prop('selectedIndex'),function(resp){
$(selectID+" option")[resp].selected = true;
});
}
</script>
</head>
<body onload="onBodyLoad();">
<select id="selecta">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<select id="selectb">
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</select>
</body>
</html>
PhonegapUtility.h
#import <Cordova/CDV.h>
@interface PhonegapUtility : CDVPlugin <UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) NSString *callbackId;
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) UIView *pickerWrappertView;
@property (strong, nonatomic) NSArray *pickerData;
- (void) isIpad:(CDVInvokedUrlCommand*)command;
- (void) showPicker:(CDVInvokedUrlCommand*)command;
- (void) pickerDone;
@end
PhonegapUtility.m
#import "PhonegapUtility.h"
#import "AppDelegate.h"
@implementation TomPhonegapUtility
@synthesize callbackId,pickerData,pickerView,pickerWrappertView;
- (void) isIpad:(CDVInvokedUrlCommand*)command {
int iPad = 0;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) iPad = 1;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:iPad];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void) showPicker:(CDVInvokedUrlCommand*)command {
callbackId = [[NSString alloc] initWithString: command.callbackId];
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
UIViewController *rootViewController = appDelegate.window.rootViewController;
pickerData = [[command.arguments objectAtIndex:0] componentsSeparatedByString:@"|||"];
float viewWidth = rootViewController.view.bounds.size.width;
float viewHeight = rootViewController.view.bounds.size.height;
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStylePlain
target:self
action:@selector(pickerDone)];
toolBar.items = @[flex, barButtonDone];
pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
[pickerView setFrame: CGRectMake(0, toolBar.frame.size.height, viewWidth, 180.0f)];
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:[[command.arguments objectAtIndex:1] intValue] inComponent:0 animated:NO];
pickerWrappertView = [[UIView alloc] initWithFrame:CGRectMake(0, viewHeight-toolBar.frame.size.height-pickerView.frame.size.height, viewWidth, toolBar.frame.size.height + pickerView.frame.size.height)];
pickerWrappertView.backgroundColor = [UIColor whiteColor];
[pickerWrappertView addSubview:pickerView];
[pickerWrappertView addSubview:toolBar];
[rootViewController.view addSubview:pickerWrappertView];
}
- (void) pickerDone {
int selectedIndex = (int) [pickerView selectedRowInComponent:0];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:selectedIndex];
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
[pickerWrappertView removeFromSuperview];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [pickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [pickerData objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}