Making Chrome extension
I use this template generator-chrome-extension to easily make Chrome extension. It is used in
I use this template generator-chrome-extension to easily make Chrome extension. It is used in
I use this template Xcode-Plugin-Template to easily make Xcode 7 plugin. It is used in
For Xcode 8, the evil way is to unsign Xcode https://github.com/inket/update_xcode_plugins
The other day I was watching , and he mentions Law of Jante
It is pretty much this
- You’re not to think you are anything special.
- You’re not to think you are as good as we are.
- You’re not to think you are smarter than we are.
- You’re not to convince yourself that you are better than we are.
- You’re not to think you know more than we do.
- You’re not to think you are more important than we are.
- You’re not to think you are good at anything.
- You’re not to laugh at us.
- You’re not to think anyone cares about you.
- You’re not to think you can teach us anything.
There are many discussions about this
Putting on your black hat, it sounds negative. Putting on your yellow hat, it sounds positive
But what I learn about it is the team work. No one lives alone, everyone lives among the others. It is about to be humble and learn collaboration
There are times we want to run an action just once. It would be nice if we can encapsulate this
dispatch_once
is meant to be used for action that runs once and only once
We can have
1 |
class Once { |
Then we can use it like
1 |
class ViewController: UIViewController { |
Functions in Swift are distinguishable by
so that these are all valid, and works for subscript
as well
1 |
struct A { |
When you specialize a generic type, like Array
, you’re actually using a concrete type
Unfortunately, this does not work for NSObject subclass
Method ‘get()’ with Objective-C selector ‘get’ conflicts with previous declaration with the same Objective-C selector
1 |
class B: NSObject { |
We can overload generic functions as well
1 |
func f |
This post is like a sum up of sum ways to configure your property
1 |
var label: UILabel! |
1 |
lazy var label: UILabel = { [weak self] in |
Ah, by the way, did you know that
label
in ViewController deinit
, because it is lazy
and we have weak self
lazy
increases your compile timeI first saw it on https://github.com/AliSoftware/Dip/blob/develop/Sources/Dip.swift#L61
1 |
public init( configBlock: (DependencyContainer->()) = { _ in }) { |
This https://github.com/devxoul/Then makes it easier to configure your property as an extension to NSObject
1 |
extension Then where Self: AnyObject { |
so we have
1 |
lazy var label: UILabel = UILabel().then { [weak self] in |
We have to declare label: UILabel
to use `[weak self]
I try to avoid extension, after reading this http://nshipster.com/new-years-2016/
1 |
public func Init |
we can use it like
1 |
lazy var label: UILabel = Init(UILabel()) { [weak self] in |
We have to declare label: UILabel
to use `[weak self]
This https://gist.github.com/erica/4fa60524d9b71bfa9819 makes configuration easier
1 |
lazy var label: UILabel = { [weak self] in |
GitHub is so awesome. It is where people around the world collaborate with each other.
It is more awesome to show more about you in your GitHub profile. How about a badge? a welcome text?
It is doable with organization. GitHub takes time and name of the organiazations you joined to determined how it displays on your profile
This is what shown on my GitHub profile https://github.com/onmyway133
For me, I display the text “Hello World”, so I have to create organizations for “h”, “e”, “l”, “l”, “o”, “w”, “o”, “r”, “l”, “d”
To ensure the order, you can name your organization like “org-h”, “org-he”, “org-hel”, “org-hell”, “org-hello”, … and you must join the organization in the correct order
I create another GitHub account called https://github.com/fantabot to manage my organizations
Your imaginary is your limit. May your code continue to compile :grin:
Swift allows us to define more methods on existing class using extension.
1 |
extension UIView { |
If you ‘re afraid of the naming conflict, you can prefix your methods. Or a better way, reverse it :dancer: , like
1 |
view.animation.shake() |
This way, no more conflict and we make it clear that shake()
and fade()
belongs to animation
category
Actually, animation
and layout
are properties in UIView
extension. This may cause naming conflict, but the number of them is reduced
This is how it works
1 |
extension UIView { |
This is applied in Wave
Here are my notes for working with Push Notification, updated for iOS 9
registerForRemoteNotificationTypes
is deprecated in iOS 8+
1 |
UIApplication.sharedApplication().registerForRemoteNotifications() |
If your app displays alerts, play sounds, or badges its icon, you must call this method during your launch cycle to request permission to alert the user in these ways
1 |
let types: UIUserNotificationType = [.Badge, .Sound, .Alert] |
You don’t need to wait for registerUserNotificationSettings
to callback before calling registerForRemoteNotifications
Never cache a device token; always get the token from the system whenever you need it. If your app previously registered for remote notifications, calling the registerForRemoteNotifications method again does not incur any additional overhead, and iOS returns the existing device token to your app delegate immediately. In addition, iOS calls your delegate method any time the device token changes, not just in response to your app registering or re-registering
The user can change the notification settings for your app at any time using the Settings app. Because settings can change, always call the registerUserNotificationSettings: at launch time and use the application:didRegisterUserNotificationSettings: method to get the response. If the user disallows specific notification types, avoid using those types when configuring local and remote notifications for your app.
About application:didReceiveRemoteNotification:
Implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one whenever possible. If your delegate implements both methods, the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.
If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.
About application:didReceiveRemoteNotification:fetchCompletionHandler:
This is for silent push notification with content-available
Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background
In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a push notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
If the user opens your app from the system-displayed alert, the system may call this method again when your app is about to enter the foreground so that you can update your user interface and display information pertaining to the notification.
Usually, the use of push notification is to display a specific article, a specific DetailViewController, … in your app. So the good practices are
1 |
- func handlePushNotification(userInfo: NSDictionary) { |
Here we create another method `handlePushNotification:`` to handle push notification. When you receive push notification, 3 cases can occur
Loud push
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledSilent push
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledLoud push
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledSilent push
application:didReceiveRemoteNotification:fetchCompletionHandler:
called. If app is suspended, its state changed to UIApplicationStateBackground
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledLoud push
application:didFinishLaunchingWithOptions:
with launchOptions
, application:didReceiveRemoteNotification:fetchCompletionHandler:
calledapplication:didFinishLaunchingWithOptions:
is called with launchOptions
set to nilSilent push
application:didReceiveRemoteNotification:fetchCompletionHandler:
called. If app was not killed by user, it is woke up and state changed to UIApplicationStateInactive
.application:didFinishLaunchingWithOptions:
with launchOptions
, application:didReceiveRemoteNotification:fetchCompletionHandler:
calledapplication:didFinishLaunchingWithOptions:
is called with launchOptions
set to nilSystem alert only show if the payload contains “alert”
1 |
{ |
For now I see that silent push must contain “sound” for application:didReceiveRemoteNotification:fetchCompletionHandler:
to be called when app is in background
1 |
{ |
Here are my notes for working with Push Notification, updated for iOS 9
registerForRemoteNotificationTypes
is deprecated in iOS 8+
1 |
UIApplication.sharedApplication().registerForRemoteNotifications() |
If your app displays alerts, play sounds, or badges its icon, you must call this method during your launch cycle to request permission to alert the user in these ways
1 |
let types: UIUserNotificationType = [.Badge, .Sound, .Alert] |
You don’t need to wait for registerUserNotificationSettings
to callback before calling registerForRemoteNotifications
Never cache a device token; always get the token from the system whenever you need it. If your app previously registered for remote notifications, calling the registerForRemoteNotifications method again does not incur any additional overhead, and iOS returns the existing device token to your app delegate immediately. In addition, iOS calls your delegate method any time the device token changes, not just in response to your app registering or re-registering
The user can change the notification settings for your app at any time using the Settings app. Because settings can change, always call the registerUserNotificationSettings: at launch time and use the application:didRegisterUserNotificationSettings: method to get the response. If the user disallows specific notification types, avoid using those types when configuring local and remote notifications for your app.
About application:didReceiveRemoteNotification:
Implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one whenever possible. If your delegate implements both methods, the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.
If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.
About application:didReceiveRemoteNotification:fetchCompletionHandler:
This is for silent push notification with content-available
Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background
In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a push notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
If the user opens your app from the system-displayed alert, the system may call this method again when your app is about to enter the foreground so that you can update your user interface and display information pertaining to the notification.
Usually, the use of push notification is to display a specific article, a specific DetailViewController, … in your app. So the good practices are
1 |
- func handlePushNotification(userInfo: NSDictionary) { |
Here we create another method `handlePushNotification:`` to handle push notification. When you receive push notification, 3 cases can occur
Loud push
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledSilent push
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledLoud push
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledSilent push
application:didReceiveRemoteNotification:fetchCompletionHandler:
called. If app is suspended, its state changed to UIApplicationStateBackground
application:didReceiveRemoteNotification:fetchCompletionHandler:
calledLoud push
application:didFinishLaunchingWithOptions:
with launchOptions
, application:didReceiveRemoteNotification:fetchCompletionHandler:
calledapplication:didFinishLaunchingWithOptions:
is called with launchOptions
set to nilSilent push
application:didReceiveRemoteNotification:fetchCompletionHandler:
called. If app was not killed by user, it is woke up and state changed to UIApplicationStateInactive
.application:didFinishLaunchingWithOptions:
with launchOptions
, application:didReceiveRemoteNotification:fetchCompletionHandler:
calledapplication:didFinishLaunchingWithOptions:
is called with launchOptions
set to nilSystem alert only show if the payload contains “alert”
1 |
{ |
For now I see that silent push must contain “sound” for application:didReceiveRemoteNotification:fetchCompletionHandler:
to be called when app is in background
1 |
{ |