Monday, March 10, 2014

iOS Keychain: Sharing data between iOS apps !

Save data in keychain from one iOS app and access it from another iOS app (Xcode 5) !


Creating your first app that will write to the Keychain


Settings for project : 
  1. Add the Security.framework to your project.
  2. Download UICKeyChainStore  and add the UICKeyChainStore.h and UICKeyChainStore.m files from Lib folder to your project.
  3. Go to Target -> Capabilities -> Keychain Sharing. By default it is off. On this option.
  4. As you will on it, a file name projectName.entitlements will add in your "Project Navigator", with key "Keychain Access Groups" of type "Array" with one "Item" of type "String" and Value "$(AppIdentifierPrefix)$(CFBundleIdentifier)". For example : com.my.firstApp is the Bundle identifier then Value will be "$(AppIdentifierPrefix)com.my.firstApp". It will look like this : 



Now all you need to do is start using the UICKeyChainStore and you'll be saving data to the KeyChain.

Now, in your class import "UICKeyChainStore.h" and write the following code: 

[UICKeyChainStore setString:@"mySecretPassword" forKey:@"password" service:@"MyService"];

[UICKeyChainStore stringForKey:@"password" service:@"MyService"];

Specifying the service allows you to store keys for a particular service, maybe you have a Twitter or Facebook specific set of keys you wish to save.

As you can see we are not specifying the accessGroup. By default it will pick the first access-group specified in your projectName.entitlements when writing and will search across all access-groups when none is specified.

Allowing your second app to access your shared keychain data:

Settings for project : 
  1. Repeat above steps from 1 to 3 (for firstApp). It will look like this : 



We want access to the Keychain group $(AppIdentifierPrefix)com.my.firstApp for reading, but we still want the default writing to occur in our own keychain group so we still specify $(AppIdentifierPrefix)com.my.secondApp as the first / default group. Add one more item in "Keychain Access Groups" with Value $(AppIdentifierPrefix)com.my.firstApp

It will look like this: 


Now, read the value from the shared Keychain exactly how we did in our first app.

In your class import "UICKeyChainStore.h" and write the following code: 

[UICKeyChainStore stringForKey:@"password" service:@"MyService"];

This should return the value “mySecretPassword” :)


Happy Coding :)




Tuesday, October 1, 2013

Send Mail with multiple attachments to multiple recipients through AppleScript !



#Read file for mail body
set filePath to "/Users/......../MailBodyText.txt"
set theContent to (read filePath)

                 OR

#Compose mail body
set theContent to "Hi,

New build is available. Please find the details below:
Version: 1.1.1
URL:http://...........ipa

Detailed release note is attached for your reference.

Regards,
Dev-Team
"

#set file for attachment
set theAttachment1 to POSIX file "/Users/............/release_notes.xls"

#set Another attachment for latest log file
set attachFilePath to "/Users/....../builds/" & LatestFolderName & "/log"
set theAttachment2 to POSIX file attachFilePath


#Set Recipients
set nameList to {"Archana Chaurasia", "Recipient2", "Recipient3"}
set addressList to {"archana.2207@gmail.com", "Recipient2@test.com", "Recipient3@test.com"}

#Mail Tell Block
tell application "Mail"
           #Create the message
           set theMessage to make new outgoing message with properties {subject:releaseVersioncontent:theContentvisible:true}
           #Set a recipient
           tell theMessage
              make new attachment with properties {file name:theAttachment1at after the last paragraph
              make new attachment with properties {file name:theAttachment2at after the last paragraph
              repeat with i from 1 to count nameList
                        make new to recipient at end of to recipients with properties {name:item i of nameListaddress:item i of addressList}
              end repeat
#Send the Message
            send
            end tell

end tell