I have the following code snippet for clearing user's clipboard:
func clearClipboard() {
let pb = UIPasteboard.generalPasteboard()
let pbtypes = pb.pasteboardTypes()
for pbtype in pbtypes {
pb.setValue("", forPasteboardType: pbtype)
}
print("Clipboard was cleared")
}
I tested the code on iPhone running iOS 8.4 and it works. However, on iOS 9.3 it doesn't work. On iOS 9.3, the pbtypes array is empty.
Did Apple change anything in the API permissions? Any advice will be appreciated.
So after more investigation it looks like this the way to clear the clipboard:
pb.setValue("", forPasteboardType: UIPasteboardNameGeneral)
Credit to this thread: How to clear/empty pasteboard on viewWillDisappear
Related
I am creating offer codes with auto-renewable In-app purchases in swift. But when i use presentCodeRedemptionSheet(), it does not show any redemption page to redeem offer code. I am using below code, but nothing is happening on tapping the button.
let paymentQueue = SKPaymentQueue.default()
if #available(iOS 14.0, *) {
DispatchQueue.main.async {
paymentQueue.presentCodeRedemptionSheet()
}
}
Are you trying on the simulator?
This command only works on a real device not in the simulator.
My code is pretty straight forward.
I am not sure what is the issue and I am not able to find a solution.
var suggestions = [INShortcut]()
for component in components
{
if let userActivity = UserActivityGenerator.getUserActivity(component: component)
{
let shortCut = INShortcut.init(userActivity: userActivity)
suggestions.append(shortCut)
}
}
INVoiceShortcutCenter.shared.setShortcutSuggestions(suggestions)
The app crashes in setShortcutSuggestions.
It works fine in iOS 12 with same Xcode 11 Beta.
Execute the function in the main queue.
DispatchQueue.main.async {
INVoiceShortcutCenter.shared.setShortcutSuggestions(suggestions)
}
after new iOS 10.3 launched, In-Purchases functional stopped working in application suddenly. Actually, this method doesn't call anymore.
func requestDidFinish(request: SKRequest) {
if let receiptURL = NSBundle.mainBundle().appStoreReceiptURL {
if let receipt = NSData(contentsOfURL: receiptURL) {
handleReceipt(receipt)
}
}
Seems the connection to AppleStore is broken.
It was Apple issue, already fixed!
I'm looking for a way to programmatically list any nearby Bluetooth devices (discoverable) that my device finds. I have not been able to find any information or tutorials regarding performing this call in Swift 3.0. This Q-A post discusses finding these devices using Swift 1.0 and building in Xcode 6, rather than the latest version 8.
I did my best to try to make my code into the 3.0 Syntax from the 1.0, but while running the following code, nothing is returned in the Playground:
import Cocoa
import IOBluetooth
import PlaygroundSupport
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
aborted
print("called")
let devices = sender.foundDevices()
for device : Any? in devices! {
if let thingy = device as? IOBluetoothDevice {
thingy.getAddress()
}
}
}
}
var delegate = BlueDelegate()
var inquiry = IOBluetoothDeviceInquiry(delegate: delegate)
inquiry?.start()
PlaygroundPage.current.needsIndefiniteExecution = true
Using IOBluetooth the Correct Way
The following code works flawlessly in Xcode Version 8.2.1 (8C1002), Swift 3.0. There are a few lines that aren't required, such as the entire method of deviceInquiryStarted.
Update: These usages still work as of Xcode 9.2 (9B55) and Swift 4.
Playground
import Cocoa
import IOBluetooth
import PlaygroundSupport
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) {
print("Inquiry Started...")
//optional, but can notify you when the inquiry has started.
}
func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) {
print("\(device.addressString!)")
}
func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) {
//optional, but can notify you once the inquiry is completed.
}
}
var delegate = BlueDelegate()
var ibdi = IOBluetoothDeviceInquiry(delegate: delegate)
ibdi?.updateNewDeviceNames = true
ibdi?.start()
PlaygroundPage.current.needsIndefiniteExecution = true
Project-Application Usage
import Cocoa
import IOBluetooth
import ...
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) {
print("Inquiry Started...")
}
func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) {
print("\(device.addressString!)")
}
}
//other classes here:
//reference the following outside of any class:
var delegate = BlueDelegate()
var ibdi = IOBluetoothDeviceInquiry(delegate: delegate)
//refer to these specifically inside of any class:
ibdi?.updateNewDeviceNames = true
ibdi?.start() //recommended under after an action-button press.
Explanation
The issue I was originally faced with was trying to access the information as the inquiry was still in process.
When I accessed it, under many different occasions my playground would hang and I would be forced to force quit both Xcode.app, and com.apple.CoreSimulator.CoreSimulatorService from the Activity Monitor. I lead myself to believe that this was just a Playground bug, only to learn that my application would crash once the inquiry finished.
As Apple's API Reference states:
Important Note: DO NOT perform remote name requests on devices from delegate methods or while this object is in use. If you wish to do your own remote name requests on devices, do them after you have stopped this object. If you do not heed this warning, you could potentially deadlock your process.
Which entirely explained my issue. Rather than directly asking for the IOBluetoothDevice information from the sender.foundDevices() method (which I believe may not have been updating..?) I simply used the parameters built into the function to mention that it was indeed an IOBluetoothDevice object, and simply to ask for that information to be printed.
Final Note
I hope that this Q/A I've created helps others in need when using IOBluetooth in Swift. The lack of any tutorials and the high amounts of outdated, Objective-C code made finding this information very challenging. I'd like to thank #RobNapier for the support on trying to find the answer to this riddle in the beginning. I'd also like to thank NotMyName for the reply on my post on the Apple Developer Forums.
I will be exploring the usage of this in an iOS device more sooner than later!
I have encountered this issue few times already, and wondering what is wrong there? or this is simply Xcode bug.
Breakpoint is skipped in viewDidLoad, however printing to console works.
override func viewDidLoad() {
super.viewDidLoad()
//set up user data if exist
let User = _UserManager!.currentUser!
if (User.containsDetails()) {
println("DUCK!")
} else {
//no details given yet
ageSlider.value = 2.0
incomeSlider.value = 2.0
}
}
This happened to me with version 6.1. It seems to be a bug, usually closing Xcode and starting it again solves the problem.