Showing posts with label Swift. Show all posts
Showing posts with label Swift. Show all posts

Tuesday, January 17, 2017

Swift some important note.

Who are swift struggling to comment, pragma, .warning, to do or other imports note to write, which one to next time or fix some issue, it's embarrassing to who already working on Objective-C, here we go with some swift important slip.

Comment:

Single line: //
Multi Line: /* here your commommnets*/

Pragma or methods section mark which Objective-C is called #pragma

//MARK:UITableViewDelegate

Warning:

//TODO: this method complete next day 

Logical change or need to fix somethings then:

//FIXME: logical error here, or should update depreciated

Thursday, October 13, 2016

How do I prevent files from being backed up to iCloud and iTunes from my iOS app using Swift or ObjectiveC



Objective C:

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
   
    NSError *error = nil;
    BOOL success = [URL setResourceValue:[NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

Swift 3.0:

    func addSkipBackupAttributeToItemAtURL(filePath:String) -> Bool
       
    {
       let URL:NSURL = NSURL.fileURL(withPath: filePath) as NSURL
        assert(FileManager.default.fileExists(atPath: filePath), "File \(filePath) does not exist")
        var success: Bool
       
        do {
            try URL.setResourceValue(true, forKey:URLResourceKey.isExcludedFromBackupKey)
            success = true
        } catch let error as NSError {
           success = false
          print("Error excluding \(URL.lastPathComponent) from backup \(error)");
       }
        return success
    }

Thursday, September 29, 2016

UIimagePickerController Example using Swift 3

UIimagePickerController using we grab photo from photo library or take a picture using camera.

At first we need to add some protocol for UIImagePickerController like UIImagePickerControllerDelegate and UINavigationControllerDelegate on our ViewController.

Looks like
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
}

Image Picker Setup for photo library and camera

    //MARK: Action
    @IBAction func imagePickFromPhotoLib(_ sender: UIButton) {
        let imagePickerController = UIImagePickerController()
        // imagePickerController.allowsEditing = true
        imagePickerController.delegate = self
        imagePickerController.sourceType = .photoLibrary
        present(imagePickerController, animated: true, completion: nil)
    }

    @IBAction func imagePickFromCamera(_ sender: UIButton) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.sourceType = .camera
        imagePickerController.allowsEditing = true
        imagePickerController.cameraDevice = .front
        imagePickerController.cameraFlashMode = .auto
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }

Then we need to implement delegate

   //MARK: UIImagePickerDelegate
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo  info: [String : Any]) {
       
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        // picker selected image use on what ever we need
        picker.dismiss(animated: true, completion: nil)
    }

Info.plist file add those key 

Privacy - Camera Usage Description
Privacy - Photo Library Usage Description


Here demo project on git.