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.

Thursday, January 24, 2013

Twitter for iOS6 without the modal view.

Hi,
I guess I got this one from some Stackoverflow question.
Link against Social.framework and accounts.framework.
 #import <Accounts/Accounts.h>  
 #import <Social/Social.h>  
 =========================================
 ACAccountStore *account = [[ACAccountStore alloc] init];  
 ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:    
           ACAccountTypeIdentifierTwitter];  
       
 [account requestAccessToAccountsWithType:accountType options:nil   
         completion:^(BOOL granted, NSError *error)  
 {  
    if (granted == YES)  
    {  
      NSArray *arrayOfAccounts = [account   
          accountsWithAccountType:accountType];  
   
      if ([arrayOfAccounts count] > 0)  
      {  
       ACAccount *twitterAccount = [arrayOfAccounts lastObject];  
   
       NSDictionary *message = @{@"status": @”My First Twitter post from iOS6”};  
         
       NSURL *requestURL = [NSURL   
        URLWithString:@"http://api.twitter.com/1/statuses/update.json"];  
   
       SLRequest *postRequest = [SLRequest   
         requestForServiceType:SLServiceTypeTwitter  
             requestMethod:SLRequestMethodPOST  
             URL:requestURL parameters:message];  
   
       postRequest.account = twitterAccount;  
   
       [postRequest performRequestWithHandler:^(NSData *responseData,   
             NSHTTPURLResponse *urlResponse, NSError *error)  
       {  
          NSLog(@"Twitter HTTP response: %i", [urlResponse   
             statusCode]);  
       }];  
      }  
    }  
 }];  
That's it, don't hesitate to comment, to share your knowledge and to correct me. 
Hi,
I found this here.
Link the project against Twitter.framework and accounts.framework.
 #import <Twitter/Twitter.h>  
 #import <Accounts/Accounts.h>  
 =============================================  
 - (void)postToTwitter   
  {   
   // Create an account store object.   
   ACAccountStore *accountStore = [[ACAccountStore alloc] init];   
     
   // Create an account type that ensures Twitter accounts are retrieved.   
   ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:  
   ACAccountTypeIdentifierTwitter];   
     
   // Request access from the user to use their Twitter accounts.   
   [accountStore requestAccessToAccountsWithType:accountType   
   withCompletionHandler:^(BOOL granted, NSError *error) {   
    if(granted) {   
     // Get the list of Twitter accounts.   
     NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];   
     
     if ([accountsArray count] > 0) {   
      // Grab the initial Twitter account to tweet from.   
      ACAccount *twitterAccount = [accountsArray objectAtIndex:0];   
      TWRequest *postRequest = nil;   
     
      postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:  
             @"http://api.twitter.com/1/statuses/update.json"]   
             parameters:[NSDictionary dictionaryWithObject:  
             @"The message to post" forKey:@"status"]   
             requestMethod:TWRequestMethodPOST];   
     
      // Set the account used to post the tweet.   
      [postRequest setAccount:twitterAccount];   
     
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {   
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {   
        dispatch_async(dispatch_get_main_queue(), ^(void) {   
         if ([urlResponse statusCode] == 200) {   
          NSLog(@"Successful");   
         }else {   
     
         NSLog(@"Failed");   
         }   
        });   
       }];   
      });   
     
     }   
     else   
     {   
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];   
     }   
    }   
   }];   
  }
That's it, don't hesitate to comment, to share your knowledge and to correct me.