'IOS'에 해당되는 글 4

  1. 2015.07.06 IOS PUSH & CUSTOM ALERT
  2. 2015.07.06 IOS 유니코드 변경
  3. 2015.07.06 IOS 헥사코드(웹칼러)를 UICOLOR로
  4. 2015.07.06 IOS 루팅확인

IOS PUSH & CUSTOM ALERT

APNS를 쓰기위한 기본 설정 == 앱 델리게이트에 추가==
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/*
// 어플리케이션 실행시 옵션사항 중 Push 서비스 관련 정보를 추출
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
 
if(userInfo != nil)
{
[self application:application didFinishLaunchingWithOptions:userInfo];
}
*/
 
//-- Set Notification
#ifdef __IPHONE_8_0
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
#endif
// Add registration for remote notifications
application.applicationIconBadgeNumber = 0;
return YES;
}
 
// 어플리케이션이 최초 실행될 때에 어플리케이션이 푸시서비스를 이용함을 알리고 허용할지를 물어보게 하고 사용자의 동의를 얻었을 경우 실행되는 메서드
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
/**
* 이 메서드가 호출되면 APNS에 디바이스 정보를 등록하고 64바이트의 문자열을 받아오게 된다.
*/
NSMutableString *deviceId = [NSMutableString string];
const unsigned char* ptr = (const unsigned char*) [deviceToken bytes];
 
for(int i = 0 ; i < 32 ; i++)
{
[deviceId appendFormat:@"%02x", ptr[i]];
}
 
// 여기서 추출된 deviceId를 서드파티 서비스의 서버로 전송하여 관리한다.
_strApns = deviceId;
NSLog(@"APNS Device Token: %@", _strApns);
}
 
// 푸시 메시지를 받았을 경우 호출되는 메서드
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
//UIView *topView = window.rootViewController.view;
[StringUtil customAlert:window.rootViewController.view title:@"PUSH" message:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]];
}
 
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Error in registration. Error: %@", error);
}
 
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
 
}
오호라
[StringUtil customAlert:window.rootViewController.view title:@"PUSH" message:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]]; 
를 이용해서 푸쉬를 추가했네요.... StringUtil은 스태틱 클래스 펑션입니다. 먼저 헤더를 보면 두개의 함수가 있는데 얼렛창 여는것 닫는것 해서 있네요
#import 
 
@interface StringUtil : NSObject

+(void)customAlert:(UIView*)view title:(NSString*)strTitle message:(NSString*)strMsg;
+(void)closeAlert:(id)sender;

 
@end
이제 클래스쪽을 볼께요

#import "StringUtil.h" #import

