Swift Alamofire + Promise catching - swift2

Folks, The following works except for the catch, xcode errors out with expected member name following '.'
Is this the proper way to promisify with PromiseKit?
All suggestions welcome! Thanks!
#IBAction func loginButtonTapped(sender: AnyObject) {
let email = userEmail.text!
let password = userPassword.text!
func onSuccess(success:Bool, message:String, token: String) -> Promise<Void> {
if success {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
self.dismissViewControllerAnimated(true, completion: nil)
} else {
let myAlert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Try Again", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
}
return Promise { resolve, reject in
return resolve()
}
}
func onFailure(error:NSError) -> Promise<Void> {
return Promise { resolve, reject in
return reject(error)
}
}
Login(email, password: password).then(onSuccess).catch(onFailure)
}
private func Login(email: String, password: String) -> Promise<(success:Bool, message:String, token: String)> {
let parameters: [String: String] = [
"username" : email,
"password" : password
];
let endpoint = "https://api.foo.bar/login"
return Promise { resolve, reject in
Alamofire.request(.POST, endpoint, parameters: parameters, encoding: .JSON)
.validate()
.responseJSON { (response) in
guard response.result.error == nil else {
logger.debug(response)
let result:(success:Bool, message:String, token: String) = (false, "Login Failed", "")
return resolve(result)
}
if let value = response.result.value {
let apiResponseJSONBody = JSON(value)
let result:(success:Bool, message:String, token: String) = (true, "", token: apiResponseJSONBody["token"].string!)
return resolve(result)
}
}
}
}

Related

Drag&Drop works with URL, but not with String – why?

