问题
Recently iOS has an update of iOS 10 & there are certain changes for developers one of the change is now we can't check allow full access the way we did previously is given below
-(BOOL)isOpenAccessGranted{
return [UIPasteboard generalPasteboard];
}
I searched the latest Developer Guide for UIPasteboard, but was unable to solve it. Did any one has a proper solution for this.
回答1:
iOS11 and above is easy.
iOS10 Solution: Check all the copy-able types, if one of them is available, you have full access otherwise not.
-- Swift 4.2--
override var hasFullAccess: Bool
{
if #available(iOS 11.0, *){
return super.hasFullAccess// super is UIInputViewController.
}
if #available(iOSApplicationExtension 10.0, *){
if UIPasteboard.general.hasStrings{
return true
}
else if UIPasteboard.general.hasURLs{
return true
}
else if UIPasteboard.general.hasColors{
return true
}
else if UIPasteboard.general.hasImages{
return true
}
else // In case the pasteboard is blank
{
UIPasteboard.general.string = ""
if UIPasteboard.general.hasStrings{
return true
}else{
return false
}
}
} else{
// before iOS10
return UIPasteboard.general.isKind(of: UIPasteboard.self)
}
}
回答2:
I have fixed this issue. iOS 10.0 and Swift 3.0
func isOpenAccessGranted() -> Bool {
if #available(iOSApplicationExtension 10.0, *) {
UIPasteboard.general.string = "TEST"
if UIPasteboard.general.hasStrings {
// Enable string-related control...
UIPasteboard.general.string = ""
return true
}
else
{
UIPasteboard.general.string = ""
return false
}
} else {
// Fallback on earlier versions
if UIPasteboard.general.isKind(of: UIPasteboard.self) {
return true
}else
{
return false
}
}
}
Use like this:-
if (isOpenAccessGranted())
{
print("ACCESS : ON")
}
else{
print("ACCESS : OFF")
}
回答3:
For friends, searching solution in Objective-C, Here it is
NSOperatingSystemVersion operatingSystem= [[NSProcessInfo processInfo] operatingSystemVersion];
if (operatingSystem.majorVersion>=10) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"Hey";
if (pasteboard.hasStrings) {
pasteboard.string = @"";
return true;
}
else
{
pasteboard.string = @"";
return false;
}
}
else
{
return [UIPasteboard generalPasteboard];
}
P.S.: This is just a workaround
回答4:
Tested on iOS 10 Swift 3.0 and iOS 9
Use #available(iOS 10.0, *)
instead of #available(iOSApplicationExtension 10.0, *)
func isOpenAccessGranted() -> Bool {
if #available(iOS 10.0, *) {
var originalString = UIPasteboard.general.string
if(!(originalString != nil)){
originalString = ""
}
UIPasteboard.general.string = "Test"
if UIPasteboard.general.hasStrings {
UIPasteboard.general.string = originalString
return true
}else{
return false
}
}else{
return UIPasteboard.general.isKind(of: UIPasteboard.self)
}
}
回答5:
Swift 3
static func isOpenAccessGranted() -> Bool {
if #available(iOS 10.0, iOSApplicationExtension 10.0, *) {
let value = UIPasteboard.general.string
UIPasteboard.general.string = "checkOpenedAccess"
let hasString = UIPasteboard.general.string != nil
if let _ = value, hasString {
UIPasteboard.general.string = value
}
return hasString
}
else {
return UIPasteboard(name: UIPasteboardName(rawValue: "checkOpenedAccess"), create: true) != nil
}
}
来源:https://stackoverflow.com/questions/39543113/allow-full-access-check-in-keyboards-ios10