单击UITextField时如何显示联系人?

时间:2019-05-09 08:56:25

标签: ios swift xcode uitextfield

我已经多次尝试创建一个UITextField,当单击该UITextField时应显示设备上可用的联系人,并检索电话号码并将其显示在文本字段中。但是我一直无法做到这一点。我能做的最好的就是使用一个按钮来接收数字并将其显示在文本字段上。这可行!点击private let contactPicker = CNContactPickerViewController() override func viewDidLoad() { super.viewDidLoad() configureTextFields() configureTapGesture() phonenumber.textContentType = .telephoneNumber } private func configureTextFields() { phonenumber.delegate = self } private func configureTapGesture(){ let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SelfTestTimer.handleTap)) viewcontact.addGestureRecognizer(tapGesture) } @objc private func handleTap(){ viewcontact.endEditing(true) } @IBAction func pbbbbb(_ sender: Any) { contactPicker.delegate = self self.present(contactPicker, animated: true, completion: nil) } } extension SelfTestTimer: CNContactPickerDelegate { func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) { let phoneNumberCount = contact.phoneNumbers.count guard phoneNumberCount > 0 else { dismiss(animated: true) return } if phoneNumberCount == 1 { setNumberFromContact(contactNumber: contact.phoneNumbers[0].value.stringValue) }else{ let alertController = UIAlertController(title: "Select one of the numbers", message: nil, preferredStyle: .alert) for i in 0...phoneNumberCount-1 { let phoneAction = UIAlertAction(title: contact.phoneNumbers[i].value.stringValue, style: .default, handler: { alert -> Void in self.setNumberFromContact(contactNumber: contact.phoneNumbers[i].value.stringValue) }) alertController.addAction(phoneAction) } let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: { alert -> Void in }) alertController.addAction(cancelAction) dismiss(animated: true) self.present(alertController, animated: true, completion: nil) } } func setNumberFromContact(contactNumber: String) { var contactNumber = contactNumber.replacingOccurrences(of: "-", with: "") contactNumber = contactNumber.replacingOccurrences(of: "(", with: "") contactNumber = contactNumber.replacingOccurrences(of: ")", with: "") guard contactNumber.count >= 10 else { dismiss(animated: true) { self.presentAlert(alertTitle: "", alertMessage: "A maximum of 10 contacts allowed per session", lastAction: nil) } return } phonenumber.text = String(contactNumber.suffix(10)) } func contactPickerDidCancel(_ picker: CNContactPickerViewController) { } } extension SelfTestTimer: UITextFieldDelegate { func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } } 时该如何做?

我正在Xcode 10上运行它

UITextField

我想要这样,以便在单击public class LocationUpdateService extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener { private Context mContext; Location mLocation; GoogleApiClient mGoogleApiClient; public double lattitude = 0.0, longitude = 0.0; private LocationRequest mLocationRequest; private long UPDATE_INTERVAL = 40 * 1000; private long FASTEST_INTERVAL = 20 * 1000; public static String LOCATION_UPDATE_BROADCAST = "com.demo"; private static final String TAG = LocationUpdateService.class.getSimpleName(); @Override public void onCreate() { showLog("onCreate"); mContext = LocationUpdateService.this; mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); showLog("onStart"); if (mGoogleApiClient != null) { mGoogleApiClient.connect(); } return Service.START_STICKY; } @Override public void onConnected(Bundle bundle) { showLog("onConnected");/* Always called but nothing happen after it */ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation!=null) { Log.e("latitude--", String.valueOf(mLocation.getLatitude())); try { lattitude = mLocation.getLatitude(); longitude = mLocation.getLongitude(); } catch (Exception ex) { showLog("Exception = "+ex.getMessage()); } if(lattitude != 0.0 && longitude != 0.0) { //Here set latitude longitude in prefreance when get new lattitude longitude Session.setLatitude(mContext, String.valueOf(lattitude)); Session.setLongitude(mContext, String.valueOf(longitude)); Geocoder geocoder = new Geocoder(mContext); try { List<Address> addresses = geocoder.getFromLocation(lattitude,longitude,1); if(addresses != null && addresses.size() >0) { String country_name = addresses.get(0).getCountryName(); ProjectUtils.showLog(TAG,""+country_name); Session.setCountry_name(mContext,country_name); } }catch (IOException e) { e.printStackTrace(); } Intent intent = new Intent(LocationUpdateService.LOCATION_UPDATE_BROADCAST); intent.setPackage(mContext.getPackageName()); mContext.sendBroadcast(intent); }else { } } startLocationUpdates(); } @Override public void onConnectionSuspended(int i) { showLog("onConnectionSuspended"); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { showLog("onConnectionFailed"); } @Override public void onLocationChanged(Location location) { showLog("onLocationChanged"); if(location!=null) { try { lattitude = location.getLatitude(); longitude = location.getLongitude(); } catch (Exception ex) { showLog("Exception = "+ex.getMessage()); } if(lattitude != 0.0 && longitude != 0.0) { //Here again set location in prefreance if latitude longitude changes Session.setLatitude(mContext, String.valueOf(lattitude)); Session.setLongitude(mContext, String.valueOf(longitude)); Intent intent = new Intent(LocationUpdateService.LOCATION_UPDATE_BROADCAST); intent.setPackage(mContext.getPackageName()); mContext.sendBroadcast(intent); } } } @SuppressLint("RestrictedApi") protected void startLocationUpdates() { mLocationRequest = new LocationRequest(); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setInterval(UPDATE_INTERVAL); mLocationRequest.setFastestInterval(FASTEST_INTERVAL); if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } @Override public void onDestroy() { super.onDestroy(); showLog("Service Stop"); stopLocationUpdates(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } public void stopLocationUpdates() { if (mGoogleApiClient.isConnected()) { LocationServices.FusedLocationApi .removeLocationUpdates(mGoogleApiClient, this); mGoogleApiClient.disconnect(); } } private void showLog(String text) { Log.e(TAG,text); } } 时显示联系人,并且在选择一个联系人时,该号码应出现在文本字段中

1 个答案:

答案 0 :(得分:2)

您应该使用textFieldShouldBeginEditing方法。使用此方法打开通讯录控制器,并返回false,无需添加手势识别器。