webview中事件的用法
2013-06-26 21:34 甘超波 阅读(3225) 评论(0) 编辑 收藏 举报封装 MBProgressHud ==================================== #import "MBProgressHUD.h" @interface MBProgressHUD (Add) + (void)showError:(NSString *)error toView:(UIView *)view; + (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view; @end ============================ #import "MBProgressHUD+Add.h" @implementation MBProgressHUD (Add) #pragma mark 显示错误信息 + (void)showError:(NSString *)error toView:(UIView *)view{ // 快速显示一个提示信息 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; hud.labelText = error; // 设置图片 hud.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_failed_icon.png"]] autorelease]; // 再设置模式 hud.mode = MBProgressHUDModeCustomView; // 隐藏时候从父控件中移除 hud.removeFromSuperViewOnHide = YES; // 1秒之后再消失 [hud hide:YES afterDelay:1]; } #pragma mark 显示一些信息 + (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view { // 快速显示一个提示信息 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; hud.labelText = message; // 隐藏时候从父控件中移除 hud.removeFromSuperViewOnHide = YES; // YES代表需要蒙版效果 //hud.dimBackground = YES; return hud; } @end
webview中MBPress的用法
#import "OauthController.h" #import "MBProgressHUD+Add.h" #import "WeiboAccount.h" #import "User.h" #import "UserMgr.h" @interface OauthController () { UIWebView *_webView; } @end @implementation OauthController #pragma mark - 生命周期方法 - (void)loadView { CGRect frame = [UIScreen mainScreen].applicationFrame; _webView = [[[UIWebView alloc] initWithFrame:frame] autorelease]; self.view = _webView; } - (void)viewDidLoad { [super viewDidLoad]; // 授权成功后,会跳到回调地址,并且把access_token拼接到回调地址后面 // reponse_type=token代表授权成功后直接返回access_token NSString *str = [kOAuthURL stringByAppendingFormat:@"?%@=%@&%@=%@&display=mobile&response_type=token", kClientId, kAppKey, kRedirectURIKey, kRedirectURI]; [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]]; _webView.delegate = self; } #pragma mark - UIWebView代理方法 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *url = request.URL.absoluteString; NSRange range = [url rangeOfString:@"access_token"]; int location = range.location; // 说明这是含有access_token的回调地址 if (location != NSNotFound) { // 截取后面的参数字符串 NSString *paramStr = [url substringFromIndex:location]; // 切割所有的参数 NSArray *params = [paramStr componentsSeparatedByString:@"&"]; // access_token=2.00iEK52CsE94CDd31b67b95dEUONIB // expires_in=157678625 // uid=2148327324 // 初始化账号 WeiboAccount *account = [WeiboAccount account]; // 遍历所有的参数 for (NSString *param in params) { NSRange tokenRange = [param rangeOfString:kAccessToken]; NSRange expiresRange = [param rangeOfString:kExpiresIn]; NSRange uidRange = [param rangeOfString:KUid]; // 找到了accessToken if (tokenRange.location != NSNotFound) { account.accessToken = [param substringFromIndex:tokenRange.location + tokenRange.length + 1]; } else if(expiresRange.location != NSNotFound) { // 找到了expiresIn account.expiresIn = [[param substringFromIndex:expiresRange.location + expiresRange.length + 1] longLongValue]; } else if(uidRange.location != NSNotFound) { // 找到了uid account.uid = [param substringFromIndex:uidRange.location + uidRange.length + 1]; } } // 获取用户数据 UserMgr *mgr = [UserMgr mgr]; [mgr queryUserWithUid:account.uid]; mgr.queryUserBlock = ^(User *user, NSString *error) { // 设置账号昵称 account.screenName = user.screenName; // 将账号写入沙盒 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documents stringByAppendingPathComponent:kAccountFile]; [NSKeyedArchiver archiveRootObject:account toFile:path]; [MBProgressHUD hideHUDForView:self.view animated:YES]; // 关闭当前控制器 [self dismissViewControllerAnimated:YES completion:nil]; }; return NO; } return YES; } - (void)webViewDidStartLoad:(UIWebView *)webView { self.title = kLoadingMsg; [MBProgressHUD showMessag:kLoadingMsg toView:self.view]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; [MBProgressHUD hideHUDForView:self.view animated:YES]; } @end
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频