6

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.

kb920
  • 3,039
  • 2
  • 33
  • 44
iYoung
  • 3,596
  • 3
  • 32
  • 59

6 Answers6

5

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)
    }
}
Ahmet Akkök
  • 466
  • 1
  • 5
  • 13
4

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")
}
Mitul Marsoniya
  • 5,272
  • 4
  • 33
  • 57
  • Don't you think setting "TEST" string to UIPastebord general will replace the preexisting text of user. Is this an optimal solution? – iYoung Sep 18 '16 at 05:42
  • It's work I have used UIPasteboard.generalPasteboard().string instead of UIPasteboard.general.hasStrings – kb920 Sep 19 '16 at 11:55
  • @mitul I have tried this but it is pasting TEST in OTT textfield which is not the proper solution. – iYoung Sep 19 '16 at 13:59
  • This use for check allow full access on or off. Now i change some code then you did not get pest just review edited code. – Mitul Marsoniya Sep 19 '16 at 14:04
  • @mitulmarsonia This is still not optimal as it will not paste TEST, instead it will override the already copied data of user with "". Hope you understand. – iYoung Sep 20 '16 at 09:16
  • @iYoung Just check before you copy data.Example if you want check before copy data Access on/off then just use this function and after you put your copy code. – Mitul Marsoniya Sep 20 '16 at 09:25
  • 1
    @mitulmarsonia: Completely agree that this is a workaround but from a user's persective this is not expected as user will never like the data to be vanished which he has copied for his future use. – iYoung Sep 20 '16 at 09:29
  • @iYoung You are also right i also think about that but now i use this way because i have don't know other solution for swift 3.0. If in future we get then i will sure update answer. Thanks. – Mitul Marsoniya Sep 20 '16 at 09:37
  • I am also reading the documents of apple for this change but still this should not be used for apps, that is what I believe. – iYoung Sep 20 '16 at 09:42
  • If you want temporary then you can used because we have not any other options so. – Mitul Marsoniya Sep 20 '16 at 09:48
  • 1
    Crashes the keyboard extension if UIImage has been pasted to UIPasteboard. – user3404693 Sep 23 '16 at 12:17
4

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

iYoung
  • 3,596
  • 3
  • 32
  • 59
0

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)
    }
}
Utsav Parikh
  • 1,216
  • 7
  • 14
0

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
    }
}
dimpiax
  • 12,093
  • 5
  • 62
  • 45
0

A bit hacky, but it works:

Swift 5

static func isOpenAccessGranted() -> Bool {
    let inputVC = UIInputViewController()
    return inputVC.hasFullAccess
}
Ruud Visser
  • 3,381
  • 2
  • 20
  • 19