如何以编程方式关闭系统对话框,如“< App>想要访问您的照片“? [英] How to programmatically dismiss system dialogs like "<App> would like to access your photos"?
问题描述
有没有办法通过编程方式关闭应用程序想访问照片,访问联系人和访问位置的对话框? API方法,但我真的不知道哪个。找出哪些方法需要调整的方法是什么?如果不是这样的话,那么另外可以做什么呢?
作为一个说明,这不是一个产品,只是为了测试,所以swizzling是一个很好的选择,如果
1。覆盖访问联系人对话框
在您的 UIApplicationDelegate
实现中输入此代码(导入 < AddressBook / AddressBook.h>
是必需的:
ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void){
return kABAuthorizationStatusAuthorized;
}
2。创建一个swizzling方法:
在您的 UIApplicationDelegate
实现中键入以下方法,或者无论您需要什么,辅助类最好(导入< objc / runtime.h>
是必需的:
- (void )swizzleSelector:(SEL)selector fromInstancesOfClass:(Class)clazz withBlock:(id)block {
id replaceBlock = Block_copy(block);
方法origMethod = class_getInstanceMethod(clazz,selector);
IMP origImpl = method_getImplementation(origMethod);
IMP replaceImpl = imp_implementationWithBlock(replaceBlock);
method_setImplementation(origMethod,replaceImpl);
}
- (void)swizzleSelector:(SEL)selector fromClass:(Class)clazz withBlock:(id)block {
id replaceBlock = Block_copy(block);
方法origMethod = class_getClassMethod(clazz,selector);
IMP origImpl = method_getImplementation(origMethod);
IMP replaceImpl = imp_implementationWithBlock(replaceBlock);
method_setImplementation(origMethod,replaceImpl);
}
3。覆盖访问位置对话框:
在您的 UIApplicationDelegate
实现中执行以下操作。 (需要导入< CoreLocation / CoreLocation.h>
):
[self swizzleSelector:@selector(startUpdatingLocation)fromInstancesOfClass:[CLLocationManager class] withBlock:^ {}];
[self swizzleSelector:@selector(startMonitoringSignificantLocationChanges)fromInstancesOfClass:[CLLocationManager class] withBlock:^ {}];
[self swizzleSelector:@selector(locationServicesEnabled)fromClass:[CLLocationManager class] withBlock:^ {return NO; }];
4。覆盖访问照片对话框:
在您的 UIApplicationDelegate
实现中执行以下操作。 (需要导入< AssetsLibrary / AssetsLibrary.h>
):
[self swizzleSelector:@selector(authorizationStatus)fromClass:[ALAssetsLibrary class] withBlock:^ {return ALAuthorizationStatusAuthorized; }];
[self swizzleSelector:@selector(enumerateGroupsWithTypes:usingBlock:failureBlock :) fromInstancesOfClass:[ALAssetsLibrary class] withBlock:^ {}];
Is there a way to programmatically dismiss dialogs like the ones where the app wants to access photos, access contacts and access location?
I think there's a way by swizzling API methods, but I don't really know which. What is the methodology to find out which methods need to be swizzled? If swizzling is not the way then what could be another alternative?
As a note, this is not for a product, is just for testing so swizzling is a good option if it works.
1. Overriding "access to contacts" dialog
Type this code in your UIApplicationDelegate
implementation (importing <AddressBook/AddressBook.h>
is necessary:
ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void) {
return kABAuthorizationStatusAuthorized;
}
2. Create a swizzling method:
Type in the following method in your UIApplicationDelegate
implementation, or wherever you need it, a helper class would be best. (importing <objc/runtime.h>
is required:
- (void)swizzleSelector:(SEL)selector fromInstancesOfClass:(Class)clazz withBlock:(id)block {
id replaceBlock = Block_copy(block);
Method origMethod = class_getInstanceMethod(clazz, selector);
IMP origImpl = method_getImplementation(origMethod);
IMP replaceImpl = imp_implementationWithBlock(replaceBlock);
method_setImplementation(origMethod, replaceImpl);
}
- (void)swizzleSelector:(SEL)selector fromClass:(Class)clazz withBlock:(id)block {
id replaceBlock = Block_copy(block);
Method origMethod = class_getClassMethod(clazz, selector);
IMP origImpl = method_getImplementation(origMethod);
IMP replaceImpl = imp_implementationWithBlock(replaceBlock);
method_setImplementation(origMethod, replaceImpl);
}
3. Overriding "access to location" dialog:
Do the following in your UIApplicationDelegate
implementation. (importing <CoreLocation/CoreLocation.h>
is required):
[self swizzleSelector:@selector(startUpdatingLocation) fromInstancesOfClass:[CLLocationManager class] withBlock:^{}];
[self swizzleSelector:@selector(startMonitoringSignificantLocationChanges) fromInstancesOfClass:[CLLocationManager class] withBlock:^{}];
[self swizzleSelector:@selector(locationServicesEnabled) fromClass:[CLLocationManager class] withBlock:^{ return NO; }];
4. Overriding "access to photos" dialog:
Do the following in your UIApplicationDelegate
implementation. (importing <AssetsLibrary/AssetsLibrary.h>
is required):
[self swizzleSelector:@selector(authorizationStatus) fromClass:[ALAssetsLibrary class] withBlock:^{ return ALAuthorizationStatusAuthorized; }];
[self swizzleSelector:@selector(enumerateGroupsWithTypes:usingBlock:failureBlock:) fromInstancesOfClass:[ALAssetsLibrary class] withBlock:^{}];
这篇关于如何以编程方式关闭系统对话框,如“< App>想要访问您的照片“?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!