In my code the drag&drop function works fine with URL objects. But the exact same code for String objects does not. I have tried with countless casts and loadItem instead of loadObject ... no luck so far.
Can anyone help me here? It would be very much appreciated.
code for URL objects – Working
.onDrag {
return NSItemProvider(object: item.url as NSURL)
}
.onDrop(of: [UTType.url], isTargeted: $isDropping) { providers in
_ = providers.first?.loadObject(ofClass: URL.self) { url, error in
if let error = error { print(error.localizedDescription) }
if let url = url {
DispatchQueue.main.async {
self.array2.insert(Item(title: "new", url: url), at: 0)
}
}
}
return true
}
the same with String does not:
.onDrag {
return NSItemProvider(object: item.title as NSString)
}
.onDrop(of: [UTType.text], isTargeted: $isDropping) { providers in
_ = providers.first?.loadObject(ofClass: String.self) { string, error in
if let error = error { print(error.localizedDescription) }
if let string = string {
DispatchQueue.main.async {
self.array2.insert(Item(title: string, url: URL(string: "http://www.apple.com")!), at: 0)
}
}
}
return true
}
full MRE code:
import SwiftUI
import UniformTypeIdentifiers
struct Item: Identifiable {
let id = UUID()
var title: String
var url: URL
}
struct ContentView: View {
#State var array1: [Item] = [
Item(title: "One", url: URL(string: "http://www.amazon.com")!),
Item(title: "Two", url: URL(string: "http://www.apple.com")!),
Item(title: "Three", url: URL(string: "http://www.example.com")!),
]
#State var array2: [Item] = []
#State var isDropping = false
var body: some View {
HStack(alignment: .top) {
VStack(alignment: .leading) {
ForEach(array1) { item in
Text(item.title)
Text(item.url.absoluteString).foregroundColor(.secondary)
// DRAG
.onDrag {
return NSItemProvider(object: item.url as NSURL) // WORKS
// return NSItemProvider(object: item.title as NSString) // DOES NOT WORK
}
}
}
.frame(maxWidth: .infinity)
Divider()
VStack(alignment: .leading) {
ForEach(array2) { item in
Text(item.title)
Text(item.url.absoluteString).foregroundColor(.secondary)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(isDropping ? .green : .gray)
// DROP with url/NSURL -- WORKS
.onDrop(of: [UTType.url], isTargeted: $isDropping) { providers in
_ = providers.first?.loadObject(ofClass: URL.self) { url, error in
if let error = error { print(error.localizedDescription) }
if let url = url {
DispatchQueue.main.async {
self.array2.insert(Item(title: "new", url: url), at: 0)
}
}
}
return true
}
// DROP with text/NSString -- DOES NOT WORK
.onDrop(of: [UTType.text], isTargeted: $isDropping) { providers in
_ = providers.first?.loadObject(ofClass: String.self) { string, error in
if let error = error { print(error.localizedDescription) }
if let string = string {
DispatchQueue.main.async {
self.array2.insert(Item(title: string, url: URL(string: "http://www.apple.com")!), at: 0)
}
}
}
return true
}
}
}
}
As always thanks to #Asperi and his answer here:
SwiftUI: Not getting dropped NSString value in DropDelegate
This now works:
.onDrop(of: [UTType.utf8PlainText], isTargeted: $isDropping) { providers in
_ = providers.first?.loadItem(forTypeIdentifier: "public.utf8-plain-text") { data, error in
if let error = error { print(error.localizedDescription) }
if let data = data as? Data {
DispatchQueue.main.async {
let string = NSString(data: data, encoding: 4) ?? "failed"
print(string)
self.array2.insert(Item(title: string as String, url: URL(string: "http://www.apple.com")!), at: 0)
}
}
}
return true
}

I want to check text validate when tap button

I want to check text validate when tap button. If validate failure, it will show error text on UILabel and do not send request. Else if validate success, it will send request.
I see many demo about login, but they control button enabled to avoid validate data when tap button. I am puzzled with it.
I have write some code.
class LoginViewModel: BaseViewModel, ViewModelType {
struct Input {
let loginTaps: Driver<Void>
}
struct Output {
let validatedUsername: Driver<Bool>
let validatedPassword: Driver<Bool>
}
let username = BehaviorRelay(value: "")
let password = BehaviorRelay(value: "")
let loginTapped = PublishSubject<Void>()
func transform(input: Input) -> Output {
let validatedUsername = username.asDriver(onErrorJustReturn: "").map { username in
return username.isPhoneNumber
}
let validatedPassword = password.asDriver(onErrorJustReturn: "").map { password in
return password.count > 7
}
input.loginTaps.map { () -> Void in
<#code#>
// I want do check and then do network request
}
input.loginTaps.drive(onNext: { [weak self] () in
self?.loginTapped.onNext(())
}).disposed(by: rx.disposeBag)
loginTapped.flatMapLatest { _ -> Observable<RxSwift.Event<Token>> in
// and if I want to return Bool not Token, how should I do????????????
return self.provider.login(username: self.username.value, password: self.password.value)
.asObservable()
.materialize()
}.subscribe(onNext: { (event) in
switch event {
case .next(let token):
AuthManager.setToken(token: token)
case .error(let error):
log.error(error.localizedDescription)
default: break
}
}).disposed(by: rx.disposeBag)
return Output(validatedUsername: validatedUsername,
validatedPassword: validatedPassword)
}
}
I would expect to see something like this:
class LoginViewModel {
struct Input {
let loginTap: Signal<Void>
let username: Driver<String>
let password: Driver<String>
}
struct Output {
let errorText: Driver<String>
let loginSuccess: Signal<Void>
}
var networkRequest: (URLRequest) -> Observable<Data> = { _ in fatalError("need to replace this with an implementation.") }
func transform(input: Input) -> Output {
func isValidCredentials(username: String, password: String) -> Bool {
return username.isPhoneNumber && password.count > 7
}
let credentials = Driver.combineLatest(input.username, input.password)
// this chain emits when the login button is tapped and the credentials are invalid
let invalidInputError = input.loginTap
.withLatestFrom(credentials)
.filter { !isValidCredentials(username: $0, password: $1) }
.map { _ in "Credentials are invalid" }
.asDriver(onErrorRecover: { _ in fatalError("can't get here") })
let networkRequest = self.networkRequest // to avoid dealing with `self` in the flatMap below
// this chain makes a request if the login button is tapped while the credentials are valid
let loginResult = input.loginTap
.withLatestFrom(credentials)
.filter { isValidCredentials(username: $0, password: $1) }
.map { URLRequest.login(username: $0, password: $1) }
.asObservable()
.flatMapLatest { networkRequest($0).materialize() }
.share(replay: 1)
// this chain emits when the login result produces an error
let loginError = loginResult
.map { $0.error }
.filter { $0 != nil }
.map { $0!.localizedDescription }
.asDriver(onErrorRecover: { _ in fatalError("can't get here") })
// this chain emits when the login result succeeds
let loginSuccess = loginResult
.filter { $0.element != nil }
.map { _ in }
.asSignal(onErrorRecover: { _ in fatalError("can't get here") })
let errorText = Driver.merge(invalidInputError, loginError)
return Output(errorText: errorText, loginSuccess: loginSuccess)
}
}

In change of Swift 2 Extra argument ' error' in call

Upgrade to Xcode 7 Swift 2 and SDK for iOS 9. I get the error "extra argument" error "in call" my code is:
let myUrl = NSURL(string: "http://localhost/SwiftAppAndMySQL/scripts/registerUser.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let postString = "userEmail=\(userEmail)&userFirstName=\(userFirstName)&userLastName=\(userLastName)&userPassword=\(userPassword)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData!, response:NSURLResponse!, error:NSError!) -> Void in
dispatch_async(dispatch_get_main_queue())
{
spinningActivity.hide(true)
if error != nil {
self.displayAlertMessage(error.localizedDescription)
return
}
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary
if let parseJSON = json {
var userId = parseJSON["userId"] as? String
if( userId != nil)
{
var myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(action) in
self.dismissViewControllerAnimated(true, completion: nil)
}
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil)
} else {
let errorMessage = parseJSON["message"] as? String
if(errorMessage != nil)
{
self.displayAlertMessage(errorMessage!)
}
}
}
}
}).resume()
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler ) asks you for 3 optionals arguments and you're giving 3 forceds unwrapping arguments.
try change
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data:NSData!, response:NSURLResponse!, error:NSError!)
to
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?)
Now is working, I replaced the previous code with this::
let myUrl = NSURL(string: "http://dcapp1.testingview.com/DryCleanAppClientes/scripts/registerUser.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let postString = "userEmail=\(userEmail)&userFirstName=\(userFirstName)&userLastName=\(userLastName)&userPassword=\(userPassword)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
print(postString)
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
dispatch_async(dispatch_get_main_queue())
{
spinningActivity.hide(true)
if error != nil {
self.displayAlertMessage(error!.localizedDescription)
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
if let parseJSON = json {
let userId = parseJSON["userId"] as? String
if( userId != nil)
{
let myAlert = UIAlertController(title: "Mensaje", message: "¡Registro exitoso!", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(action) in
self.dismissViewControllerAnimated(true, completion: nil)
}
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil)
} else {
let errorMessage = parseJSON["message"] as? String
if(errorMessage != nil)
{
self.displayAlertMessage(errorMessage!)
}
}
}
} catch _ as NSError {
}
}
}).resume()
}

