Skip to content

Instantly share code, notes, and snippets.

View Kjuly's full-sized avatar

Kaijie Yu Kjuly

View GitHub Profile
@Kjuly
Kjuly / ky_registering_for_remote_notifications.md
Last active June 22, 2016 21:53
Registering for Remote Notifications (APN)

Client side:

An app must register with Apple Push Notification service (APNs) to receive remote notifications sent by the app’s push provider. In iOS 8 and later, registration has four stages:

  1. Register the notification types your app supports using registerUserNotificationSettings:.
  2. Register to receive push notifications via APNs by calling your app’s registerForRemoteNotifications method.
  3. Store the device token returned to the app delegate by the server for a successful registration, or handle registration failure gracefully.
  4. Forward the device token to the app’s push provider.

Server side:

@Kjuly
Kjuly / remove_all_viewdidunload_snippets_from_your_projects_vcs.md
Last active December 24, 2015 03:29
The method -viewDidUnload is deprecated in iOS 6.0. Here offers a way to remove all -viewDidUnload snippets from your project's view controllers in batch.

Remove All -viewDidUnload Snippets From Your Project's VCs

As Apple's Official Doc said:

-viewDidUnload is deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.

Meanwhile, it's better to remove all -viewDidUnload implements from projects that the deployment target is iOS 6.0 or later. But removing it one by one is boring, especially you've hundreds of controllers & several projects.

So, is there any script or command can do this batch job?

@Kjuly
Kjuly / ky_ignoring_just_one_deprecated_warning.m
Last active December 23, 2015 03:59
Ignore just one deprecated warning. e.g.: calling UIDevice's |-uniqueIdentifier| without warning.
/*
Disable deprecated-declarations warning.
See http://clang.llvm.org/docs/UsersManual.html#diagnostics_pragmas
Basic workflow:
1. push current warnings onto stack
2. ignore warning we know will get thrown
3. do dodgy thing that causes warning
4. pop warnings - go back to what we had before we started fiddling with them
@Kjuly
Kjuly / ky_iOS7_note.m
Created September 16, 2013 04:27
Note for iOS7.0.
/* Status Bar
*
* Related threads:
* - https://devforums.apple.com/message/867059#867059
* - https://devforums.apple.com/thread/199395?start=0&tstart=0
*
* The relevant messages to override are:
*/
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO
@Kjuly
Kjuly / ky_grep_content_global.sh
Created August 4, 2013 15:02
Use grep to search "THE_TARGET_CONTENT" in all files. It'll list all files that include the content
// Search "THE_TARGET_CONTENT" in all .m files
find . -type f -iname "*.m" -exec grep -i "THE_TARGET_CONTENT" {} +
@Kjuly
Kjuly / ky_convert_12345678910_to_1234xxx8910.m
Last active December 18, 2015 16:29
Convert "1234567890" to "1234***8910" (Obj-C).
// Result: 1234***8910
NSString * original = @"12345678910";
NSError *error = nil;
NSRegularExpression * regex =
[NSRegularExpression regularExpressionWithPattern:@"\\d"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSString * result =
[regex stringByReplacingMatchesInString:original
@Kjuly
Kjuly / ky_create_c_array_of_retained_pointers_under_arc.m
Last active December 16, 2015 04:38
Create a C array of retained pointers under ARC (Obj-C)
// Note calloc() to get zero-filled memory.
__strong SomeClass **dynamicArray = (__strong SomeClass **)calloc(sizeof(SomeClass *), entries);
for (int i = 0; i < entries; ++i)
dynamicArray[i] = [[SomeClass alloc] init];
// When you're done, set each entry to nil to tell ARC to release the object.
for (int i = 0; i < entries; ++i)
dynamicArray[i] = nil;
free(dynamicArray);
@Kjuly
Kjuly / ky_xcode_version_management.sh
Last active October 3, 2015 19:58
Xcode version management script
#!/bin/bash
cd $PROJECT_DIR
# BUILD_VERSION=`/usr/local/bin/git rev-parse --short HEAD`
BUILD_VERSION=`git rev-parse --short HEAD`
cd $BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app
# Note: It's Info.plist, not Proj-Info.plist
RELEASE_VERSION=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" Info.plist)
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BUILD_VERSION" Info.plist
# here, 5 is my index of version part in |PreferenceSpecifiers| array
/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:5:DefaultValue $RELEASE_VERSION ($BUILD_VERSION)" Settings.bundle/Root.plist
@Kjuly
Kjuly / ky_auto_change_topbar_position.js
Last active September 27, 2015 09:28
Auto change the topbar position type depend on scrollbar
/*!
* Author: Kjuly(Kj Yu)
* Date: 09/29/2011
* Feel free to use this code snippet. ;)
*
*/
$(function() {
$(window).scroll(function() {
if ($(this).scrollTop() >= 100) { // The Logo's above the topbar is 100px
@Kjuly
Kjuly / ky_webview_edit_code.js
Last active September 27, 2015 00:08
Make the web page editable anytime.
// Put it in as the browser address & press ENTER, after you have opened a web page.
javascript:document.body.contentEditable='true';document.designMode='on';void 0