Friday, May 9, 2014

How to force the View Pager to wrap its content.

The problem is that the "wrap_content" doesn't seem to be working with the View pager.
Here is the solution:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context) {
        super(context);
    }

public CustomViewPager(Context context, AttributeSet attrs){

        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = 0;
        for(int i = 0; i < getChildCount(); i++) {

            View child = getChildAt(i);

            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

            int h = child.getMeasuredHeight();

            if(h > height) height = h;

        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

reference:
http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content

That's it, don't hesitate to comment, to share your knowledge and to correct me.

Dimming the background behind the dialogs

I'm just reblogging what you can find here.
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); 
lp.dimAmount=0.78f; 
dialog.getWindow().setAttributes(lp); 
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
That's it, don't hesitate to comment, to share your knowledge and to correct me.

Friday, September 27, 2013

Re-sign a .IPA file

The problem:
  1. To re-sign a .ipa file with a different distribution provisioning profile.
  2. To change the bundle identifier.
Required files:
  1. The .ipa file.
  2. The distribution provisioning profile. (.mobileprovision)
  3. Entitlements.plist
Put all the files under the same directory.

Create "Entitlements.plist":
Copy the following code in a text file and make sure that the extension is (.plist).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>application-identifier</key>
    <string>kjhdfsui.com.your.bundle.id</string>
    <key>get-task-allow</key>
    <false/>
</dict>
</plist>
To get the bundle-id value used in "Entitlements.plist" (the previous piece of code), open (.mobileprovision) using an editor and copy the value under "Entitlements".

Re-sign:
  1. Unzip the .ipa file. in the terminal type
    unzip MyApp.ipa
  2. Inside the (Payload) folder, you'll find the .app file. Open it by right click > show package content.
  3. find the (info.plist) file and open it using Xcode. replace the old bundle identifier with the new one.
  4. Open the terminal and move to the directory that contains the (.ipa) file.
  5. Remove the old code signature
    rm -r "Payload/MyApp.app/_CodeSignature" "Payload/MyApp.app/CodeResources" 2> /dev/null | true
    
  6. Replace the embedded mobile provisioning profile
    cp "MyEnterprise.mobileprovision" "Payload/MyApp.app/embedded.mobileprovision"
    
  7. Re-sign
    /usr/bin/codesign -f -s "iPhone Distribution: Certificate Name" --resource-rules "Payload/MyApp.app/ResourceRules.plist" --entitlements Entitlements.plist "Payload/MyApp.app"
    
  8. Re-package
    zip -qr "MyApp.resigned.ipa" Payload
    
  9. To submit the app:
    1. Create an app in itunes connect.
    2. Use "Application loader" to upload the app.
For some reason you may face this error "Codesign object file format invalid or unsuitable" in step #7. to solve it, type the following in the terminal:
export CODESIGN_ALLOCATE="/Applications/Xcode.app/Contents/Developer/usr/bin/codesign_allocate"

If you have many versions of Xcode, then you'll need to specify a version that contains "codesign_allocate".

Sunday, August 4, 2013

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://graph.facebook.com/"]];
    
    NSString *link = [NSString stringWithFormat:@"https://graph.facebook.com/%@/likes/%@", USER_ID, PAGE_ID];
    
    NSDictionary *params = @{@"access_token" : [[[FBSession activeSession] accessTokenData] accessToken]};
    
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                            path:link
                                                      parameters:params];
    
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON ) {
        
        if([[JSON objectForKey:@"data"] count] == 0){
            
            // The page is not liked.
        }
        else{
            
            // The user likes the page.
        }
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        
                                             
    }];

    [operation start];
That's it, don't hesitate to comment, to share your knowledge and to correct me.

Friday, August 2, 2013

iOS, Set CoreData up.

1- Link against CoreData.framework
2- add the following to the appDelegate.h
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
3- add the following to the appDelegate.m
#import <coredata/coredata.h>
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"database" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
    
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.
         
         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
         
         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.
         
         
         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
         
         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
         
         * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
         [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
         
         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
         
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    
    
    return _persistentStoreCoordinator;
}
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
That's it, don't hesitate to comment, to share your knowledge and to correct me.

iOS email validator

-(BOOL) isValidEmail:(NSString *)checkString
{
    BOOL stricterFilter = YES; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}
That's it, don't hesitate to comment, to share your knowledge and to correct me.

Friday, April 5, 2013

NSURL *baseURL = [NSURL URLWithString:@"https://graph.facebook.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

NSString *link = [NSString stringWithFormat:@"/%@/likes", @"OBJECT_ID"];
NSDictionary *params = @{@"access_token" : [[[FBSession activeSession] accessTokenData] accessToken]};

[httpClient postPath:link parameters:params success:^(AFHTTPRequestOperation *op, id result) {

    NSLog(@"result %@", result);

  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"error %@", error);

}];
That's it, don't hesitate to comment, to share your knowledge and to correct me.