@implementation StringUtil UIView* alertView; +(void)closeAlert:(id)sender{ NSLOG(@"function : %s ", __FUNCTION__); alertView.hidden = TRUE; } +(void)customAlert:(UIView*)view title:(NSString*)strTitle message:(NSString*)strMsg { if(alertView != NULL){     [alertView removeFromSuperview]; }
CGSize size = view.frame.size;
float x = 0.1, y=0.3;
alertView=[[UIView alloc]initWithFrame:CGRectMake(0,0, size.width, size.height)];
[alertView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
 
UIView* bgView=[[UIView alloc]initWithFrame:CGRectMake(size.width*x, size.height*y, size.width*(1-x*2), size.height*(1-y*2))];
[bgView setBackgroundColor:[StringUtil colorWithHexString:CONST_BG]];
bgView.layer.cornerRadius = 10;
bgView.layer.masksToBounds = YES;
UIView* bgBottomView=[[UIView alloc]initWithFrame:CGRectMake(0,bgView.frame.size.height-40,bgView.frame.size.width,40)];
[bgBottomView setBackgroundColor:[UIColor whiteColor]];
UIView* bgTopView=[[UIView alloc]initWithFrame:CGRectMake(0,0,bgView.frame.size.width,40)];
[bgTopView setBackgroundColor:[UIColor whiteColor]];
UILabel *titleBorder = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, bgView.frame.size.width, 1)];
[titleBorder setBackgroundColor:[StringUtil colorWithHexString:CONST_BG_BORDER]];
[bgView addSubview:titleBorder];
UIButton *btnClose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 25)];
btnClose.layer.cornerRadius = 10;
btnClose.layer.masksToBounds = YES;
[btnClose setCenter:CGPointMake(bgBottomView.frame.size.width/2, bgBottomView.frame.size.height/2)];
btnClose.titleLabel.font = [UIFont systemFontOfSize:12.0];
[btnClose setBackgroundColor:[UIColor whiteColor]];
[btnClose setTitle:@"CLOSE"  forState:UIControlStateNormal];
[btnClose setTitleColor:[StringUtil colorWithHexString:CONST_BG] forState:UIControlStateNormal];
[btnClose setTitle:@"CLOSE" forState:UIControlStateHighlighted];
[btnClose addTarget:self action:@selector(closeAlert:) forControlEvents:UIControlEventTouchUpInside];
[btnClose setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[btnClose setBackgroundImage:[StringUtil imageWithColor:[StringUtil colorWithHexString:CONST_BG]] forState:UIControlStateHighlighted];
[[btnClose layer] setBorderWidth:2.0f];
[[btnClose layer] setBorderColor:[StringUtil colorWithHexString:CONST_BG].CGColor];
[bgBottomView addSubview:btnClose];
UILabel *viewActivityLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
[viewActivityLabel1 setCenter:CGPointMake(bgTopView.frame.size.width/2, bgTopView.frame.size.height/2)];
viewActivityLabel1.text = strTitle;
[viewActivityLabel1 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.0]];
viewActivityLabel1.font = [UIFont fontWithName:@"Helvetica" size:15];
viewActivityLabel1.textColor = [StringUtil colorWithHexString:CONST_BG];
viewActivityLabel1.textAlignment = NSTextAlignmentCenter;
[bgTopView addSubview:viewActivityLabel1];
UILabel *viewActivityLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 60)];
viewActivityLabel2.text = strMsg;
[viewActivityLabel2 setCenter:CGPointMake(bgView.frame.size.width/2, bgView.frame.size.height/2)];
[viewActivityLabel2 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.0]];
viewActivityLabel2.font = [UIFont fontWithName:@"Helvetica" size:12];
viewActivityLabel2.textColor = [UIColor whiteColor];
viewActivityLabel2.textAlignment = NSTextAlignmentCenter;
[bgView addSubview:viewActivityLabel2];
[bgView addSubview:bgTopView];
[bgView addSubview:bgBottomView];
[alertView addSubview:bgView];
[view addSubview:alertView];

}
 
@end
네 이제 해당 StringUtil.h파일을 pch파일에 등록을 하시면 커스텀 알림창이 뜹니다...


IOS 유니코드 변경

+(NSString *)urlEncodeValue:(NSString *)str{

str = 

[str stringByAddingPercentEscapesUsingEncoding:0x80000000 

kCFStringEncodingUnicode];

return 

(__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(

kCFAllocatorDefault,

(__bridgeCFStringRef)str

,NULL,CFSTR("?=&+"),kCFStringEncodingUTF8);

 

}

IOS 헥사코드(웹칼러)를 UICOLOR로

웹칼라를 UICOLOR로 변경하는 툴입니다.



+(UIColor*)colorWithHexString:(NSString*)hex

{

NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]uppercaseString];

// String should be 6 or 8 characters

if ([cString length] < 6return [UIColor grayColor];

// strip 0X if it appears

if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

if ([cString length] != 6return  [UIColor grayColor];

// Separate into r, g, b substrings

NSRange range;

range.location = 0;

range.length = 2;

NSString *rString = [cString substringWithRange:range];

range.location = 2;

NSString *gString = [cString substringWithRange:range];

range.location = 4;

NSString *bString = [cString substringWithRange:range];

// Scan values

unsigned int r, g, b;

[[NSScanner scannerWithString:rString] scanHexInt:&r];

[[NSScanner scannerWithString:gString] scanHexInt:&g];

[[NSScanner scannerWithString:bString] scanHexInt:&b];

return [UIColor colorWithRed:((float) r / 255.0f)

  green:((float) g / 255.0f)

blue:((float) b / 255.0f)

  alpha:1.0f];

 

}

IOS 루팅확인

+(BOOL) isRooting {

    NSString *filePath = @"/Applications/Cydia.app";

#if defined(__ROOTING_TRUE__)

    return TRUE;

#endif

    if ([[NSFileManager defaultManagerfileExistsAtPath:filePath]) return TRUE;

    else return FALSE;

}