Not able to access Contacts in iOS7 - iphone

I am using the following code to get the contacts from the User's Phone
CFErrorRef error;
__block BOOL accessGranted;
ABAddressBookRef myAddressBook = ABAddressBookCreateWithOptions(NULL,&error);
ABAddressBookRequestAccessWithCompletion(myAddressBook, ^(bool granted, CFErrorRef error)
{
if (!accessGranted && !granted)
{
alertViewDeny = [[UIAlertView alloc]initWithTitle:#"Deny Access" message:#"Deny" delegate:self cancelButtonTitle:nil otherButtonTitles:#"cancel", nil];
[alertViewDeny show];
[alertViewDeny release];
}
else
{
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);
DLog(#"allPeople %#",allPeople);
}
In Output I am getting //Only for iOS7 and it is working iOS6.
allPeople( )

Related

EXC bad access while selecting twitter account from actionsheet in iOS 5

I'm now currently fetching the twitter account configured in the device and display in action sheet and when I select a account I get exc bad access error. It crashes in the actionsheet delegate method where I print the twitterArray
#interface LogInViewController ()
{
NSArray *twitterAccountsArray;
ACAccount *selectedAccount;
}
-(void)getTwitterAccountsForLogin
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted && !error) {
twitterAccountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(#"Account:%#",twitterAccountsArray);
if(twitterAccountsArray == nil || [twitterAccountsArray count] == 0)
{
showAlert(#"Message", TWITTER_NO_ACCOUNT_ERROR , STRING_CONSTANT_OK, nil);
}
else if ([twitterAccountsArray count] > 1)
{
[self performSelectorOnMainThread:#selector(accountListActionSheetDynamics:) withObject:twitterAccountsArray waitUntilDone:YES];
}
else
{
NSLog(#"Only one twitter account so login to that");
}
}
else
{
showAlert(#"Message", TWITTER_ACCESS_DENIED, STRING_CONSTANT_OK, nil);
}
}];
}
- (void)accountListActionSheetDynamics:(NSArray *) accounts {
UIActionSheet *accountsSheet = [[UIActionSheet alloc]
initWithTitle:#"Choose a Twitter Account"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[accountsSheet setDelegate:self];
for(int i=0;i<accounts.count;i++)
{
[accountsSheet addButtonWithTitle:[[accounts objectAtIndex:i] valueForKey:#"username"]];
}
accountsSheet.cancelButtonIndex = [accountsSheet addButtonWithTitle:#"Cancel"];
[accountsSheet showFromRect:self.view.bounds inView:self.view animated:YES];
NSLog(#"Account:%#",twitterAccountsArray);
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex)
{
return;
}
else
{
//Crashes here with exc bad access while printing log
NSLog(#"Account:%#",twitterAccountsArray);
for(int i=0;i<twitterAccountsArray.count;i++)
if([[actionSheet buttonTitleAtIndex:buttonIndex] caseInsensitiveCompare: [[twitterAccountsArray objectAtIndex:i] valueForKey:#"username"]]==NSOrderedSame)
{
selectedAccount = [twitterAccountsArray objectAtIndex:i];
}
}
}
The NSLog(#"Account:%#",twitterAccountsArray); prints fine in the action sheet method and the initial twitter method but crashes in the action sheet delegate method. Any ideas. This happens only in iOS5 works fine for iOS6 and above
I was able to solve the issue by making accountStore and accountType global
#interface LogInViewController ()
{
NSArray *twitterAccountsArray;
ACAccount *selectedAccount;
ACAccountStore *accountStore;
ACAccountType *accountType;
}

Multiple objects in PFQueryTableViewController - Parse.com

I'm trying to display two objects or "classNames" into a PFQueryTableViewController. Here is my code so far with only one object. I can't seem to be able to add more than one object.
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Customize the table
// The className to query on
self.className = #"Funny";
//self.className = #"Story";
// The key of the PFObject to display in the label of the default cell style
self.textKey = #"title";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 100;
}
return self;
}
Just add more #propertys to your view controller. Make sure to add the necessary ones (className2, textKey2, etc) and to modify the datasource methods of your table view to display the data.
That being said, it seems strange the the view controller is initiated with initWithCoder. That is usually the method invoked by storyboard for views.
I used two object when saving the post. It worked perfectly!
PFObject *quoteNew = [PFObject objectWithClassName:#"New"];
[quoteNew setObject:[[self attribution] text] forKey:#"by"];
[quoteNew setObject:[[self quoteText] text] forKey:#"quoteText"];
[quoteNew setObject:[[self attributionTitle] text] forKey:#"title"];
[quoteNew saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self done:self];
} else {
[[[UIAlertView alloc] initWithTitle:#"Uh oh. Somthing went wrong"
message:[[error userInfo] objectForKey:#"error"]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil] show];
}
}];
PFObject *quote = [PFObject objectWithClassName:#"Funny"];
[quote setObject:[[self attribution] text] forKey:#"by"];
[quote setObject:[[self quoteText] text] forKey:#"quoteText"];
[quote setObject:[[self attributionTitle] text] forKey:#"title"];
[quote saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self done:self];
} else {
[[[UIAlertView alloc] initWithTitle:#"Uh oh. Somthing went wrong"
message:[[error userInfo] objectForKey:#"error"]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil] show];
}
}];
}

how to make login page in iphone using sqlite

I am trying to create to create login page in iPhone using sqlite.
I need suggestions ,how to create login form with username /password authentication. For successful login - user should navigate to next page like UITableview.Otherwise giving user a error message: login failed. Please help me. I am new to the Programming.
First you create registration page and after this code apply in your login view..
if([uername.text isEqualToString:#""] || [password.text isEqualToString:#""]) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Here comes a block." message:#"You missed something, please check your details and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
else
{
//insert data
NSString *query=[NSString stringWithFormat:#"select * from registration_table where username=\"%#\" and password=\"%#\"",uername.text,password.text];
NSLog(#"%#",query);
//select * from registration_tbl where username="d" and password="d"
array=[app.sk lookupAllForSQL:query];
NSLog(#"%#",[array description]);
if (array.count>0)
{
app.currrentid=[[array objectAtIndex:0]objectForKey:#"uid"];
NSLog(#"%#",app.currrentid);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"" message:#"login successful" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
messageviewcontroller *log = [[messageviewcontroller alloc]initWithNibName:#"messageviewcontroller" bundle:nil];
[self.navigationController pushViewController:log animated:YES];
[log release];
log.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:log animated:YES];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Here comes a block." message:#"angry" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
uername.text=#"";
password.text=#"";
//push the view
}
i hope this code useful for you.
Create a table for storing username and password
Input the value such that password is encrypted.
Make a login page with Textfield for username and password
On button action collect the username and password from textfield
.
-(IBAction)loginbuttonPressed:(id)sender
{
if (check username is in table == success)
{
//encrypt the password entered compare and check for password is true for the username in table
if(check password is in table= password found success)
{
// Successfully logged in
}
else
{
//alert "Password Incorrect"
}
}
else
{
// Alert "Username not found"
}
}
First of create 1 sample database with User Table with UserName & Password fields.
Then, copy database into application while installing first time.
- (void) copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"dbSample.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
/********* Database Path *********/
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"dbSample.sqlite3"];
}
Then, check on Validate Button Click,
-(IBAction)validateUser:(id)sender
{
if (sqlite3_open([[appDel getDBPath] UTF8String], &dbTest) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"SELECT UserName, Password FROM tblUser Where UserName = \"%#\"",[txtUserName text]];
NSLog(#"%#",querySQL);
const char *sql = [querySQL UTF8String];
sqlite3_stmt *searchStatement;
if (sqlite3_prepare_v2(dbTest, sql, -1, &searchStatement, NULL) == SQLITE_OK)
{
if (sqlite3_step(searchStatement) == SQLITE_ROW)
{
infoUserName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(searchStatement, 0)];
infoUserPassword = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(searchStatement, 1)];
NSLog(#"User : %#, Password : %#",infoUserName, infoPassword);
}
}
sqlite3_finalize(searchStatement);
}
sqlite3_close(dbTest);
}
May be, it'll help you.
Thanks.
First thing you need to do is add libsqlite3.dylib to your list of frameworks.
As a Second thing you should start reading the SQLite3 documentation, especially the five-minute guide. If you want some reference to understand how to "Save Data to the SQLite Database" and "Extract Data from the SQLite Database" , you can always check the Beautiful Tutorial : An Example SQLite based iPhone Application
Third and Most Importing thing you need to do is : Read Above Links ( Reference to Line in your Question I AM new to the Programming ).
At last, I cans say you : Good Luck !!!

Check ALAssetsLibrary empty or not

I am using ALAssetsLibrary and app getting crashed when it is empty.How do i check whether it is empty or not?.
-(void)getLastImageName1
{
// if (val < 10) {
// NSLog(#"getLastImageName1\n");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
last =1;
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (last == 1) {
// NSLog(#"last\n");
last++;
// Chooses the photo at the last index
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
NSString *lastImgNew = alAsset.defaultRepresentation.filename;
// NSLog(#"current img name %#\n",lastImgNew);
NSString *plistPath1 = [DOCUMENT_DIR_PATH stringByAppendingPathComponent:#"previouslastimagename.plist"];
NSArray *lastImg = [NSArray arrayWithContentsOfFile:plistPath1];
// NSLog(#"get pre lastimg %#\n",lastImg);
// NSArray *lastImg = [[DBModel database]getPreviousName];
// NSLog(#"get lastImg %#\n",lastImg);
if ([lastImg count] != 0) {
// NSLog(#"count\n");
if ([[lastImg objectAtIndex:0] isEqualToString:lastImgNew]) {
// NSLog(#"img eql\n");
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"" message:#"equal" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
// [alert show];
[self hideImage];
// }
}
else
{
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"" message:[NSString stringWithFormat:#"pre %# current %#",[lastImg objectAtIndex:0],lastImgNew] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
// [alert show];
// NSLog(#"img not eql\n");
[self performSelectorOnMainThread:#selector(displayAlert) withObject:nil waitUntilDone:YES];
}
}
}
}];
}
} failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
// NSLog(#"No groups");
}];
The line which after last++ variable.I am checking lastimage with newscreenshot image to not allow user to use app after taking screenshot
This is the line that is giving you the error:
[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]
If there are zero assets then you are setting the index to -1. You can wrap that enum block in a conditional to test if numberOfAssets > 0 before proceeding. Also, there are other questions you may find helpful to look at here. Ex.: How to get the latest photo in iPhone Library?

Identifying the Push Notification Message

In my project I want to show the events and offers through push notification, but the problem is, I'm able to show the events or offers, not both. Is there any way to identify the message of Push notification. Here's the code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:#"alert"];
if ([alert isKindOfClass:[NSString class]]) {
message = alert;
} else if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:#"body"];
}
if (alert) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"AThe message." delegate:self
cancelButtonTitle:#"button 1"
otherButtonTitles:#"button", nil];
[alertView show];
}
NSString *contentsInfo = [userInfo objectForKey:#"contTag"];
NSLog(#"Received contents info : %#", contentsInfo);
NSString *nibName = [AppDelegate fetchNibWithViewControllerName:#"EventsViewController"];
EventsViewController *evc = [[EventsViewController alloc] initWithNibName:nibName bundle:nil];
evc.newEvent = YES;
[self.navigationController pushViewController:evc animated:YES];
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:#"aps"] objectForKey: #"badgecount"] intValue];
}
alert is always an NSDictionary with two keys: body and show-view. The value of the former is the alert message and the latter is a Boolean (false or true). If false, the alert’s View button is not shown. The default is to show the View button which, if the user taps it, launches the application.
check the docs
To identify type of the message you can provide additional fields, as described here
Example:
{
"aps":{
"badge":1,
"alert":"This is my special message!",
"mycustomvar1":"123456",
"mycustomvar2":"some text",
"myspecialtext":"This is the best!",
"url":"http://www.mywebsite.com"
}
}