NSURL error for broken links in Swift

I coded a function for OSX 10.10 that is willing to open a text file from an URL and display its content.
Everything is working but if the URL cannot be reach then the App will crash. How could I handle this type of Error?
I guess it comes from the completionHandler closure but I am not sure.
here is my code
#IBAction func checkAdminMessage(sender: NSMenuItem) {
let messageURL = NSURL(string: "http://www.xxxxxx.com/text.txt")
// The Network stuff will be handled in a background thread
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(messageURL!,
completionHandler: {
(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
// Check if text.txt has NULL as content
if urlContents! == "NULL" {
// Have to use Grand Central Dispatch to put NSAlert in the main thread
let noMessage = NSLocalizedString("Nothing there", comment: "Text to dislay when the file is empty" )
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.displayAlertNotification(notification: noMessage)
}
} else {
// If the file is not empty then we display the content of this file
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.displayAlertNotification(notification: urlContents!)
}
}
})
downloadTask.resume()
}
Thank you
EDIT: Here is the updated code but the App still crashed
#IBAction func checkAdminMessage(sender: NSMenuItem) {
if let messageURL = NSURL(string: "http://www.xxxxxx.com/text.txt") {
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(messageURL,
completionHandler: {
(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil )
if urlContents == "NULL" {
println(urlContents)
// Have to use Grand Central Dispatch to put NSAlert in the main thread
let noMessage = NSLocalizedString("Nothing there", comment: "Text to dislay when the file is empty" )
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.displayAlertNotification(notification: noMessage)
}
}
else {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.displayAlertNotification(notification: urlContents!)
}
}
})
downloadTask.resume()
}
else {
println("Error")
}
}
NSURL(string: ...) returns an optional, so the result may be nil due to several reasons.
Wrap your code in a conditional unwrap:
if let messageURL = NSURL(string: "http://www.xxxxxx.com/text.txt") {
// success
...
}
else {
// error
}
I figured it out with the helps of the people that commented my question.
I was getting a nil from 'location' in downloadTaskWithUrl, then the var urlContents was receiving a nil as well.
The solution is to check if 'location' is nil :
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(messageURL,
completionHandler: {
(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
**if (location != nil) {**
var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil ) ...

Saving an image to special album swift

I want to add a new photo to certain album, I'm trying to do it like that:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
albumName = "\(data.objectAtIndex(indexPath.row))"
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Choose image source", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: {})
}))
actionSheet.addAction(UIAlertAction(title: "Camera Roll", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
var groupToAddTo: ALAssetsGroup = ALAssetsGroup()
self.library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum),
usingBlock: {
(group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumName){
groupToAddTo = group
}
},
failureBlock: {
(myerror: NSError!) -> Void in
println("error occurred: \(myerror.localizedDescription)")
})
var img: CGImageRef = image.CGImage
self.library.writeImageToSavedPhotosAlbum(img, metadata: nil, completionBlock: {
(assetUrl: NSURL!, error: NSError!) -> Void in
if error.code == 0 {
println("saved image completed: \(assetUrl)")
self.library.assetForURL(assetUrl, resultBlock: { (asset: ALAsset!) -> Void in
groupToAddTo.addAsset(asset)
return
}, failureBlock: {
(myerror: NSError!) -> Void in
println("error occurred: \(myerror.localizedDescription)")
})
} else {
println("saved image failed. \(error.localizedDescription) code \(error.code)")
}
} )
}
However, when I run this code, I get this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
in this line:
if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumName){
So, the question is that, how to save an image to certain album in swift?
I am betting albumName is an optional value.
You should put an if statement to judge if almbumName is nil. When it's nil, create a newalbum name on your iPhone.
First:
if group != nil {
if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumNames[indexPath.row]){
groupToAddTo = group
}
}
Second, when error = nil your app will crash because of "if error.code == 0." Do "if error == nil" instead.

Resources