`save(to:for:completionHandler:)` of `UIDocument` crashes - ios

I get crash on every 32 bit device / simulator running iOS 9 on save(to:for:completionHandler:).
Xcode 8.2. Base SDK is 10.2. Target is 9.0. Standard architectures. Swift 3. For both develop and release builds. Sample project.
Could not find if it's known, neither any related issues. Can you recommend any workaround? Should I require 64 bit architecture?

The work around is to return NS object, Apple engineer recommended NSMutableData specifically:
override func contents(forType typeName: String) throws -> Any {
guard let data = text.data(using: .utf8) else { ... }
if #available(iOS 10, *) {
return data
} else {
return NSMutableData(data: data)
}
}

Related

Reading files from an SD card using iOS14

I'm trying to read files from an SD card. The following code works great when my iOS app is run in Xcode's Simulator, but fails to read the directory when i build to the iPad. A similar question was asked 10 years ago and it appears no solution existed back then.
func getFilesfromUSB() {
let filePath = URL(fileURLWithPath: "/Volumes/" + sdCardName + "/FILES")
var fileURLs: [URL] = []
DispatchQueue.global(qos: .userInitiated ).async {
do {
fileURLs = try self.fm.contentsOfDirectory(at: filePath, includingPropertiesForKeys: nil)
print("getting files via USB")
print(fileURLs)
} catch {
print("failed to read directory – bad permissions, perhaps?")
}
DispatchQueue.main.async {
self.parseFiles(from: fileURLs)
}
}
}
How would I go about reading (and writing) to an SD card from iOS 14+?

failed to produce diagnostic for expression, Xcode 11.4, Moya

