Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit a3b4682

Browse files
committed
Merge pull request #65 from AgileBits/task/update-readme-code-snippets
Updated the README
2 parents 2c7c7af + a516cb1 commit a3b4682

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

Demos/App Demo for iOS/App Demo for iOS/ChangePasswordViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ - (UIStatusBarStyle)preferredStatusBarStyle{
3333
}
3434

3535
- (IBAction)changePasswordIn1Password:(id)sender {
36+
// Password generation options are optional, but are very handy in case you have strict rules about password lengths
3637
NSDictionary *passwordGenerationOptions = @{
3738
AppExtensionGeneratedPasswordMinLengthKey: @(6),
3839
AppExtensionGeneratedPasswordMaxLengthKey: @(50)

Demos/App Demo for iOS/App Demo for iOS/RegisterViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ - (IBAction)saveLoginTo1Password:(id)sender {
4646
}
4747
};
4848

49+
// Password generation options are optional, but are very handy in case you have strict rules about password lengths
4950
NSDictionary *passwordGenerationOptions = @{
5051
AppExtensionGeneratedPasswordMinLengthKey: @(6),
5152
AppExtensionGeneratedPasswordMaxLengthKey: @(50)

README.md

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,20 @@ Next we need to wire up the action for this button to this method in your UIView
9898
```objective-c
9999
- (IBAction)findLoginFrom1Password:(id)sender {
100100
__weak typeof (self) miniMe = self;
101-
[[OnePasswordExtension sharedExtension] findLoginForURLString:@"https://www.acme.com" forViewController:self completion:^(NSDictionary *loginDict, NSError *error) {
101+
[[OnePasswordExtension sharedExtension] findLoginForURLString:@"https://www.acme.com" forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
102102
if (!loginDict) {
103-
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);
103+
if (error.code != AppExtensionErrorCodeCancelledByUser) {
104+
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);
105+
}
104106
return;
105107
}
106-
108+
107109
__strong typeof(self) strongMe = miniMe;
108110
strongMe.usernameTextField.text = loginDict[AppExtensionUsernameKey];
109111
strongMe.passwordTextField.text = loginDict[AppExtensionPasswordKey];
110112
}];
111113
}
114+
}
112115
```
113116

114117
Aside from the [weak/strong self dance](http://dhoerl.wordpress.com/2013/04/23/i-finally-figured-out-weakself-and-strongself/), this code is pretty straight forward:
@@ -139,18 +142,21 @@ Adding 1Password to your registration screen is very similar to adding 1Password
139142
// Add as many string fields as you please.
140143
}
141144
};
142-
145+
146+
// Password generation options are optional, but are very handy in case you have strict rules about password lengths
143147
NSDictionary *passwordGenerationOptions = @{
144148
AppExtensionGeneratedPasswordMinLengthKey: @(6),
145149
AppExtensionGeneratedPasswordMaxLengthKey: @(50)
146150
};
147151

148152
__weak typeof (self) miniMe = self;
149153

150-
[[OnePasswordExtension sharedExtension] storeLoginForURLString:@"https://www.acme.com" loginDetails:newLoginDetails passwordGenerationOptions:passwordGenerationOptions forViewController:self completion:^(NSDictionary *loginDict, NSError *error) {
154+
[[OnePasswordExtension sharedExtension] storeLoginForURLString:@"https://www.acme.com" loginDetails:newLoginDetails passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
151155

152156
if (!loginDict) {
153-
NSLog(@"Failed to use 1Password App Extension to save a new Login: %@", error);
157+
if (error.code != AppExtensionErrorCodeCancelledByUser) {
158+
NSLog(@"Failed to use 1Password App Extension to save a new Login: %@", error);
159+
}
154160
return;
155161
}
156162

@@ -159,7 +165,7 @@ Adding 1Password to your registration screen is very similar to adding 1Password
159165
strongMe.usernameTextField.text = loginDict[AppExtensionUsernameKey] ? : @"";
160166
strongMe.passwordTextField.text = loginDict[AppExtensionPasswordKey] ? : @"";
161167
strongMe.firstnameTextField.text = loginDict[AppExtensionReturnedFieldsKey][@"firstname"] ? : @"";
162-
strongMe.lastnameTextField.text = loginDict[AppExtensionReturnedFieldsKey][@"lastname"] ? : @""
168+
strongMe.lastnameTextField.text = loginDict[AppExtensionReturnedFieldsKey][@"lastname"] ? : @"";
163169
// retrieve any additional fields that were passed in newLoginDetails dictionary
164170
}];
165171
}
@@ -169,16 +175,51 @@ You'll notice that we're passing a lot more information into 1Password than just
169175

170176
An important thing to notice is the `AppExtensionURLStringKey` is set to the exact same value we used in the login scenario. This allows users to quickly find the login they saved for your app the next time they need to sign in.
171177

178+
### Use Case #3: Change Password
172179

173-
### Use Case #3: Web View Support
180+
Allow your users to easily change passwords for saved logins in 1Password directly from your change password page. The old and the newly generated are returned to you so you can update your UI and complete the password change process.
181+
182+
Adding 1Password to your change password screen is very similar to adding 1Password to your login and registration screens. In this case you'll wire the 1Password button to an action like this:
183+
184+
```objective-c
185+
- (IBAction)changePasswordIn1Password:(id)sender {
186+
// Password generation options are optional, but are very handy in case you have strict rules about password lengths
187+
NSDictionary *passwordGenerationOptions = @{
188+
AppExtensionGeneratedPasswordMinLengthKey: @(6),
189+
AppExtensionGeneratedPasswordMaxLengthKey: @(50)
190+
};
191+
192+
__weak typeof (self) miniMe = self;
193+
NSString *username = [LoginInformation sharedLoginInformation].username ? : @"";
194+
[[OnePasswordExtension sharedExtension] changePasswordForLoginWithUsername:username andURLString:@"https://www.acme.com" passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
195+
if (!loginDict) {
196+
if (error.code != AppExtensionErrorCodeCancelledByUser) {
197+
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);
198+
}
199+
return;
200+
}
201+
202+
__strong typeof(self) strongMe = miniMe;
203+
strongMe.oldPasswordTextField.text = loginDict[AppExtensionOldPasswordKey];
204+
strongMe.freshPasswordTextField.text = loginDict[AppExtensionPasswordKey];
205+
strongMe.confirmPasswordTextField.text = loginDict[AppExtensionPasswordKey];
206+
}];
207+
}
208+
```
209+
210+
### Use Case #4: Web View Support
174211

175212
The 1Password App Extension is not limited to filling native UIs. With just a little bit of extra effort, users can fill `UIWebView`s and `WKWebView`s within your application as well.
176213

177214
Simply add a button to your UI with its action assigned to this method in your web view's UIViewController:
178215

179216
```objective-c
180217
- (IBAction)fillUsing1Password:(id)sender {
181-
[[OnePasswordExtension sharedExtension] fillLoginIntoWebView:self.webView forViewController:self completion:nil];
218+
[[OnePasswordExtension sharedExtension] fillLoginIntoWebView:self.webView forViewController:self sender:sender completion:^(BOOL success, NSError *error) {
219+
if (!success) {
220+
NSLog(@"Failed to fill login in webview: <%@>", error);
221+
}
222+
}];
182223
}
183224
```
184225

0 commit comments

Comments
 (0)