Monday, November 21, 2016

Dynamic CollectionView Cell size create based on text length size .



Firstly we need to calculate text length and configure to collection view cell size layout.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        CGSize calCulateSize =[(NSString*)[_categoryList objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
        calCulateSize.width = calCulateSize.width+50; // for better look we add more 50 pixel
        calCulateSize.height = 50; // fixed height
        return calCulateSize;
}



as usual, we implement all of the required collection view delegate and data source, then we see look like collection view cell. demo attached here, if any things need, let me know.


Demo here

Wednesday, November 2, 2016

iOS extension to Host app data share.

Most of the developer struggle data send the extension to Host app, We try to figure out this issue.

Steps:

1. Create  new project ->Xcode->File->New->Project->Single View Application
2. Extension Target add -> File->New->Target->iMessage Extension (also you choose your one)

Host App

1. Info.plist add URL scheme
       <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>url.Identifier</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.avigitsaha.halloweenwallpaperhd.Schemes</string>
            </array>
        </dict>
    </array>

2. AppDelegate.m

 - (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
   
    NSLog(@"options %@",options);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query:1234 %@", [url query]);
   
    NSURLComponents *urlComponents;
    NSArray* queryItems;
   
    // init
    urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
    queryItems = [urlComponents queryItems];
   
    for (NSURLQueryItem* item in queryItems)
    {
        if ([[item name] isEqualToString:@"imageId"]){
            NSLog(@"image Id %@",[item value]);
        }
       
        if ([[item name] isEqualToString:@"categoryId"]){
            NSLog(@"Category Id %@",[item value]);
        }
       
    }
    return NO;
}

Extension App

1. UIButton added for host app code here

- (IBAction)openHostAppWithData:(UIButton *)sender {
    NSString *customURL = [NSString stringWithFormat:@"com.avigitsaha.halloweenwallpaperhd.Schemes://"];
   
    NSURLComponents* urlComponents;
    urlComponents = [NSURLComponents componentsWithURL:[NSURL URLWithString:customURL] resolvingAgainstBaseURL:NO];
   
    [urlComponents setQueryItems:@[[NSURLQueryItem queryItemWithName:@"imageId" value:@"1"],[NSURLQueryItem queryItemWithName:@"categoryId" value:@"2"]
                                   ]];
    [self.extensionContext openURL:[urlComponents URL] completionHandler:^(BOOL success){
        NSLog(@"success %d",success);
    }];
}

Here attached Demo project

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
    }

Sunday, October 9, 2016

Xcode 8 iOS Simulator Activity Logging Tracing


Xcode 8 iOS application run on simulator we see lots of debugging messages, which are call Activity tracing.
Now we are learning, how to Activity logging tracing enable and disable:


Editor Scheme
















argument setting















Now are add environment add (+):
name:OS_ACTIVITY_MODE
value:disable
activity mode set










Now have a look your simulator debugging section, system logging is not showing, If want to deactivate this feature just uncheck, not to remove OS_ACTIVITY_MODE.






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.

Tuesday, September 27, 2016

Email validation in swift


    func validateEmail(emailAddress:String)->Bool{

        let emailRegularExpression = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
       
        let emailValidationPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegularExpression)
       
        return emailValidationPredicate.evaluate(with: emailAddress)
       
    }

Introduction to UIAlertController, Swift 3.0.

    let learningAlertController = UIAlertController(title: "Hello!!!", message: "I'm alert controller", preferredStyle: .alert)

    let okAlertAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction) in
        print("You've pressed OK button");
    }
    learningAlertController.addAction(okAlertAction)
    present(learningAlertController, animated: true, completion: nil)