App link: https://drive.google.com/open?id=0BwU5Y4Xqfa42aHdoNTNxNWVkd3M
appdelegate.h
appdelegate.h
#import <UIKit/UIKit.h>
#import "PayPalMobile.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property NSMutableArray *aryItems,*imageItems;
@end
appdelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[PayPalMobile initializeWithClientIdsForEnvironments:@{PayPalEnvironmentProduction : @"YOUR_CLIENT_ID_FOR_PRODUCTION", PayPalEnvironmentSandbox : @"YOUR_CLIENT_ID_FOR_SANDBOX"}];
_aryItems=[[NSMutableArray alloc]init];
_imageItems=[[NSMutableArray alloc]init];
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
view.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITabBarDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableV;
@property NSMutableArray *ary,*imageAry,*disAry,*mainAry,*mainImageAry;
@property NSMutableDictionary *dic;
@property NSURLSessionDataTask *data;
@property NSURLRequest *urlFreeReq;
@property NSURLSession *urlSession;
@property (strong, nonatomic) IBOutlet UITabBarItem *cartTab;
@end
view.m
#import "ViewController.h"
#import "customTableViewCell.h"
#import "AppDelegate.h"
#import "cartViewController.h"
@interface ViewController (){
AppDelegate *ad;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ad=[[UIApplication sharedApplication]delegate];
_tableV.delegate=self;
_tableV.dataSource=self;
_ary=[[NSMutableArray alloc]init];
_imageAry = [[NSMutableArray alloc]init];
_disAry=[[NSMutableArray alloc]init];
_mainAry=[[NSMutableArray alloc]init];
_dic=[[NSMutableDictionary alloc]init];
_urlFreeReq=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://itunes.apple.com/in/rss/topfreeapplications/limit=10/json"]];
_urlSession=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
_data=[_urlSession dataTaskWithRequest:_urlFreeReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
_dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (int i=0; i<9; i++) {
[_ary addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:name"]objectForKey:@"label"]];
NSLog(@"%@",self.ary);
NSString *imageUrl=[[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSData *data1=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
[self.imageAry addObject:[UIImage imageWithData:data1]];
[_disAry addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"summary"]objectForKey:@"label"]];
[_tableV reloadData];
}
dispatch_async(dispatch_get_main_queue(), ^{
[_tableV reloadData];
});
}];
[_data resume];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_ary count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier=@"cell";
customTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell.addToCartBtn addTarget:self action:@selector(addCart:) forControlEvents:UIControlEventTouchUpInside];
}
NSLog(@"%@",_ary);
if (_ary.count>0) {
cell.nameL.text=[NSString stringWithFormat:@"%lu %@",(indexPath.row +1),[self.ary objectAtIndex:indexPath.row]];
cell.imageV.image=[_imageAry objectAtIndex:indexPath.row];
cell.addToCartBtn.selected=[self.mainAry containsObject:_dic];
cell.addToCartBtn.selected=[self.mainImageAry containsObject:_dic];
[cell.addToCartBtn setTitle:[NSString stringWithFormat:@"%li",(long)indexPath.row] forState:UIControlStateDisabled];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 123;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addCart:(UIButton*)sender {
// access the row number using the used trick
NSUInteger index = [[sender titleForState:UIControlStateDisabled] integerValue];
// obtain the object to be removed/added into cart array
NSDictionary *obj = [self.ary objectAtIndex:index];
// create indexpath
NSIndexPath *ip = [NSIndexPath indexPathForRow:index inSection:0];
//image to cart
UIImage *cartImg=[self.imageAry objectAtIndex:index];
// perform action
if(sender.selected) {
// remove selected array
[self.mainAry removeObject:obj];
[ad.imageItems removeObject:cartImg];
// update badge number
[self reloadBadgeNumber];
} else {
// add into selected array
[self.mainAry addObject:obj];
[ad.imageItems addObject:cartImg];
// perform add to cart animation
[self addToCartTapped:ip];
}
// reload specific row with animation
[self.tableV reloadRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationMiddle];
}
- (void)addToCartTapped:(NSIndexPath*)indexPath {
// grab the cell using indexpath
customTableViewCell *cell = [self.tableV cellForRowAtIndexPath:indexPath];
// grab the imageview using cell
// UIImageView *imgV = (UIImageView*)[cell viewWithTag:kCCell_Img];
// get the exact location of image
CGRect rect = [cell.imageV.superview convertRect:cell.imageV.frame fromView:nil];
rect = CGRectMake(5, (rect.origin.y*-1)-10, cell.imageV.frame.size.width, cell.imageV.frame.size.height);
NSLog(@"rect is %f,%f,%f,%f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
// create new duplicate image
UIImageView *starView = [[UIImageView alloc] initWithImage:cell.imageV.image];
[starView setFrame:rect];
starView.layer.cornerRadius=5;
starView.layer.borderColor=[[UIColor blackColor]CGColor];
starView.layer.borderWidth=1;
[self.view addSubview:starView];
// begin ---- apply position animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration=0.65;
pathAnimation.delegate=self;
// tab-bar right side item frame-point = end point
CGPoint endPoint = CGPointMake(210+rect.size.width/2, 390+rect.size.height/2);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, starView.frame.origin.x, starView.frame.origin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, starView.frame.origin.y, endPoint.x, starView.frame.origin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
// end ---- apply position animation
// apply transform animation
CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"];
[basic setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.25, 0.25, 0.25)]];
[basic setAutoreverses:NO];
[basic setDuration:0.65];
[starView.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
[starView.layer addAnimation:basic forKey:@"transform"];
[starView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.65];
[self performSelector:@selector(reloadBadgeNumber) withObject:nil afterDelay:0.65];
}
// update the Badge number
- (void)reloadBadgeNumber {
if(self.mainAry.count) {
cartViewController *cv=[self.storyboard instantiateViewControllerWithIdentifier:@"cvc"];
[self presentViewController:cv animated:YES completion:^{
[cv.cartTableV reloadData];
}];
[self dismissViewControllerAnimated:YES completion:nil];
// self.cartTab.badgeValue=[NSString stringWithFormat:@"%lu",(unsigned long)self.mainAry.count];
NSLog(@"%@ %@",_mainAry,_imageAry);
ad.aryItems=_mainAry;
} else {
self.cartTab.badgeValue=nil;
}
}
@end
customTableCell.h
#import <UIKit/UIKit.h>
@interface customTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageV;
@property (strong, nonatomic) IBOutlet UILabel *nameL;
@property (strong, nonatomic) IBOutlet UIButton *addToCartBtn;
@end
customTableCell.m
#import "customTableViewCell.h"
@implementation customTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
cartVC.h
#import <UIKit/UIKit.h>
#import "PayPalMobile.h"
#import "PayPalConfiguration.h"
#import "PayPalPaymentViewController.h"
@interface cartViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,PayPalPaymentDelegate>
@property (strong, nonatomic) IBOutlet UITableView *cartTableV;
@property NSMutableArray *cartAry;
@property (strong, nonatomic) IBOutlet UITabBarItem *cartBtnAction;
- (IBAction)paymentAct:(id)sender;
@property PayPalConfiguration *paypalConfig;
@end
cartVC.m
#import "cartViewController.h"
#import "ViewController.h"
#import "AppDelegate.h"
#import "displayCartTableViewCell.h"
@interface cartViewController (){
AppDelegate *ad;
}
@end
@implementation cartViewController
- (void)viewDidLoad {
[super viewDidLoad];
ad=[[UIApplication sharedApplication]delegate];
_cartTableV.delegate=self;
_cartTableV.dataSource=self;
//paypal Payment
_paypalConfig=[[PayPalConfiguration alloc]init];
_paypalConfig.acceptCreditCards=YES;
_paypalConfig.merchantName=@"Naresh";
_paypalConfig.merchantPrivacyPolicyURL = [NSURL URLWithString:@"https://www.paypal.com/webapps/mpp/ua/privacy-full"];
_paypalConfig.merchantUserAgreementURL = [NSURL URLWithString:@"https://www.paypal.com/webapps/mpp/ua/useragreement-full"];
_paypalConfig.languageOrLocale=[NSLocale preferredLanguages][0];
_paypalConfig.payPalShippingAddressOption=PayPalShippingAddressOptionPayPal;
NSLog(@"PAY PAL SDK :%@",[PayPalMobile libraryVersion]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ad.aryItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier=@"dcvc";
displayCartTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"displayCartTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(@"%@",ad.aryItems);
cell.labelCart.text=[NSString stringWithFormat:@"%lu %@",(indexPath.row +1),[ad.aryItems objectAtIndex:indexPath.row]];
cell.imageCart.image=[ad.imageItems objectAtIndex:indexPath.row];
return cell;
}
//refresh the data
-(void)viewWillAppear:(BOOL)animated{
[self.cartTableV reloadData];
self.cartBtnAction.badgeValue=[NSString stringWithFormat:@"%lu",(unsigned long)ad.aryItems.count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[ad.aryItems removeObjectAtIndex:indexPath.row];
[ad.imageItems removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//paypal Payment
- (IBAction)paymentAct:(id)sender {
PayPalItem *item1=[PayPalItem itemWithName:@"iPhone6" withQuantity:1 withPrice:[NSDecimalNumber decimalNumberWithString:@"74.30"] withCurrency:@"USD" withSku:@"SKU-iPhone6"];
PayPalItem *item2=[PayPalItem itemWithName:@"iPhone5" withQuantity:1 withPrice:[NSDecimalNumber decimalNumberWithString:@"545.30"] withCurrency:@"USD" withSku:@"SKU-iPhone5"];
NSArray *items=@[item1,item2];
NSDecimalNumber *subtotal=[PayPalItem totalPriceForItems:items];
NSDecimalNumber *shipping=[[NSDecimalNumber alloc]initWithString:@"5.28"];
NSDecimalNumber *tax=[[NSDecimalNumber alloc]initWithString:@"1.2"];
PayPalPaymentDetails *paymentDetails=[PayPalPaymentDetails paymentDetailsWithSubtotal:subtotal withShipping:shipping withTax:tax];
NSDecimalNumber *total=[[subtotal decimalNumberByAdding:shipping]decimalNumberByAdding:tax];
PayPalPayment *payment=[[PayPalPayment alloc]init];
payment.amount=total;
payment.currencyCode=@"USD";
payment.shortDescription=@"My payment";
payment.items=items;
payment.paymentDetails=paymentDetails;
if (!payment.processable) {
//write code payment is -ve
}
PayPalPaymentViewController *paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment
configuration:self.paypalConfig
delegate:self];
[self presentViewController:paymentViewController animated:YES completion:nil];
}
#pragma mark - PaypalSDKDelegate Methods
- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController {
NSLog(@"paypal payment cancel");
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController didCompletePayment:(PayPalPayment *)completedPayment {
NSLog(@"Payment sucess" );
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
displayCartCustomCell.h
#import <UIKit/UIKit.h>
@interface displayCartTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *valueL;
- (IBAction)addAct:(id)sender;
- (IBAction)removeCellAct:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *labelCart;
@property (strong, nonatomic) IBOutlet UIImageView *imageCart;
@end
displayCartCustomCell.m
#import "displayCartTableViewCell.h"
#import "AppDelegate.h"
#import "cartViewController.h"
@implementation displayCartTableViewCell{
AppDelegate *ad;
}
NSInteger counter = 1;
- (void)awakeFromNib {
ad=[[UIApplication sharedApplication]delegate];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)addAct:(id)sender {
[_valueL setText:[NSString stringWithFormat:@"%ld",(long)++counter]];
if(counter >9)
{
_valueL.text=[NSString stringWithFormat:@"%i",9];
counter=9;
}
}
- (IBAction)removeCellAct:(id)sender {
[_valueL setText:[NSString stringWithFormat:@"%ld",(long)--counter]];
if (counter < 1 ){
_valueL.text=[NSString stringWithFormat:@"%i",1];
counter=1;
}
return;
}
- (IBAction)deleteCellBtn:(id)sender {
cartViewController * firstView = [cartViewController new];
UINavigationController * navViewController = [[UINavigationController alloc]initWithRootViewController:firstView];
self.window.rootViewController = navViewController;
//[firstView.cartTableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
// CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:firstView.cartTableV];
// NSIndexPath *indexPath = [firstView.cartTableV indexPathForRowAtPoint:buttonPosition];
// [ad.aryItems removeObjectAtIndex:indexPath.row];
// [firstView.cartTableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[firstView.cartTableV reloadData];
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon