Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Tuesday, September 1, 2015

Facebook Video Upload on iOS using latest Facebook SDK for iOS(4.x)

 let videoData = NSData(contentsOfURL: NSURL.fileURLWithPath(path))
        
        if (hasGrantedPermission("publish_actions")) {
            
            var params = [String: AnyObject]()
            params["fields"] = ""
            params["video.mov"] = videoData
            params["contentType"] = "video/quicktime"
            params["description"] = "Test"
            FBSDKGraphRequest(graphPath: "/me/videos", parameters: params, HTTPMethod:"POST").startWithCompletionHandler {
                (connection, result, error) -> Void in
            }

        }

Wednesday, July 1, 2015

Detect idle time and do something when timer fires (Swift)

func resetIdleTimer()
    {

        if (idleTimer == nil)
        {
           idleTimer = NSTimer.scheduledTimerWithTimeInterval(kMaxIdleTimeSeconds, target: self, selector: "idleTimerExceeded:", userInfo: nil, repeats: false)
        }
        else
        {
           if (fabs(idleTimer.fireDate.timeIntervalSinceNow) < kMaxIdleTimeSeconds-1.0)
           {
               idleTimer.fireDate = NSDate(timeIntervalSinceNow: kMaxIdleTimeSeconds)
            
           }
        }
    }
    
    func idleTimerExceeded(sender: AnyObject)
    {
        idleTimer = nil

//do something when timer fires
    }

Thursday, June 25, 2015

hide/show with animation in Swift

For hiding the dock like view and show again

 func hide(sender: AnyObject)
 {
     let frame = hideshowView?.frame
     let height = frame?.size.height
     let offsetY = height!
        
     //hide after 10 seconds delay
     if frame != nil {
     UIView.animateWithDuration(0.5, delay: 10.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { [weak self] () -> Void in
                
                if let weakSelf = self
                {
                    weakSelf.segmentedControl?.frame = CGRectOffset(frame!, 0, offsetY)
                    weakSelf.openBottomBarButton.hidden = false
                }
            }) { (Bool v) -> Void in
            }
      }
 }
    
 func show(sender: AnyObject)
 {
        let frame =  hideshowView?.frame
        let height = frame?.size.height
        let offsetY = -height!
        
        //show without delay
        UIView.animateWithDuration(0.5)
        {
            hideshowView?.frame = CGRectOffset(frame!, 0, offsetY)
        
        }
}

Friday, June 19, 2015

Setting ImageView corner radius iOS, Swift

To get rounded image view

imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.layer.masksToBounds = true



Check whether array contains element in Swift

extension Array {
    func contains<T where T : Equatable>(obj: T) -> Bool {
        return self.filter({$0 as? T == obj}).count > 0
    }
}
array.contains(element)
Also can use Dollar Framework: http://www.dollarswift.org

Thursday, June 11, 2015

CocoaPods dependency errors

Analyzing dependencies
[!] Unable to satisfy the following requirements:

- `AFNetworking (= 1.3.4)` required by `Podfile.lock`
- `AFNetworking (~> 1.3.0)` required by `RestKit/Network (0.20.3)`

- `AFNetworking (~> 2.5)` required by `ABMSoundCloudAPI (0.1.0)`

References:
http://blog.cocoapods.org/CocoaPods-0.35/

Solution:

  1. Fork the repository,
  2. Make the changes or update dependencies
  3. Install from the forked repository

Converting Milliseconds to readable Time (hh:mm:ss) format

String convertMS(int ms) {
    int seconds = (int) ((ms / 1000) % 60);
    int minutes = (int) (((ms / 1000) / 60) % 60);
    int hours = (int) ((((ms / 1000) / 60) / 60) % 24);

    String sec, min, hrs;
    if(seconds<10 0="" code="" else="" hours="=" hrs="" if="" min="" minutes="" return="" sec="" seconds="">
In Swift:

func convertMilliSeconds(milliSeconds : NSNumber) -> String
    {
        let seconds = (milliSeconds.integerValue / 1000) % 60
        let minutes = ((milliSeconds.integerValue / 1000) / 60) % 60
        let hours = (((milliSeconds.integerValue / 1000) / 60) / 60) % 24
        
        var sec : String
        var min : String
        var hrs: String
        if seconds<10
        {
            sec = "0\(seconds)"
        }
        else
        {
            sec = "\(seconds)"
        }
        if(minutes<10)
        {
            min = "0\(minutes)"
        }
        else
        {
            min = "\(minutes)"
        }
        if(hours<10)
        {
            hrs = "0\(hours)"
        }
        else
        {
            hrs = "\(hours)"
        }
        
        if(hours == 0)
        {
            return "\(min):\(sec)"
        }
        else
        {
            return "\(hours):\(minutes):\(seconds)"
        }
        

    }

Wednesday, May 27, 2015

[__NSCFNumber length]: unrecognized selector sent to instance

If you get any of these runtime errors/warnings:
[__NSCFNumber length]: unrecognized selector sent to instance
restkit.object_mapping:RKMappingOperation.m: Coercing NSNull value to nil in shouldSetValue:atKeyPath: -- should be fixed
class User: NSObject {
    var userId: String?
    var userImageUrl: String?

}

Check whether server is sending JSON response not as String like
{
  "id":12345
  "avatar_url":""
}

Solution:

Such error causing properties should be defined as NSObject

class User: NSObject {
    var userId: NSObject?
    var userImageUrl: String?

}

Tuesday, May 19, 2015

Overriding isEqual in Swift

class MyClass: NSObject {

    var value = 5

    override func isEqual(object: AnyObject?) -> Bool {
        if let object = object as? MyClass {
            return value == object.value
        } else {
            return false
        }
    }

    override var hash: Int {
        return value.hashValue
    }
}
var x = MyClass()
var y = MyClass() 
x.isEqual(y) // true

Saturday, May 16, 2015

Instagram iOS SDK integration

Get it here
https://github.com/crino/instagram-ios-sdk

Errs:

{ "code": 403, "error_type": "OAuthForbiddenException", "error_message": "Implicit authentication is disabled" }

Follow these steps:

{ "code": 400, "error_type": "OAuthException", "error_message": "Redirect URI does not match registered redirect URI" }

Make sure REDIRECT URI is set to ig[clientId]://authorize
while registering application on Instagram website.




Tuesday, May 12, 2015

Adding Notifications in Swift

Post Notification:

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

Add Observer for Notification in ViewWillAppear:

          
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)


Remove in ViewWillDisappear:
        
NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)Remove in ViewWillDisappear:
Selector Method:

func methodOfReceivedNotification(notification: NSNotification){
    //Action on Notification
}

Tuesday, April 3, 2012

Useful for getting the Table View Cell height containing Text

-(CGFloat)labelHeightForText:(NSString *)text

{

   CGFloat constrainedSize = 231.0; //or any other size

   UIFont *font = [UIFont fontWithName:@"Helvetica" size:17.0];

   CGSize textSize = [text sizeWithFont:font 

                      constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)

                          lineBreakMode:UILineBreakModeWordWrap];

   return textSize.height;

}