After updating to Xcode 11.4, I started to get this error
Failed to produce diagnostic for expression please file a bug report
On:
let provider = MoyaProvider<EndPoint>(requestClosure: requestClosure)
I updated Moya but it doesn't look like the issue. Runs fine on previous Xcode versions.
full code:
let requestClosure: MoyaProvider.RequestClosure = {
[unowned self] (endpoint: Endpoint, done: #escaping MoyaProvider.RequestResultClosure) in
guard let request = try? endpoint.urlRequest() else { return }
self.authenticator.authenticate(request, done: { (request) in
done(.success(request))
})
}
provider = MoyaProvider<EndPoint>(requestClosure: requestClosure
I found the issue, Xcode 11.4 compile issues are not verbose enough when it comes to optionals and generics.
Moya version and Swift version were not changed.
An issue with generics:
let requestClosure: MoyaProvider<EndPoint>.RequestClosure
Had to explicitly define the type in the closure, which makes sense, but was not a requirement pre-Xcode version 11.4
let requestClosure: MoyaProvider<EndPoint>.RequestClosure = {
[unowned self] (endpoint: Endpoint, done: #escaping MoyaProvider.RequestResultClosure) in
guard let request = try? endpoint.urlRequest() else { return }
self.authenticator.authenticate(request, done: { (request) in
done(.success(request))
})
}
provider = MoyaProvider<EndPoint>(requestClosure: requestClosure
For me the I was getting that error because one of my property was of type Error and I was assigning a value of type LocalizedError to it. Changing the property type to LocalizedError fixed the problem.
It seems to be an Xcode bug for some cases at least. I renamed a protocol and then renamed it back and the issue was gone.

Build time is too long in Xcode 8.3 with swift 3 in particular file

Im using Xcode 8.3 with Swift 3. I have written one method named pdfFromData(data:) to form the pdf document from the Data, whenever I build my project its not getting build due to this method, means the compiler is got stopped/hanged when it start compile particular file where I coded pdfFromData(data:) method(In Xcode 8.2 with Swift 3 it worked fine). Whenever i comment this method and build means everything working fine.
func pdfFromData(data: Data) -> CGPDFDocument? { // Form pdf document from the data.
if let pdfData = data as? CFData {
if let provider = CGDataProvider(data: pdfData) {
let pdfDocument = CGPDFDocument(provider)
return pdfDocument
}
}
return nil
}
What's wrong with this method?. I want to build my project with this method as well. Thanks in advance.
I tried debugging your issue. This is what I found out:
if let pdfData = data as? CFData {
}
The above line for casting object of type Data to CFData is where it's taking too much time to build.
Replacing that with the following piece of code significantly reduces your build time.
let pdfNsData: NSData = NSData(data: data) // convert `Data` to `NSData`
if let cfPdfData: CFData = pdfNsData as? CFData {
// cast `NSData` to `CFData`
}
NSData and CFData are toll-free bridged.
Please let me know if there's any doubt

alamofire gives exact response in simulator but gives error in device

I'm working on project first time with swift language. I have used alamofire for web service api. I have created a common func for it.
class func postWebService(methodname:String,param:NSDictionary,userName:String,password:String, CompletionHandler:(success:Bool,response:NSDictionary) -> ())
{
let mainlink :String = "mymainurl"
var link = mainlink + methodname
var url:NSURL = NSURL(string: link)!
let plainString = "\(userName):\(password)" as NSString
// example : let plainString = "textuser:testpwd" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64String!]
request(.GET, url, parameters: param as? [String : AnyObject]).responseJSON { (req, res, jsonresp, error) -> Void in
if(error != nil) {
NSLog("Error: \(req) \(error)")
var errDict:NSDictionary = ["message":"\(error?.localizedDescription)"]
CompletionHandler(success: false,response: errDict)
}
else {
var json: NSDictionary = jsonresp! as! NSDictionary
NSLog("%#",json)
CompletionHandler(success: true,response: json)
}
}
}
It is working fine and gives me response when I run it in simulator. But if I load my project in device than It gives me a error
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x17dc4580 {NSDebugDescription=Invalid value around character 0.})
If there is a problem in code than it should not work in simulator also. And it is nice and working code for web service api. Any help will be appreciated. Thanks in advance.
==== EDIT====
Mention : it is server url.
I figured out one more thing. If I run this code in ios 8 and above device it is working fine. And this problem is occurred in ios 7 device. I'm dealing with iPod with ios7. I have checked in ios8 devices like iPhone and iPad, it is working fine.
May or not be the case with you but I once had that error when accessing a url that was not correct. In my case it was pointing to localhost, which was perfect for the simulator but complete useless on the device.
Double check your URL, make sure the web service is online and can be reached from your device...
Hope this helps, if not others will chime in ;)
I have researched for this and mainly I checked all information of Alamofire on github. In this page I have found solution and it is my main and big mistake is that Alamofire have not support of ios 7. It Requires ios 8+.
Above My code is right and good for web service api. There is no problem in URL or in Code. My problem is for ios 7.0 which is not supported so this problem occured.
So, It is my own mistake, not of Alamofire Framework or code. But is should support ios7 because we can't ignore ios version 7 for our projects. There are may be lots of devices having ios7. Thanks for all support.

How do I conditionally use a class in Swift that may not be present in the targeted version of the OS?

This answer addresses how to use a Swift class from a framework that may not be linked to an app, but is present in the target OS. Relatedly, but a bit different, I'd like to know how to conditionally use a Swift class that may not be included in the target OS. For example, say I have an app, written in Swift, that targets iOS 7. I want to use WKWebView if I'm on iOS 8, and UIWebView if I'm on iOS 7. This doesn't work:
if NSClassFromString("WKWebView") == nil {
var wv = WKWebView()
} else {
var wv = UIWebView()
}
Because my app targets iOS 7, I get a compile-time error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_WKWebView", referenced from:
__TMaCSo9WKWebView in MyWebKitViewController.o
I tried just assigning the WKWebView to an AnyObject property, but then I can’t call methods on it. So how can I get Swift to just check for these things at runtime?
I'm new to Swift so I don't know if this is Swift or not, but I found this on a different question. https://stackoverflow.com/a/25874127/1012144
http://floatlearning.com/2014/12/uiwebview-wkwebview-and-tying-them-together-using-swift/
if (NSClassFromString("WKWebView") != nil) {
// Use WebKit
} else {
// Fallback on UIWebView
}
The code in the OP is nearly correct; it just needed to use != instead of ==:
if NSClassFromString("WKWebView") != nil {
var wv = WKWebView()
} else {
var wv = UIWebView()
}
To make it work, be sure to set iOS Deployment Target to 7.0. Otherwise you get the undefined symbol errors.

Resources