اكتشاف وجود الكاميرا في تطبيق iPhone؟
-
30-09-2019 - |
سؤال
أنا أكتب تطبيق iOS ، وأحتاج إلى أن أكون قادرًا على اكتشاف ما إذا كان الجهاز يحتوي على كاميرا. في السابق ، كنت أتحقق مما إذا كان الجهاز هو جهاز iPhone أم لا ، نظرًا لأن iPhone فقط يحتوي على كاميرا - ولكن مع إطلاق iPod Touch 4 ، لم يعد هذا خيارًا قابلًا للتطبيق. يعمل التطبيق بدون كاميرا ، لكن وجود الكاميرا يضيف وظيفة.
لذا ، هل يمكن لأي شخص أن يزودني برمز يرجع ما إذا كانت هناك كاميرا أم لا؟
المحلول
يمكنك استخدام +isSourceTypeAvailable:
الطريقة في UiimagePickerNtroller:
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
// Has camera
نصائح أخرى
سريع 3
كما خوان بويرو كتب التحقق من:
if UIImagePickerController.isSourceTypeAvailable(.camera){ }
لكنني سأضيف شيكًا آخر لمعرفة ما إذا كان المستخدم يسمح للوصول إلى الكاميرا كما تقترح Apple في مثال التصوير الخاص بهم (مثال Photopicker Objective-C):
*يرجى ملاحظة أنه يجب عليك استيراد Avfoundation
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if authStatus == AVAuthorizationStatus.denied {
// Denied access to camera
// Explain that we need camera access and how to change it.
let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
dialog.addAction(okAction)
self.present(dialog, animated:true, completion:nil)
} else if authStatus == AVAuthorizationStatus.notDetermined { // The user has not yet been presented with the option to grant access to the camera hardware.
// Ask for it.
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
// If access was denied, we do not set the setup error message since access was just denied.
if grantd {
// Allowed access to camera, go ahead and present the UIImagePickerController.
self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
}
})
} else {
// Allowed access to camera, go ahead and present the UIImagePickerController.
self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
}
func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = sourceType
self.present(myPickerController, animated: true, completion: nil)
}
إذا كنت تستخدم فصول Foundation AV بدلاً من UiimagePickerController ، فيمكنك القيام بذلك:
BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);
إذا كنت تستخدم UiimagePickerController ، فربما لا يستحق ذلك ، حيث يجب عليك إضافة avfoundation.framework إلى مشروعك.
نعم ، هناك واجهة برمجة تطبيقات مقدمة للقيام بذلك:
BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
سويفت:
if UIImagePickerController.isSourceTypeAvailable(.Camera){
//Your code goes here
//For example you can print available media types:
print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))
}
إذا كنت بحاجة إلى معرفة ما إذا كان للجهاز على وجه التحديد كاميرا أمامية أو خلفية ، فاستخدم هذا:
isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
للتحقق من الكاميرا متوفرة (سريعة)
if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
يمكنك التحقق من توفر نوع مصدر معين باستخدام جلسة الاكتشاف (Swift 5):
let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
let isWideAngleCameraSupported = !discovery.devices.isEmpty